Skip to content

Commit

Permalink
changed nk link to expect the path to the plugin.yml file directly, a…
Browse files Browse the repository at this point in the history
…dded support for passing multiple plugins to nk link
  • Loading branch information
ciiqr committed Sep 26, 2023
1 parent f1d2013 commit 1a3743d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
13 changes: 10 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,6 +82,12 @@ lazy_static! {
))
.collect::<String>()
);
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)]
Expand Down Expand Up @@ -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<PathBuf>,
}

#[derive(Debug, Clone, ValueEnum)]
Expand Down
59 changes: 29 additions & 30 deletions src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,38 @@ use std::{
};

pub fn link(args: &LinkArgs) -> Result<(), Box<dyn std::error::Error>> {
// 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(())
}

Expand Down

0 comments on commit 1a3743d

Please sign in to comment.