support working_directory and command string

This commit is contained in:
Ilion Beyst 2022-07-23 00:38:34 +02:00
parent 1cf20810c5
commit 500061375c
2 changed files with 25 additions and 3 deletions

View file

@ -14,6 +14,7 @@ serde = { version = "1.0", features = ["derive"] }
toml = "0.5" toml = "0.5"
planetwars-matchrunner = { path = "../planetwars-matchrunner" } planetwars-matchrunner = { path = "../planetwars-matchrunner" }
clap = { version = "3.2", features = ["derive", "env"]} clap = { version = "3.2", features = ["derive", "env"]}
shlex = "1.1"
[build-dependencies] [build-dependencies]
tonic-build = "0.7.2" tonic-build = "0.7.2"

View file

@ -32,7 +32,24 @@ struct PlayMatch {
struct BotConfig { struct BotConfig {
#[allow(dead_code)] #[allow(dead_code)]
name: String, name: String,
command: Vec<String>, command: Command,
working_directory: Option<String>,
}
#[derive(Deserialize)]
#[serde(untagged)]
enum Command {
String(String),
Argv(Vec<String>),
}
impl Command {
pub fn to_argv(&self) -> Vec<String> {
match self {
Command::Argv(vec) => vec.clone(),
Command::String(s) => shlex::split(s).expect("invalid command string"),
}
}
} }
#[tokio::main] #[tokio::main]
@ -76,8 +93,12 @@ async fn run_player(bot_config: BotConfig, player_key: String, channel: Channel)
}); });
let mut bot_process = Bot { let mut bot_process = Bot {
working_dir: PathBuf::from("."), working_dir: PathBuf::from(
argv: bot_config.command, bot_config
.working_directory
.unwrap_or_else(|| ".".to_string()),
),
argv: bot_config.command.to_argv(),
} }
.spawn_process(); .spawn_process();