Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support custom repo and path sources #360

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading