diff options
author | yuzu <yuzu@b9215c17-b818-4693-b096-d1e41a411fef> | 2025-07-08 07:12:59 +0000 |
---|---|---|
committer | yuzu <yuzu@b9215c17-b818-4693-b096-d1e41a411fef> | 2025-07-08 07:12:59 +0000 |
commit | 141e546e1d7a8385817b3edb46bd91b502563cd9 (patch) | |
tree | e215d8e4d4c79fee4b94e527cef8522b27ba5cec /src/bin/smd.rs | |
parent | 8f05a437d80a243e504b4fb5d26b53bbd7de9c47 (diff) | |
download | salaryman-141e546e1d7a8385817b3edb46bd91b502563cd9.tar.gz salaryman-141e546e1d7a8385817b3edb46bd91b502563cd9.tar.bz2 salaryman-141e546e1d7a8385817b3edb46bd91b502563cd9.zip |
working proof of concept -- minecraft
git-svn-id: svn+ssh://diminuette.aengel.lesbianunix.dev/salaryman/trunk@9 b9215c17-b818-4693-b096-d1e41a411fef
Diffstat (limited to 'src/bin/smd.rs')
-rw-r--r-- | src/bin/smd.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/bin/smd.rs b/src/bin/smd.rs new file mode 100644 index 0000000..5a8401a --- /dev/null +++ b/src/bin/smd.rs @@ -0,0 +1,92 @@ +use clap::Parser; +use serde::{Deserialize, Serialize}; + +use tokio::fs::read_to_string; + +use std::{net::IpAddr, path::PathBuf}; + +use salaryman::model::{Service, ServiceConf}; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg( + short, + long, + value_name = "FILE", + help = "config file override", + default_value = "salaryman.toml" + )] + config: PathBuf, + #[arg( + short, + long, + value_name = "ADDR", + help = "IP address to bind API to", + default_value = "127.0.0.1" + )] + address: IpAddr, + #[arg( + short, + long, + value_name = "PORT", + help = "TCP Port to bind API to", + default_value = "3080" + )] + port: u16, +} + +#[derive(Serialize, Deserialize, Debug)] +struct Config { + address: Option<IpAddr>, + port: Option<u16>, + service: Vec<ServiceConf>, +} +impl Config { + fn new() -> Self { + Self { + address: None, + port: None, + service: Vec::new(), + } + } +} + +async fn load_config(file: &PathBuf) -> Config { + let s: String = match read_to_string(file).await { + Ok(s) => s, + Err(_) => String::new(), + }; + match toml::from_str(s.as_str()) { + Ok(c) => c, + Err(_) => Config::new(), + } +} + +#[tokio::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + let args = Args::parse(); + let conf: Config = load_config(&args.config).await; + let mut services: Vec<Service> = Vec::new(); + for i in 0..conf.service.len() { + services.push(Service::from_conf(&conf.service[i])); + if conf.service[i].autostart { + services[i].start().await?; + services[i].scan_stdout().await?; + services[i].scan_stderr().await?; + } + } + tokio::time::sleep(std::time::Duration::from_secs(60)).await; + println!("trying to write to stdin!"); + for i in 0..services.len() { + services[i].write_stdin("stop\n".into()).await?; + } + tokio::time::sleep(std::time::Duration::from_secs(30)).await; + for mut service in services { + match service.stop().await { + Ok(_) => println!("lol it was killed"), + Err(_) => println!("it either didn't exist, or failed to kill"), + } + } + Ok(()) +} |