Skip to content

Commit

Permalink
feat(core): install plugins in a separate directory
Browse files Browse the repository at this point in the history
This commit:
 1- modifies the install method to copy the plugin folder to ~/.coffee/<network>/plugins/<plugin_name>
 2- modifies the remove method to remove the plugin folder when uninstalling

 This also allows for future enhancements to the upgrade method

Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Jul 29, 2023
1 parent 953030a commit 0c7f7e3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coffee_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"

[dependencies]
tokio = { version = "1.22.0", features = ["full"] }
fs_extra = "1.3.0"
async-trait = "0.1.57"
coffee_lib = { path = "../coffee_lib" }
coffee_github = { path = "../coffee_github" }
Expand Down
76 changes: 57 additions & 19 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Coffee mod implementation
use coffee_storage::nosql_db::NoSQlStorage;
use fs_extra::dir;
use std::collections::HashMap;
use std::fmt::Debug;
use std::vec::Vec;
Expand Down Expand Up @@ -223,27 +224,61 @@ impl PluginManager for CoffeeManager {
for repo in self.repos.values() {
if let Some(mut plugin) = repo.get_plugin_by_name(plugin) {
log::trace!("{:#?}", plugin);
let result = plugin.configure(verbose).await;
log::debug!("result from plugin configure: {:?}", result);
match result {
Ok(path) => {
log::debug!("runnable plugin path {path}");
if !try_dynamic {
self.config.plugins.push(plugin);
log::debug!("path coffee conf: {}", self.coffee_cln_config.path);
self.coffee_cln_config
.add_conf("plugin", &path.to_owned())
.map_err(|err| CoffeeError::new(1, &err.cause))?;
log::debug!("coffee conf updated: {}", self.coffee_cln_config);
self.flush().await?;
self.update_conf().await?;
} else {
self.start_plugin(&path).await?;

// old_root_path is the path where the plugin is cloned and currently stored
// eg. ~/.coffee/repositories/<repo_name>/<plugin_name>
let old_root_path = plugin.root_path.clone();
// new_root_path is the path where the plugin will be installed specific to the network
// eg. ~/.coffee/<network>/plugins/<plugin_name>
let new_root_path = format!(
"{}/{}/plugins/{}",
self.config.root_path,
self.config.network,
plugin.name()
);
dir::copy(
&old_root_path,
format!("{}/{}/plugins/", self.config.root_path, self.config.network),
&dir::CopyOptions::new(),
)
.map_err(|err| error!("{}", err.to_string()))?;

let old_exec_path = plugin.exec_path.clone();

match old_exec_path.strip_prefix(&old_root_path) {
Some(relative_path) => {
let new_exec_path = format!("{}{}", new_root_path, relative_path);
plugin.root_path = new_root_path;
plugin.exec_path = new_exec_path;

log::debug!("plugin: {:?}", plugin);
let result = plugin.configure(verbose).await;
log::debug!("result from plugin configure: {:?}", result);
match result {
Ok(path) => {
log::debug!("runnable plugin path {path}");
if !try_dynamic {
self.config.plugins.push(plugin);
log::debug!(
"path coffee conf: {}",
self.coffee_cln_config.path
);
self.coffee_cln_config
.add_conf("plugin", &path.to_owned())
.map_err(|err| CoffeeError::new(1, &err.cause))?;
log::debug!("coffee conf updated: {}", self.coffee_cln_config);
self.flush().await?;
self.update_conf().await?;
} else {
self.start_plugin(&path).await?;
}
return Ok(());
}
Err(err) => return Err(err),
}
return Ok(());
}
Err(err) => return Err(err),
}
None => return Err(error!("exec path not found")),
};
}
}
Err(error!(
Expand All @@ -257,6 +292,7 @@ impl PluginManager for CoffeeManager {
if let Some(index) = plugins.iter().position(|x| x.name() == plugin) {
let plugin = plugins[index].clone();
let exec_path = plugin.exec_path.clone();
fs::remove_dir_all(plugin.root_path.clone()).await?;
log::debug!("runnable plugin path: {exec_path}");
plugins.remove(index);
log::debug!("coffee cln config: {}", self.coffee_cln_config);
Expand All @@ -278,6 +314,8 @@ impl PluginManager for CoffeeManager {
}

async fn upgrade(&mut self, repo: &str) -> Result<CoffeeUpgrade, CoffeeError> {
// TODO: upgrade should now be able to upgrade a single plugin
// without affecting other plugins installed from the same repo
let repository = self
.repos
.get(repo)
Expand Down
1 change: 1 addition & 0 deletions coffee_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl CoffeeConf {
coffee.bind_cmd_line_params(conf)?;

check_dir_or_make_if_missing(format!("{def_path}/{}", coffee.network)).await?;
check_dir_or_make_if_missing(format!("{def_path}/{}/plugins", coffee.network)).await?;
check_dir_or_make_if_missing(format!("{def_path}/repositories")).await?;
// after we know all the information regarding
// the configuration we try to see if there is
Expand Down
2 changes: 2 additions & 0 deletions coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ impl Repository for Github {
let mut plugins_effected: Vec<String> = vec![];
let remote_repo = self.list().await?;

// FIXME: upgrading the repository must also upgrade the commit
// field of all plugins cloned from this repository.
let plugins = plugins.clone();

// FIXME: mark inside a repository what plugin is installed, and remove
Expand Down

0 comments on commit 0c7f7e3

Please sign in to comment.