Skip to main content

standard_plugin_cli/
install.rs

1//! `standard-plugin install`: checks and packs the plugin, then hands the
2//! package to `standard plugin install`, which uploads it with the
3//! signed-in account's credentials, shows its grants and asks before it
4//! installs. This CLI never holds account credentials itself.
5
6use std::path::Path;
7use std::process::Command;
8
9/// The command `install` runs.
10pub fn command(dir: &Path) -> Command {
11    let mut command =
12        Command::new(std::env::var_os("STANDARD_BIN").unwrap_or_else(|| "standard".into()));
13    command.arg("plugin").arg("install").arg(dir);
14    command
15}
16
17/// Runs `standard plugin install <dir>` in place of this process (so it
18/// can ask y/N on this terminal); returns only when it could not start.
19pub fn exec(dir: &Path) -> String {
20    let mut command = command(dir);
21    println!("running: standard plugin install {}", dir.display());
22    #[cfg(unix)]
23    {
24        use std::os::unix::process::CommandExt as _;
25        let error = command.exec();
26        format!("could not run `standard` ({error}); install Standard Code or set STANDARD_BIN")
27    }
28    #[cfg(not(unix))]
29    {
30        match command.status() {
31            Ok(status) if status.success() => String::new(),
32            Ok(status) => format!("standard plugin install exited with {status}"),
33            Err(error) => format!("could not run `standard` ({error})"),
34        }
35    }
36}