-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.rs
More file actions
47 lines (44 loc) · 1.57 KB
/
Copy pathcommands.rs
File metadata and controls
47 lines (44 loc) · 1.57 KB
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
use crate::prelude::*;
use std::path::Path;
#[derive(Debug, Subcommand)]
pub enum ConfigCommands {
#[command(about = "Initialize config file with default settings")]
Init,
#[command(about = "Show currently applied configuration")]
Show,
#[command(about = "Show defaults")]
Defaults,
}
pub fn perform_command(commands: &ConfigCommands) {
match commands {
ConfigCommands::Init => {
if Path::new(&CONFIG_PATH).is_file() {
eprintln!("Config file is already initialized at '{CONFIG_PATH}'.");
eprintln!("If you want to reinitialize it with defaults, remove it:");
eprintln!("rm '{CONFIG_PATH}'");
} else {
let config = Config::default();
let config_str =
toml::to_string(&config).expect("Default config should be serializable");
if let Err(err) = std::fs::write(CONFIG_PATH, config_str) {
eprintln!("Error writing to file: {err}");
} else {
println!("Initialized config file '{CONFIG_PATH}'");
}
}
}
ConfigCommands::Show => {
println!(
"{}",
toml::to_string(&*CONFIG).expect("Loaded config should always be serializable")
);
}
ConfigCommands::Defaults => {
println!(
"{}",
toml::to_string(&Config::default())
.expect("Loaded config should always be serializable")
);
}
}
}