use std::path::Path;
use std::process::{Command, Stdio};
use crate::error::{Error, Result};
#[derive(Debug, Clone)]
pub struct Options {
pub url: String,
pub token: String,
pub name: String,
pub replace: bool,
pub labels: Option<String>,
pub ephemeral: bool,
}
#[must_use]
pub fn config_args(options: &Options) -> Vec<String> {
let mut args = vec![
"--url".to_owned(),
options.url.clone(),
"--token".to_owned(),
options.token.clone(),
"--name".to_owned(),
options.name.clone(),
"--unattended".to_owned(),
];
if options.replace {
args.push("--replace".to_owned());
}
if options.ephemeral {
args.push("--ephemeral".to_owned());
}
if let Some(labels) = &options.labels {
args.push("--labels".to_owned());
args.push(labels.clone());
}
args
}
#[must_use]
pub fn remove_args(token: &str) -> Vec<String> {
vec!["remove".to_owned(), "--token".to_owned(), token.to_owned()]
}
pub fn configure(dir: &Path, options: &Options) -> Result<()> {
let mut command = script_command();
command.args(config_args(options));
run_script(dir, "configuration", command)
}
pub fn remove(dir: &Path, token: &str) -> Result<()> {
let mut command = script_command();
command.args(remove_args(token));
run_script(dir, "removal", command)
}
fn script_command() -> Command {
if cfg!(windows) {
let mut command = Command::new("cmd");
command.arg("/C").arg("config.cmd");
command
} else {
Command::new("./config.sh")
}
}
fn run_script(dir: &Path, action: &str, mut command: Command) -> Result<()> {
command
.current_dir(dir)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
let status = command.status()?;
if !status.success() {
return Err(Error::Command(format!(
"runner {action} failed with {status}"
)));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{Options, config_args, remove_args};
fn options() -> Options {
Options {
url: "https://github.com/acme/app".to_owned(),
token: "secret".to_owned(),
name: "actions-runner".to_owned(),
replace: false,
labels: None,
ephemeral: false,
}
}
#[test]
fn builds_minimal_arguments() {
assert_eq!(
config_args(&options()),
vec![
"--url",
"https://github.com/acme/app",
"--token",
"secret",
"--name",
"actions-runner",
"--unattended",
]
);
}
#[test]
fn adds_optional_arguments() {
let options = Options {
replace: true,
labels: Some("self-hosted,linux".to_owned()),
..options()
};
assert_eq!(
config_args(&options),
vec![
"--url",
"https://github.com/acme/app",
"--token",
"secret",
"--name",
"actions-runner",
"--unattended",
"--replace",
"--labels",
"self-hosted,linux",
]
);
}
#[test]
fn builds_remove_arguments() {
assert_eq!(remove_args("secret"), vec!["remove", "--token", "secret"]);
}
#[test]
fn adds_ephemeral_flag() {
let options = Options {
ephemeral: true,
..options()
};
assert!(config_args(&options).iter().any(|arg| arg == "--ephemeral"));
}
}