Skip to content

Commit

Permalink
Add --help and --version command line arguments (closes #365)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Apr 29, 2024
1 parent 58b8c5b commit 592ca3c
Showing 1 changed file with 63 additions and 11 deletions.
74 changes: 63 additions & 11 deletions crates/common/src/manager/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ pub struct BootManager {
pub guards: Option<Vec<WorkerGuard>>,
}

const HELP: &str = r#"Stalwart Mail Server
Usage: stalwart-mail [OPTIONS]
Options:
-c, --config <PATH> Start server with the specified configuration file
-b, --backup <PATH> Backup all data to a specific path
-r, --restore <PATH> Restore all data from a specific path
-i, --init <PATH> Initialize a new server at a specific path
-h, --help Print help
-V, --version Print version
"#;

impl BootManager {
pub async fn init() -> Self {
let mut config_path = std::env::var("CONFIG_PATH").ok();
Expand All @@ -64,31 +77,70 @@ impl BootManager {
.and_then(|arg| arg.strip_prefix("--").map(|arg| arg.to_string()))
{
let (key, value) = if let Some((key, value)) = arg.split_once('=') {
(key.to_string(), value.trim().to_string())
} else if let Some(value) = args.next() {
(arg, value)
(key.to_string(), Some(value.trim().to_string()))
} else {
failed(&format!("Invalid command line argument: {arg}"));
(arg, args.next())
};

match key.as_str() {
"config" => {
config_path = Some(value);
"help" | "h" => {
println!("{HELP}");
std::process::exit(0);
}
"version" | "V" => {
println!("{}", env!("CARGO_PKG_VERSION"));
std::process::exit(0);
}
_ => (),
}

match key.as_str() {
"config" | "c" => {
config_path = Some(value.unwrap_or_else(|| {
failed(&format!(
"Missing value for argument '{key}', try '--help'."
))
}));
}
"init" => {
quickstart(value);
"init" | "i" => {
quickstart(value.unwrap_or_else(|| {
failed(&format!(
"Missing value for argument '{key}', try '--help'."
))
}));
std::process::exit(0);
}

"backup" | "b" => {
let path = value.unwrap_or_else(|| {
failed(&format!(
"Missing value for argument '{key}', try '--help'."
))
});
std::process::exit(0);
}
"restore" | "r" => {
let path = value.unwrap_or_else(|| {
failed(&format!(
"Missing value for argument '{key}', try '--help'."
))
});
std::process::exit(0);
}
_ => {
failed(&format!("Invalid command line argument: {key}"));
failed(&format!("Unrecognized command '{key}', try '--help'."));
}
}
}

if config_path.is_none() {
println!("{HELP}");
std::process::exit(0);
}
}

// Read main configuration file
let cfg_local_path =
PathBuf::from(config_path.failed("Missing parameter --config=<path-to-config>."));
let cfg_local_path = PathBuf::from(config_path.unwrap());
let mut config = Config::default();
match std::fs::read_to_string(&cfg_local_path) {
Ok(value) => {
Expand Down

0 comments on commit 592ca3c

Please sign in to comment.