aboutsummaryrefslogtreecommitdiff
path: root/apps/cassiopeia/src/bin/cass.rs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/cassiopeia/src/bin/cass.rs')
-rw-r--r--apps/cassiopeia/src/bin/cass.rs77
1 files changed, 31 insertions, 46 deletions
diff --git a/apps/cassiopeia/src/bin/cass.rs b/apps/cassiopeia/src/bin/cass.rs
index 8bceddc911a4..3fea64380623 100644
--- a/apps/cassiopeia/src/bin/cass.rs
+++ b/apps/cassiopeia/src/bin/cass.rs
@@ -1,4 +1,4 @@
-use cassiopeia::{meta, Cassiopeia};
+use cassiopeia::{error::ParseResult, meta, Cassiopeia};
use clap::{App, Arg, SubCommand};
fn main() {
@@ -69,64 +69,49 @@ If you want to report a bug, please do so on my mailing list: lists.sr.ht/~space
let cass_file = cli.value_of(meta::ARG_FILE).unwrap();
let mut cass = match Cassiopeia::load(cass_file) {
- Some(cf) => cf,
- None => {
- eprintln!(
- "Invalid CASS file '{}'; file not found, or unparsable.",
- cass_file
- );
- std::process::exit(2);
+ Ok(cf) => cf,
+ Err(e) => {
+ eprintln!("{}", e);
+ std::process::exit(1);
}
};
// Parse the matches generated by clap
match cli.subcommand() {
("start", Some(ops)) => {
- // This parameter turns rounding OFF
let round = ops.is_present(meta::ARG_ROUNDING);
- match cass.start(!round) {
- Some(()) => println!("Started session!"),
- None => {
- eprintln!("Failed to start session...");
- std::process::exit(1);
- }
- }
+ run_command(|| cass.start(!round));
}
("stop", Some(ops)) => {
- // This parameter turns rounding OFF
let round = ops.is_present(meta::ARG_ROUNDING);
- match cass.stop(!round) {
- Some(()) => println!("Stopped session!"),
- None => {
- eprintln!("Failed to stop session...");
- std::process::exit(1);
- }
- }
+ run_command(|| cass.stop(!round));
}
("invoice", _) => {
- println!("Invoice command only partially implemented! No generation is supported");
- match cass.invoice().run() {
- Some(()) => println!("Added INVOICE block"),
- None => {
- eprintln!("Failed to add INVOICE block...");
- std::process::exit(1);
- }
- }
+ eprintln!("Invoice command only partially implemented! No generation is supported");
+ run_command(|| cass.invoice().run());
}
- ("update", _) => match cass.update() {
- Some(()) => println!("Updated file to new version: {}", meta::VERSION),
- None => {
- eprintln!("Failed to update file...");
- std::process::exit(1);
- }
- },
- (meta::CMD_STAT, _) => match cass.stat() {
- Some(s) => println!("{}", s),
- None => {
- eprintln!("Failed to collect time statistics...");
- std::process::exit(1);
- }
- },
+ ("update", _) => run_command(|| cass.update()),
+ (meta::CMD_STAT, _) => run_command(|| {
+ let stats = cass.stat()?;
+ println!("{}", stats);
+ Ok(())
+ }),
(_, _) => todo!(),
}
}
+
+/// Run a closure and print the associated error message
+///
+/// Set the exit code for the program.
+fn run_command<F>(mut cmd: F)
+where
+ F: FnMut() -> ParseResult<()>,
+{
+ match cmd() {
+ Ok(_) => std::process::exit(0),
+ Err(e) => {
+ eprintln!("{}", e);
+ std::process::exit(2);
+ }
+ }
+}