use crate::targets::{self, CommandSpec, SshTarget};
#[derive(Debug, Clone)]
pub(super) enum CacheHost {
Local,
Ssh(SshTarget),
}
impl CacheHost {
pub(super) fn for_target(target: &targets::TargetTemplate) -> Option<Self> {
match target {
targets::TargetTemplate::LocalPodman(_)
| targets::TargetTemplate::LocalDocker(_)
| targets::TargetTemplate::AppleContainer(_) => Some(Self::Local),
targets::TargetTemplate::SshPodman { ssh, .. }
| targets::TargetTemplate::SshDocker { ssh, .. } => Some(Self::Ssh(ssh.clone())),
targets::TargetTemplate::LocalBare
| targets::TargetTemplate::AwsEc2(_)
| targets::TargetTemplate::SshBare { .. } => None,
}
}
pub(super) fn for_machine(machine: &mj_core::config::Machine) -> Option<Self> {
match machine {
mj_core::config::Machine::Local { .. } => Some(Self::Local),
mj_core::config::Machine::Ssh { ssh, .. } => Some(Self::Ssh(SshTarget::from(ssh))),
mj_core::config::Machine::AwsEc2 { .. } => None,
}
}
pub(super) fn ssh(&self) -> Option<&SshTarget> {
match self {
Self::Local => None,
Self::Ssh(ssh) => Some(ssh),
}
}
pub(super) fn key(&self) -> String {
match self {
Self::Local => "local".to_owned(),
Self::Ssh(ssh) => format!("ssh:{}", ssh.destination),
}
}
pub(super) fn command(&self, remote: Vec<String>, purpose: impl Into<String>) -> CommandSpec {
let command = match self {
Self::Local => CommandSpec::new(remote[0].clone(), remote[1..].iter().cloned()),
Self::Ssh(ssh) => {
let mut args = ssh.ssh_args.clone();
targets::push_connection_sharing_args(&mut args);
args.push(ssh.destination.clone());
args.push(targets::join_remote_command(&remote));
CommandSpec::new("ssh", args).ssh_destination(ssh.destination.clone())
}
};
command.purpose(purpose)
}
pub(super) fn shell_command(
&self,
script: &str,
label: &str,
arguments: impl IntoIterator<Item = String>,
purpose: impl Into<String>,
) -> CommandSpec {
let mut remote = vec![
"sh".to_owned(),
"-c".to_owned(),
script.to_owned(),
label.to_owned(),
];
remote.extend(arguments);
self.command(remote, purpose)
}
}