Skip to content

Commit 014c8ce

Browse files
committed
refactor(cli): Simplify script logic
1 parent 3e56c4e commit 014c8ce

File tree

3 files changed

+42
-53
lines changed

3 files changed

+42
-53
lines changed

src/bin/cargo/cli.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ To pass the arguments to the subcommand, remove `--`",
337337
}
338338
}
339339
if commands::run::is_manifest_command(cmd) {
340-
{
341-
return Ok((args, GlobalArgs::default()));
342-
}
340+
return Ok((args, GlobalArgs::default()));
343341
}
344342

345343
let mut alias = alias
@@ -476,15 +474,12 @@ impl Exec {
476474
match self {
477475
Self::Builtin(exec) => exec(gctx, subcommand_args),
478476
Self::Manifest(cmd) => {
479-
// Placeholder
480-
{
481-
let ext_args: Vec<OsString> = subcommand_args
482-
.get_many::<OsString>("")
483-
.unwrap_or_default()
484-
.cloned()
485-
.collect();
486-
commands::run::exec_manifest_command(gctx, &cmd, &ext_args)
487-
}
477+
let ext_args: Vec<OsString> = subcommand_args
478+
.get_many::<OsString>("")
479+
.unwrap_or_default()
480+
.cloned()
481+
.collect();
482+
commands::run::exec_manifest_command(gctx, &cmd, &ext_args)
488483
}
489484
Self::External(cmd) => {
490485
let mut ext_args = vec![OsStr::new(&cmd)];

src/bin/cargo/commands/run.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -96,45 +96,42 @@ pub fn is_manifest_command(arg: &str) -> bool {
9696

9797
pub fn exec_manifest_command(gctx: &mut GlobalContext, cmd: &str, args: &[OsString]) -> CliResult {
9898
let manifest_path = Path::new(cmd);
99-
match manifest_path.is_file() {
100-
true => {}
101-
false => {
102-
let possible_commands = crate::list_commands(gctx);
103-
let is_dir = if manifest_path.is_dir() {
104-
format!(": `{cmd}` is a directory")
105-
} else {
99+
if !manifest_path.is_file() {
100+
let possible_commands = crate::list_commands(gctx);
101+
let is_dir = if manifest_path.is_dir() {
102+
format!(": `{cmd}` is a directory")
103+
} else {
104+
"".to_owned()
105+
};
106+
let suggested_command = if let Some(suggested_command) = possible_commands
107+
.keys()
108+
.filter(|c| cmd.starts_with(c.as_str()))
109+
.max_by_key(|c| c.len())
110+
{
111+
let actual_args = cmd.strip_prefix(suggested_command).unwrap();
112+
let args = if args.is_empty() {
106113
"".to_owned()
107-
};
108-
let suggested_command = if let Some(suggested_command) = possible_commands
109-
.keys()
110-
.filter(|c| cmd.starts_with(c.as_str()))
111-
.max_by_key(|c| c.len())
112-
{
113-
let actual_args = cmd.strip_prefix(suggested_command).unwrap();
114-
let args = if args.is_empty() {
115-
"".to_owned()
116-
} else {
117-
format!(
118-
" {}",
119-
args.into_iter().map(|os| os.to_string_lossy()).join(" ")
120-
)
121-
};
114+
} else {
122115
format!(
123-
"\nhelp: there is a command with a similar name: `{suggested_command} {actual_args}{args}`"
116+
" {}",
117+
args.into_iter().map(|os| os.to_string_lossy()).join(" ")
124118
)
125-
} else {
126-
"".to_owned()
127-
};
128-
let suggested_script = if let Some(suggested_script) = suggested_script(cmd) {
129-
format!("\nhelp: there is a script with a similar name: `{suggested_script}`")
130-
} else {
131-
"".to_owned()
132119
};
133-
return Err(anyhow::anyhow!(
134-
"no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}"
120+
format!(
121+
"\nhelp: there is a command with a similar name: `{suggested_command} {actual_args}{args}`"
135122
)
136-
.into());
137-
}
123+
} else {
124+
"".to_owned()
125+
};
126+
let suggested_script = if let Some(suggested_script) = suggested_script(cmd) {
127+
format!("\nhelp: there is a script with a similar name: `{suggested_script}`")
128+
} else {
129+
"".to_owned()
130+
};
131+
return Err(anyhow::anyhow!(
132+
"no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}"
133+
)
134+
.into());
138135
}
139136

140137
let manifest_path = root_manifest(Some(manifest_path), gctx)?;

src/cargo/util/command_prelude.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,13 +1097,10 @@ pub fn root_manifest(manifest_path: Option<&Path>, gctx: &GlobalContext) -> Carg
10971097
manifest_path.display()
10981098
)
10991099
} else if !path.ends_with("Cargo.toml") && !crate::util::toml::is_embedded(&path) {
1100-
// Placeholder
1101-
{
1102-
anyhow::bail!(
1103-
"the manifest-path must be a path to a Cargo.toml or script file: `{}`",
1104-
path.display()
1105-
)
1106-
}
1100+
anyhow::bail!(
1101+
"the manifest-path must be a path to a Cargo.toml or script file: `{}`",
1102+
path.display()
1103+
)
11071104
}
11081105
Ok(path)
11091106
} else {

0 commit comments

Comments
 (0)