1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
use serde::{
Deserialize,
Serialize,
};
use clap::Parser;
use tokio::fs::read_to_string;
use std::{
fs::canonicalize,
io::Read,
process::{
Child,
Command,
Stdio
},
path::PathBuf,
net::IpAddr,
};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
#[arg(short, long, value_name = "ADDR")]
address: Option<IpAddr>,
#[arg(short, long, value_name = "PORT")]
port: Option<u16>,
}
#[derive(Serialize, Deserialize)]
struct Service {
name: String,
command: String,
args: String,
directory: Option<PathBuf>,
}
impl Service {
fn new() -> Self {
Self {
name: String::new(),
command: String::new(),
args: String::new(),
directory: None,
}
}
}
#[derive(Serialize, Deserialize)]
struct Config {
address: Option<IpAddr>,
port: Option<u16>,
service: Vec<Service>,
}
impl Config {
fn new() -> Self {
Self {
address: None,
port: None,
service: Vec::new(),
}
}
}
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)
.current_dir(canonicalize(cwd)?)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
Ok(child)
} else {
let child = Command::new(image)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
Ok(child)
}
}
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)
}
#[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()?;
Ok(())
}
|