chore(cli): optimized args handling for init & new (#2418)

This commit is contained in:
Andrew Voynov 2024-05-15 21:29:50 +03:00 committed by GitHub
parent 16313894da
commit e69c69abd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 17 deletions

View file

@ -28,24 +28,20 @@ pub struct Create {
impl Create {
pub fn create(self) -> Result<()> {
let mut args = GenerateArgs {
let args = GenerateArgs {
define: self.option,
name: self.name,
silent: self.yes,
template_path: TemplatePath {
auto_path: Some(self.template),
subfolder: self.subtemplate,
..Default::default()
},
define: self.option,
..Default::default()
};
if self.yes {
if self.name.is_none() {
return Err(
"You have to provide the project's name when using `--yes` option.".into(),
);
}
args.silent = true;
if self.yes && args.name.is_none() {
return Err("You have to provide the project's name when using `--yes` option.".into());
}
args.name = self.name;
let path = cargo_generate::generate(args)?;
post_create(&path)
}

View file

@ -29,20 +29,18 @@ impl Init {
let name = std::env::current_dir()?
.file_name()
.map(|f| f.to_str().unwrap().to_string());
let mut args = GenerateArgs {
let args = GenerateArgs {
define: self.option,
init: true,
name,
silent: self.yes,
template_path: TemplatePath {
auto_path: Some(self.template),
subfolder: self.subtemplate,
..Default::default()
},
name,
init: true,
define: self.option,
..Default::default()
};
if self.yes {
args.silent = true;
}
let path = cargo_generate::generate(args)?;
create::post_create(&path)
}