From 1a3743dbd8a8c2effe5e2754a655f850dfee713d Mon Sep 17 00:00:00 2001 From: William Villeneuve Date: Tue, 26 Sep 2023 16:20:29 -0400 Subject: [PATCH] changed nk link to expect the path to the plugin.yml file directly, added support for passing multiple plugins to nk link --- src/args.rs | 13 +++++++--- src/commands/link.rs | 59 ++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/args.rs b/src/args.rs index 2a37b36..90cc848 100644 --- a/src/args.rs +++ b/src/args.rs @@ -35,6 +35,7 @@ pub enum Commands { Resolve(ResolveArgs), /// Link plugin at path + #[command(after_long_help = LINK_HELP.as_str())] Link(LinkArgs), /// Scripting language plugin helpers @@ -81,6 +82,12 @@ lazy_static! { )) .collect::() ); + static ref LINK_HELP: String = format!( + "{}\n{}\n{}", + style("Examples:").underlined().bold(), + " $ nk link ./plugin.yml", + " $ nk link ~/Projects/nk-plugins/*/plugin.yml" + ); } #[derive(Debug, Args)] @@ -117,9 +124,9 @@ pub struct ResolveArgs { #[derive(Debug, Args)] pub struct LinkArgs { - /// path do a plugin directory (containing a plugin.yml) - #[arg(value_name = "path")] - pub path: PathBuf, + /// path to a plugin.yml file + #[arg(value_name = "path", required = true)] + pub paths: Vec, } #[derive(Debug, Clone, ValueEnum)] diff --git a/src/commands/link.rs b/src/commands/link.rs index 46d2836..b69c582 100644 --- a/src/commands/link.rs +++ b/src/commands/link.rs @@ -9,39 +9,38 @@ use std::{ }; pub fn link(args: &LinkArgs) -> Result<(), Box> { - // resolve path - let canonical_path = match args.path.canonicalize() { - Ok(p) => Ok(p), - Err(e) => Err(format!("{}: {}", e, args.path.display())), - }?; - - // load plugin info - let definition = { - let plugin_yml = args.path.join("plugin.yml"); - match PluginDefinition::from_yaml_file(&plugin_yml) { + for path in &args.paths { + // load plugin info + let definition = match PluginDefinition::from_yaml_file(path) { Ok(val) => Ok(val), - Err(e) => Err(format!("{}: {}", e, plugin_yml.display())), - }? - }; - - let plugin_dir = PathBuf::from_str(&shellexpand::tilde( - format!("~/.nk/plugins/{}", definition.name).as_str(), - ))?; - - // delete existing plugin dir - if plugin_dir.try_exists()? { - fs::remove_dir_all(plugin_dir.clone())?; + Err(e) => Err(format!("{}: {}", e, path.display())), + }?; + + // create nk plugin directory + let nk_plugins_dir = + PathBuf::from_str(&shellexpand::tilde("~/.nk/plugins"))?; + fs::create_dir_all(&nk_plugins_dir)?; + + // delete existing plugin dir + let plugin_dir = nk_plugins_dir.join(definition.name); + if plugin_dir.try_exists()? { + fs::remove_dir_all(&plugin_dir)?; + } + + // resolve path to plugin.yml + let canonical_path = match path.canonicalize() { + Ok(p) => Ok(p), + Err(e) => Err(format!("{}: {}", e, path.display())), + }?; + // get parent of plugin.yml + let canonical_parent = canonical_path + .parent() + .ok_or("could not determine plugin parent")?; + + // link plugin + symlink_dir(canonical_parent, plugin_dir)?; } - // make parent dir - let parent_dir = plugin_dir - .parent() - .ok_or("plugin destination parent dir could not be determined")?; - fs::create_dir_all(parent_dir)?; - - // link plugin - symlink_dir(canonical_path, plugin_dir)?; - Ok(()) }