(
paths: Option<&SpacetimePaths>,
_argv0: Option<&OsStr>,
args: Vec<OsString>,
)
| 8 | use std::process::ExitCode; |
| 9 | |
| 10 | pub(crate) fn run_cli( |
| 11 | paths: Option<&SpacetimePaths>, |
| 12 | _argv0: Option<&OsStr>, |
| 13 | args: Vec<OsString>, |
| 14 | ) -> anyhow::Result<ExitCode> { |
| 15 | let parse_args = || PartialCliArgs::parse(&args); |
| 16 | let mut is_version_subcommand = None; |
| 17 | let paths_; |
| 18 | let paths = match paths { |
| 19 | Some(paths) => paths, |
| 20 | None => { |
| 21 | let partial_args = parse_args()?; |
| 22 | is_version_subcommand = Some(partial_args.is_version_subcommand()); |
| 23 | paths_ = match partial_args.root_dir { |
| 24 | Some(root_dir) => SpacetimePaths::from_root_dir(&root_dir), |
| 25 | None => SpacetimePaths::platform_defaults()?, |
| 26 | }; |
| 27 | &paths_ |
| 28 | } |
| 29 | }; |
| 30 | let cli_path = if let Some(artifact_dir) = running_from_target_dir() { |
| 31 | let cli_path = spacetimedb_paths::cli::VersionBinDir::from_path_unchecked(artifact_dir).spacetimedb_cli(); |
| 32 | anyhow::ensure!( |
| 33 | cli_path.0.exists(), |
| 34 | "running spacetimedb-update's cli proxy from a target/ directory, but the |
| 35 | spacetimedb-cli binary doesn't exist. try running `cargo build -p spacetimedb-cli`" |
| 36 | ); |
| 37 | cli_path |
| 38 | } else { |
| 39 | paths.cli_bin_dir.current_version_dir().spacetimedb_cli() |
| 40 | }; |
| 41 | |
| 42 | // Check for updates before exec'ing the CLI. On Unix, exec replaces |
| 43 | // the process so this is our only chance to print a notice. |
| 44 | crate::update_notice::maybe_print_update_notice(paths.cli_config_dir.as_ref()); |
| 45 | |
| 46 | let mut cmd = Command::new(&cli_path); |
| 47 | cmd.args(&args); |
| 48 | #[cfg(unix)] |
| 49 | { |
| 50 | use std::os::unix::process::CommandExt; |
| 51 | if let Some(_argv0) = _argv0 { |
| 52 | cmd.arg0(_argv0); |
| 53 | } |
| 54 | }; |
| 55 | let exec_result = exec_replace(&mut cmd).with_context(|| format!("exec failed for {}", cli_path.display())); |
| 56 | let exec_err = match exec_result { |
| 57 | Ok(status) => return Ok(status), |
| 58 | Err(err) => err, |
| 59 | }; |
| 60 | // if we failed to exec cli and it seems like the user is trying to run `spacetime version`, |
| 61 | // patch them through directly. |
| 62 | if is_version_subcommand.unwrap_or_else(|| parse_args().is_ok_and(|a| a.is_version_subcommand())) { |
| 63 | return crate::spacetimedb_update_main(); |
| 64 | } |
| 65 | Err(exec_err) |
| 66 | .context(format!("exec failed for {}", cli_path.display())) |
| 67 | .context( |
no test coverage detected
searching dependent graphs…