Skip to content

Commit 88f2241

Browse files
authored
chore: extract logic for defaulting to run into helper (#10011)
### Description Refactor to pull the logic for getting a `Command` from `cli_args` into a named helper. The `get_command` helper further uses a helper that makes it clear that `Command::Run` is the default if no command is specified when Turborepo is invoked. There's still some future work to do based on the comment here: https://github.com/vercel/turborepo/blob/b6a97bdc947f0b4855a1c9051df7eb0295cae39e/crates/turborepo-lib/src/cli/mod.rs#L1250 to make `cli_args` to be non-mutable.
1 parent ec03f1d commit 88f2241

File tree

1 file changed

+32
-24
lines changed
  • crates/turborepo-lib/src/cli

1 file changed

+32
-24
lines changed

crates/turborepo-lib/src/cli/mod.rs

+32-24
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,37 @@ fn should_print_version() -> bool {
11911191
print_version_state == PrintVersionState::Enabled && ci_state == CIState::Outside
11921192
}
11931193

1194+
fn default_to_run_command(cli_args: &Args) -> Result<Command, Error> {
1195+
let run_args = cli_args.run_args.clone().unwrap_or_default();
1196+
let execution_args = cli_args
1197+
.execution_args
1198+
// We clone instead of take as take would leave the command base a copy of cli_args
1199+
// missing any execution args.
1200+
.clone()
1201+
.ok_or_else(|| Error::NoCommand(Backtrace::capture()))?;
1202+
1203+
if execution_args.tasks.is_empty() {
1204+
let mut cmd = <Args as CommandFactory>::command();
1205+
let _ = cmd.print_help();
1206+
process::exit(1);
1207+
}
1208+
1209+
Ok(Command::Run {
1210+
run_args: Box::new(run_args),
1211+
execution_args: Box::new(execution_args),
1212+
})
1213+
}
1214+
1215+
fn get_command(cli_args: &mut Args) -> Result<Command, Error> {
1216+
if let Some(command) = mem::take(&mut cli_args.command) {
1217+
Ok(command)
1218+
} else {
1219+
// If there is no command, we set the command to `Command::Run` with
1220+
// `self.parsed_args.run_args` as arguments.
1221+
default_to_run_command(cli_args)
1222+
}
1223+
}
1224+
11941225
/// Runs the CLI by parsing arguments with clap, then either calling Rust code
11951226
/// directly or returning a payload for the Go code to use.
11961227
///
@@ -1227,30 +1258,7 @@ pub async fn run(
12271258
eprintln!("{}\n", GREY.apply_to(format!("turbo {}", get_version())));
12281259
}
12291260

1230-
// If there is no command, we set the command to `Command::Run` with
1231-
// `self.parsed_args.run_args` as arguments.
1232-
let mut command = if let Some(command) = mem::take(&mut cli_args.command) {
1233-
command
1234-
} else {
1235-
let run_args = cli_args.run_args.clone().unwrap_or_default();
1236-
let execution_args = cli_args
1237-
.execution_args
1238-
// We clone instead of take as take would leave the command base a copy of cli_args
1239-
// missing any execution args.
1240-
.clone()
1241-
.ok_or_else(|| Error::NoCommand(Backtrace::capture()))?;
1242-
1243-
if execution_args.tasks.is_empty() {
1244-
let mut cmd = <Args as CommandFactory>::command();
1245-
let _ = cmd.print_help();
1246-
process::exit(1);
1247-
}
1248-
1249-
Command::Run {
1250-
run_args: Box::new(run_args),
1251-
execution_args: Box::new(execution_args),
1252-
}
1253-
};
1261+
let mut command = get_command(&mut cli_args)?;
12541262

12551263
// Set some run flags if we have the data and are executing a Run
12561264
match &mut command {

0 commit comments

Comments
 (0)