about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoryuzu <yuzu@b9215c17-b818-4693-b096-d1e41a411fef>2025-06-03 16:48:06 +0000
committeryuzu <yuzu@b9215c17-b818-4693-b096-d1e41a411fef>2025-06-03 16:48:06 +0000
commit288842e0621eb3e07a0790ba328727939c480884 (patch)
treee879dda42d430bdc8991a9f92b376e0fc22607c5
parent0d58b22d4fecd19370b92e4a12803f08ade0dd32 (diff)
downloadsalaryman-288842e0621eb3e07a0790ba328727939c480884.tar.gz
salaryman-288842e0621eb3e07a0790ba328727939c480884.tar.bz2
salaryman-288842e0621eb3e07a0790ba328727939c480884.zip
boilerplate done
git-svn-id: svn+ssh://diminuette.aengel.lesbianunix.dev/salaryman/trunk@6 b9215c17-b818-4693-b096-d1e41a411fef
-rw-r--r--salaryman.toml15
-rw-r--r--src/main.rs74
2 files changed, 54 insertions, 35 deletions
diff --git a/salaryman.toml b/salaryman.toml
new file mode 100644
index 0000000..b09acd4
--- /dev/null
+++ b/salaryman.toml
@@ -0,0 +1,15 @@
+address = "0.0.0.0"
+port = 8080
+
+[[service]]
+name = "minecraft"
+command = "java"
+args = "-jar minecraft_server.jar"
+directory = "/minecraft/data"
+autostart = false
+
+[[service]]
+name = "derpcraft"
+command = "java"
+args = "-jar minecraft_server.jar"
+autostart = true
diff --git a/src/main.rs b/src/main.rs
index 82dc2f9..1804ca9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,21 +1,15 @@
-use serde::{
-    Deserialize,
-    Serialize,
-};
 use clap::Parser;
+use serde::{Deserialize, Serialize};
 
 use tokio::fs::read_to_string;
+use tokio::fs::write;
 
 use std::{
     fs::canonicalize,
     io::Read,
-    process::{
-        Child,
-        Command,
-        Stdio
-    },
+    net::{IpAddr, Ipv4Addr, Ipv6Addr},
     path::PathBuf,
-    net::IpAddr,
+    process::{Child, Command, Stdio},
 };
 
 #[derive(Parser)]
@@ -29,25 +23,27 @@ struct Args {
     port: Option<u16>,
 }
 
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, Debug)]
 struct Service {
     name: String,
     command: String,
-    args: String,
+    args: Option<String>,
     directory: Option<PathBuf>,
+    autostart: bool,
 }
 impl Service {
     fn new() -> Self {
         Self {
             name: String::new(),
             command: String::new(),
-            args: String::new(),
+            args: None,
             directory: None,
+            autostart: false,
         }
     }
 }
 
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, Debug)]
 struct Config {
     address: Option<IpAddr>,
     port: Option<u16>,
@@ -63,7 +59,11 @@ impl Config {
     }
 }
 
-fn exec(image: &str, args: Vec<&str>, dir: Option<PathBuf>) -> Result<Child, Box<dyn std::error::Error>> {
+fn exec(
+    image: &str,
+    args: Vec<&str>,
+    dir: Option<PathBuf>,
+) -> Result<Child, Box<dyn std::error::Error>> {
     if let Some(cwd) = dir {
         let child = Command::new(image)
             .args(args)
@@ -84,30 +84,34 @@ fn exec(image: &str, args: Vec<&str>, dir: Option<PathBuf>) -> Result<Child, Box
     }
 }
 
-async fn load_config(file: PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
-    let config: Config = toml::from_str(read_to_string(file).await?.as_str())?;
-    Ok(config)
+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 mut conf: Config = Config::new();
-    if let Some(file) = args.config.as_deref() {
-        conf = load_config(PathBuf::from(file)).await?;
-    } else {
-        conf = load_config(PathBuf::from("salaryman.toml")).await?;
-    }
-    
-    let mut child = exec("java", vec!["-jar", "minecraft_server.jar"], None)?;
-    std::thread::sleep(std::time::Duration::from_secs(60));
-    let mut buf: [u8; 512] = [0; 512];
-    let mut ebuf: [u8; 512] = [0; 512];
-    child.stdout.as_mut().unwrap().read(&mut buf[..])?;
-    child.stderr.as_mut().unwrap().read(&mut ebuf[..])?;
-    println!("{}", String::from_utf8_lossy(&buf));
-    println!("{}", String::from_utf8_lossy(&ebuf));
-    child.kill()?;
+    let conf: Config = load_config(PathBuf::from("salaryman.toml")).await;
+
+    println!("{conf:?}");
+
+    /*
+        let mut child = exec("java", vec!["-jar", "minecraft_server.jar"], None)?;
+        std::thread::sleep(std::time::Duration::from_secs(60));
+        let mut buf: [u8; 512] = [0; 512];
+        let mut ebuf: [u8; 512] = [0; 512];
+        child.stdout.as_mut().unwrap().read(&mut buf[..])?;
+        child.stderr.as_mut().unwrap().read(&mut ebuf[..])?;
+        println!("{}", String::from_utf8_lossy(&buf));
+        println!("{}", String::from_utf8_lossy(&ebuf));
+        child.kill()?;
+    */
     Ok(())
 }
-