use std::path::Path;
pub const NAME: &str = "openmoat";
const POWERSHELL_TAIL: &str = "; if (-not $?) { exit 2 }";
pub fn command(binary: &Path, id: &str) -> String {
command_for(&binary.to_string_lossy(), id, cfg!(windows))
}
fn command_for(binary: &str, id: &str, powershell: bool) -> String {
if powershell {
let binary = binary.replace('\'', "''");
format!("& '{binary}' guard --host {id}{POWERSHELL_TAIL}")
} else {
format!("'{}' guard --host {id}", binary.replace('\'', r"'\''"))
}
}
pub fn binary_of(command: &str, id: &str) -> Option<String> {
let suffix = format!(" guard --host {id}");
let command = command.trim();
let command = command.strip_suffix(POWERSHELL_TAIL).unwrap_or(command);
let quoted = command.strip_suffix(&suffix)?.trim();
let (quoted, powershell) = match quoted.strip_prefix("& ") {
Some(rest) => (rest.trim(), true),
None => (quoted, false),
};
let Some(inner) = quoted.strip_prefix('\'').and_then(|q| q.strip_suffix('\'')) else {
return Some(quoted.trim_matches('"').to_owned());
};
Some(if powershell {
inner.replace("''", "'")
} else {
inner.replace(r"'\''", "'")
})
}
#[cfg(test)]
mod tests;