diff --git a/coffee_cmd/src/cmd.rs b/coffee_cmd/src/cmd.rs index 702b90c..c171095 100644 --- a/coffee_cmd/src/cmd.rs +++ b/coffee_cmd/src/cmd.rs @@ -29,6 +29,8 @@ pub enum CoffeeCommand { verbose: bool, #[arg(short, long, action = clap::ArgAction::SetTrue)] dynamic: bool, + #[arg(short, long)] + branch: Option, }, /// upgrade a single repository. #[clap(arg_required_else_help = true)] @@ -96,9 +98,10 @@ impl From<&CoffeeCommand> for coffee_core::CoffeeOperation { match value { CoffeeCommand::Install { plugin, + branch, verbose, dynamic, - } => Self::Install(plugin.to_owned(), *verbose, *dynamic), + } => Self::Install(plugin.to_owned(), branch.clone(), *verbose, *dynamic), CoffeeCommand::Upgrade { repo, verbose } => Self::Upgrade(repo.to_owned(), *verbose), CoffeeCommand::List {} => Self::List, CoffeeCommand::Setup { cln_conf } => Self::Setup(cln_conf.to_owned()), diff --git a/coffee_cmd/src/main.rs b/coffee_cmd/src/main.rs index c64106e..ef61a74 100644 --- a/coffee_cmd/src/main.rs +++ b/coffee_cmd/src/main.rs @@ -18,6 +18,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr match args.command { CoffeeCommand::Install { plugin, + branch, verbose, dynamic, } => { @@ -26,7 +27,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr } else { None }; - match coffee.install(&plugin, verbose, dynamic).await { + match coffee.install(&plugin, branch, verbose, dynamic).await { Ok(_) => { spinner.and_then(|spinner| Some(spinner.finish())); term::success!("Plugin {plugin} Compiled and Installed") diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index 1f0f01c..70fd5b6 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -15,6 +15,7 @@ use serde_json::json; use tokio::process::Command; use coffee_github::repository::Github; +use coffee_github::utils::git_checkout; use coffee_lib::errors::CoffeeError; use coffee_lib::plugin_manager::PluginManager; use coffee_lib::repository::Repository; @@ -255,6 +256,7 @@ impl PluginManager for CoffeeManager { async fn install( &mut self, plugin: &str, + branch: Option, verbose: bool, try_dynamic: bool, ) -> Result<(), CoffeeError> { @@ -315,6 +317,13 @@ impl PluginManager for CoffeeManager { plugin.root_path = new_root_path; plugin.exec_path = new_exec_path; + if let Some(branch) = branch { + // FIXME: Where we store the date? how we manage it? + let (commit, _) = + git_checkout(&plugin.root_path, &branch, verbose).await?; + plugin.commit = Some(commit); + } + log::debug!("plugin: {:?}", plugin); let path = plugin.configure(verbose).await?; log::debug!("runnable plugin path {path}"); @@ -407,7 +416,7 @@ impl PluginManager for CoffeeManager { UpgradeStatus::Updated(_, _) => { for plugins in status.plugins_effected.iter() { self.remove(plugins).await?; - self.install(plugins, verbose, false).await?; + self.install(plugins, None, verbose, false).await?; } } _ => {} diff --git a/coffee_core/src/lib.rs b/coffee_core/src/lib.rs index 9629f58..e4bafd4 100644 --- a/coffee_core/src/lib.rs +++ b/coffee_core/src/lib.rs @@ -7,8 +7,8 @@ pub use coffee_lib as lib; #[derive(Clone, Debug)] pub enum CoffeeOperation { - /// Install(plugin name, verbose run, dynamic installation) - Install(String, bool, bool), + /// Install(plugin name, branch, verbose run, dynamic installation) + Install(String, Option, bool, bool), /// List List, // Upgrade(name of the repository, verbose run) diff --git a/coffee_github/src/lib.rs b/coffee_github/src/lib.rs index 987ca5e..b312f8c 100644 --- a/coffee_github/src/lib.rs +++ b/coffee_github/src/lib.rs @@ -1,7 +1,7 @@ //! Github repository implementation pub mod repository; -mod utils; +pub mod utils; #[cfg(test)] mod tests { diff --git a/coffee_github/src/utils.rs b/coffee_github/src/utils.rs index 269354a..d578e74 100644 --- a/coffee_github/src/utils.rs +++ b/coffee_github/src/utils.rs @@ -1,8 +1,10 @@ +use log::debug; +use tokio::process::Command; + use coffee_lib::errors::CoffeeError; use coffee_lib::macros::error; use coffee_lib::url::URL; use coffee_lib::{commit_id, get_repo_info, sh}; -use log::debug; use coffee_lib::types::response::UpgradeStatus; @@ -30,8 +32,6 @@ pub async fn git_upgrade( branch: &str, verbose: bool, ) -> Result { - use tokio::process::Command; - let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?; let (local_commit, _) = get_repo_info!(repo); @@ -48,3 +48,19 @@ pub async fn git_upgrade( Ok(UpgradeStatus::Updated(upstream_commit, date)) } } + +pub async fn git_checkout( + path: &str, + branch: &str, + verbose: bool, +) -> Result<(String, String), CoffeeError> { + let mut cmd = format!("git fetch origin\n"); + cmd += &format!("git reset --hard\n"); + cmd += &format!("git checkout origin/{branch}"); + sh!(path, cmd, verbose); + + let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?; + let (upstream_commit, date) = get_repo_info!(repo); + + Ok((upstream_commit, date)) +} diff --git a/coffee_httpd/src/httpd/server.rs b/coffee_httpd/src/httpd/server.rs index dc429fd..320e11a 100644 --- a/coffee_httpd/src/httpd/server.rs +++ b/coffee_httpd/src/httpd/server.rs @@ -93,7 +93,7 @@ async fn coffee_install( let try_dynamic = body.try_dynamic; let mut coffee = data.coffee.lock().await; - let result = coffee.install(plugin, false, try_dynamic).await; + let result = coffee.install(plugin, None, false, try_dynamic).await; handle_httpd_response!(result, "Plugin '{plugin}' installed successfully") } diff --git a/coffee_lib/src/plugin_manager.rs b/coffee_lib/src/plugin_manager.rs index 103b77e..38f4a4e 100644 --- a/coffee_lib/src/plugin_manager.rs +++ b/coffee_lib/src/plugin_manager.rs @@ -14,6 +14,7 @@ pub trait PluginManager { async fn install( &mut self, plugins: &str, + branch: Option, verbose: bool, try_dynamic: bool, ) -> Result<(), CoffeeError>; diff --git a/coffee_plugin/src/plugin/plugin_mod.rs b/coffee_plugin/src/plugin/plugin_mod.rs index 4f3fa40..22a4902 100644 --- a/coffee_plugin/src/plugin/plugin_mod.rs +++ b/coffee_plugin/src/plugin/plugin_mod.rs @@ -91,7 +91,7 @@ fn coffee_install(plugin: &mut Plugin, request: Value) -> Result