aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/populate/src/shell.rs
blob: 4093ed7f670ed45941a91ce681fe8aea8356006e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! populate shell: runs shell code on a target, reached like populate
//! reaches it.

use crate::cli::ShellOptions;
use crate::run::{self, Origin};
use std::os::unix::process::CommandExt;
use std::process::{Command, ExitCode};

/// Runs the script as the options say, replacing populate, so the script's
/// exit status, terminal, and signals are populate's.  Returns only if
/// that fails.
pub fn shell(options: ShellOptions) -> ExitCode {
    let origin = Origin::from_env();
    let target = options
        .target
        .complete(&origin, options.sudo, options.ssh_args);
    let local = options.local.unwrap_or_else(|| origin.is_local(&target));
    if options.local_only && !local {
        let user = target
            .user
            .as_deref()
            .map(|user| format!("{user}@"))
            .unwrap_or_default();
        eprintln!(
            "populate: shell: {user}{} is not the machine populate runs on",
            target.host
        );
        return ExitCode::FAILURE;
    }
    let mut words: Vec<String> = Vec::new();
    if target.sudo {
        words.push("sudo".to_string());
    }
    words.extend([
        run::SH.to_string(),
        "-c".to_string(),
        options.script,
        "sh".to_string(),
        if local { "local" } else { "remote" }.to_string(),
    ]);
    let mut command = if local {
        let mut command = Command::new(&words[0]);
        command.args(&words[1..]);
        command
    } else {
        let mut command = Command::new("ssh");
        if let Some(user) = &target.user {
            command.args(["-l", user]);
        }
        if let Some(port) = &target.port {
            command.args(["-p", port]);
        }
        command
            .arg(if options.tty { "-t" } else { "-T" })
            .args(&target.extra_options)
            .arg(&target.host)
            .arg(
                words
                    .iter()
                    .map(|word| run::quote(word))
                    .collect::<Vec<_>>()
                    .join(" "),
            );
        command
    };
    let error = command.exec();
    eprintln!(
        "populate: shell: can't run {}: {error}",
        command.get_program().to_string_lossy()
    );
    ExitCode::from(127)
}