Skip to content

Commit

Permalink
feat(cli): support custom repo and path sources (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA authored Oct 17, 2024
1 parent ce13646 commit a2d4646
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
88 changes: 74 additions & 14 deletions cli/src/create.rs
Original file line number Diff line number Diff line change
@@ -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<RepoArgs>,

#[arg(short, long, group = "source")]
path: Option<PathBuf>,
}

#[derive(Args, Debug, Clone)]
#[group(requires = "repo")]
pub struct RepoArgs {
#[arg(short, long, required = false, group = "source")]
repo: String,
#[arg(short, long)]
branch: Option<String>,
#[arg(short, long, conflicts_with = "branch")]
tag: Option<String>,
}

impl From<Source> for Option<cargo_generate::TemplatePath> {
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<Source>) {
println!("Generating blueprint with name: {}", name);

let source = source.unwrap_or_default();
let template_path_opt: Option<cargo_generate::TemplatePath> = 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,
Expand Down
8 changes: 5 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ enum GadgetCommands {
/// The name of the blueprint
#[arg(short, long)]
name: String,

#[command(flatten)]
source: Option<create::Source>,
},

/// Deploy a blueprint to the Tangle Network.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a2d4646

Please sign in to comment.