From a2d4646eb501de94df9f8711fc2dc9e0818ec654 Mon Sep 17 00:00:00 2001 From: Alex <69764315+Serial-ATA@users.noreply.github.com> Date: Thu, 17 Oct 2024 19:18:59 -0400 Subject: [PATCH] feat(cli): support custom repo and path sources (#360) --- cli/src/create.rs | 88 +++++++++++++++++++++++++++++++++++++++-------- cli/src/main.rs | 8 +++-- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/cli/src/create.rs b/cli/src/create.rs index 8b8276d27..309def0ef 100644 --- a/cli/src/create.rs +++ b/cli/src/create.rs @@ -1,18 +1,78 @@ -pub fn new_blueprint(name: &str) { +use clap::Args; +use std::path::PathBuf; + +#[derive(Args, Debug, Clone, Default)] +#[group(id = "source", required = false, multiple = false)] +pub struct Source { + #[command(flatten)] + repo: Option, + + #[arg(short, long, group = "source")] + path: Option, +} + +#[derive(Args, Debug, Clone)] +#[group(requires = "repo")] +pub struct RepoArgs { + #[arg(short, long, required = false, group = "source")] + repo: String, + #[arg(short, long)] + branch: Option, + #[arg(short, long, conflicts_with = "branch")] + tag: Option, +} + +impl From for Option { + fn from(value: Source) -> Self { + let mut template_path = cargo_generate::TemplatePath::default(); + + match value { + Source { + repo: Some(repo_args), + .. + } => { + template_path.git = Some(repo_args.repo); + template_path.branch = repo_args.branch; + template_path.tag = repo_args.tag; + Some(template_path) + } + Source { + path: Some(path), .. + } => { + template_path.path = Some(path.to_string_lossy().into()); + Some(template_path) + } + Source { + repo: None, + path: None, + } => None, + } + } +} + +pub fn new_blueprint(name: String, source: Option) { + println!("Generating blueprint with name: {}", name); + + let source = source.unwrap_or_default(); + let template_path_opt: Option = source.into(); + + let template_path; + match template_path_opt { + Some(tp) => template_path = tp, + None => { + // TODO: Interactive selection (#352) + template_path = cargo_generate::TemplatePath { + git: Some(String::from( + "https://github.com/tangle-network/blueprint-template/", + )), + branch: Some(String::from("main")), + ..Default::default() + }; + } + } + let path = cargo_generate::generate(cargo_generate::GenerateArgs { - template_path: cargo_generate::TemplatePath { - auto_path: None, - subfolder: None, - test: false, - git: Some(String::from( - "https://github.com/tangle-network/blueprint-template/", - )), - branch: Some(String::from("main")), - tag: None, - revision: None, - path: None, - favorite: None, - }, + template_path, list_favorites: false, name: Some(name.to_string()), force: false, diff --git a/cli/src/main.rs b/cli/src/main.rs index 59a8ae16a..a6f06689b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -36,6 +36,9 @@ enum GadgetCommands { /// The name of the blueprint #[arg(short, long)] name: String, + + #[command(flatten)] + source: Option, }, /// Deploy a blueprint to the Tangle Network. @@ -71,9 +74,8 @@ async fn main() -> color_eyre::Result<()> { match cli.command { Commands::Gadget { subcommand } => match subcommand { - GadgetCommands::Create { name } => { - println!("Generating blueprint with name: {}", name); - create::new_blueprint(&name); + GadgetCommands::Create { name, source } => { + create::new_blueprint(name, source); } GadgetCommands::Deploy { rpc_url, package } => { let manifest_path = cli