Rust · 1426 bytes Raw Blame History
1 mod config;
2 mod daemon;
3 mod dbus;
4 mod lock;
5 mod logging;
6 mod runtime;
7
8 use std::env;
9
10 use garwarp_ipc::PROTOCOL_VERSION;
11
12 fn main() {
13 let command = parse_command(env::args().nth(1).as_deref());
14 let result = match command {
15 Command::Daemon => daemon::run(),
16 Command::Version => {
17 println!("garwarp protocol v{PROTOCOL_VERSION}");
18 Ok(())
19 }
20 Command::Help => {
21 print_help();
22 Ok(())
23 }
24 };
25
26 if let Err(error) = result {
27 eprintln!("garwarp error: {error}");
28 std::process::exit(1);
29 }
30 }
31
32 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
33 enum Command {
34 Daemon,
35 Version,
36 Help,
37 }
38
39 fn parse_command(input: Option<&str>) -> Command {
40 match input {
41 Some("daemon") | None => Command::Daemon,
42 Some("version") | Some("--version") | Some("-V") => Command::Version,
43 Some("help") | Some("--help") | Some("-h") => Command::Help,
44 Some(_) => Command::Help,
45 }
46 }
47
48 fn print_help() {
49 println!("garwarp <command>");
50 println!("commands: daemon (default), version, help");
51 }
52
53 #[cfg(test)]
54 mod tests {
55 use super::{Command, parse_command};
56
57 #[test]
58 fn daemon_is_default_command() {
59 assert_eq!(parse_command(None), Command::Daemon);
60 }
61
62 #[test]
63 fn help_for_unknown_command() {
64 assert_eq!(parse_command(Some("bogus")), Command::Help);
65 }
66 }
67