diff --git a/src/actions.rs b/src/actions.rs index 93a72a0..6c72760 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,77 +1,87 @@ use std::fs::File; use std::io::Write; use std::path::Path; -use std::process::exit; use json::JsonValue; -use seahorse::Context; +use seahorse::{ActionError, ActionResult, Context}; +use crate::config::get_config_path; +use crate::error::invalid; use crate::json_object::{get_json_object_or_create, set_json_object}; -use crate::{config::get_config_path, error::invalid}; -pub fn init_action(c: &Context) { +pub fn init_action(c: &Context) -> ActionResult { let config_path = get_config_path(); let path = Path::new(&config_path); if path.exists() { println!("config file already exists"); } else { - clear_action(c); + clear_action(c)?; } + + Ok(()) } -pub fn list_action(c: &Context) { +pub fn list_action(c: &Context) -> ActionResult { let conf = get_json_object_or_create(c.bool_flag("force-create")); for (key, value) in conf.entries() { println!("{}\t{}", key, value); } + + Ok(()) } -pub fn clear_action(_c: &Context) { +pub fn clear_action(_c: &Context) -> ActionResult { let mut file = File::create(get_config_path()).unwrap(); - write!(file, "{}", "{}").unwrap(); + + write!(file, "{}", "{}").expect("couldn't overwrite config file"); println!("cleared config file at '{:?}'", get_config_path()); + + Ok(()) } -pub fn get_action(c: &Context) { +pub fn get_action(c: &Context) -> ActionResult { if c.args.len() != 1 { - return invalid("command"); + return Err(invalid("command")); } let conf = get_json_object_or_create(c.bool_flag("force-create")); let key = c.args.get(0); let Some(key) = key else { - return invalid("key"); + return Err(invalid("key")); }; if conf.has_key(&key) { println!("{}", conf[key]); - return; + return Ok(()); } if c.bool_flag("ignore-null") { println!(); } else { - eprintln!("could not find key '{}'", key); - exit(1); + return Err(ActionError { + message: format!("could not find key '{}'", key), + }); } + + Ok(()) } -pub fn set_action(c: &Context) { +pub fn set_action(c: &Context) -> ActionResult { if c.args.len() != 2 { - return invalid("command"); + return Err(invalid("command")); } let mut conf = get_json_object_or_create(c.bool_flag("force-create")); let Some(key) = c.args.get(0) else { - return invalid("key"); + return Err(invalid("key")); }; let Some(value_str) = c.args.get(1) else { - return invalid("value"); + return Err(invalid("value")); }; let json_value = JsonValue::from(value_str.as_str()); @@ -84,20 +94,22 @@ pub fn set_action(c: &Context) { conf.insert(key, value).unwrap(); match set_json_object(conf) { - Ok(_) => println!("updated config file"), + Ok(_) => println!("'{}' -> '{}'", key, value), Err(err) => eprintln!("{}", err), } + + Ok(()) } -pub fn remove_action(c: &Context) { +pub fn remove_action(c: &Context) -> ActionResult { let mut conf = get_json_object_or_create(c.bool_flag("force-create")); let Some(key) = c.args.get(0) else { - return invalid("key"); + return Err(invalid("key")); }; if !conf.has_key(&key) { println!("key '{}' was not found", key); - return; + return Ok(()); } conf.remove(&key); @@ -106,4 +118,6 @@ pub fn remove_action(c: &Context) { Ok(_) => println!("updated config file"), Err(err) => eprintln!("{}", err), } + + Ok(()) } diff --git a/src/commands.rs b/src/commands.rs index 7d73dee..30696d2 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -10,7 +10,7 @@ pub fn init() -> Command { .description("inits config file") .alias("i") .usage(format!("{} init", env!("CARGO_PKG_NAME"))) - .action(init_action) + .action_with_result(init_action) } pub fn list() -> Command { @@ -18,7 +18,7 @@ pub fn list() -> Command { .description("list all keys and values") .alias("l") .usage(format!("{} list", env!("CARGO_PKG_NAME"))) - .action(list_action) + .action_with_result(list_action) .flag(force_create()) } @@ -27,7 +27,7 @@ pub fn clear() -> Command { .description("clear your config file") .alias("c") .usage(format!("{} clear", env!("CARGO_PKG_NAME"))) - .action(clear_action) + .action_with_result(clear_action) } pub fn remove_value() -> Command { @@ -35,7 +35,7 @@ pub fn remove_value() -> Command { .description("remove a value") .alias("r") .usage(format!("{} remove foo", env!("CARGO_PKG_NAME"))) - .action(remove_action) + .action_with_result(remove_action) } pub fn get_value() -> Command { @@ -43,7 +43,7 @@ pub fn get_value() -> Command { .description("get a value") .alias("g") .usage(format!("{} get foo", env!("CARGO_PKG_NAME"))) - .action(get_action) + .action_with_result(get_action) .flag(ignore_null()) .flag(force_create()) } @@ -53,6 +53,6 @@ pub fn set_value() -> Command { .description("set a value") .alias("s") .usage(format!("{} set foo bar", env!("CARGO_PKG_NAME"))) - .action(set_action) + .action_with_result(set_action) .flag(force_create()) } diff --git a/src/error.rs b/src/error.rs index cacf443..8805dcd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,11 @@ -use std::process::exit; +use seahorse::ActionError; -pub fn invalid(cause: &str) { - eprintln!("invalid {}. get help by running `conf set --help`", cause); - exit(1); +pub fn invalid(cause: &str) -> ActionError { + ActionError { + message: format!( + "invalid {}. get help by running `{} --help`", + cause, + env!("CARGO_PKG_NAME") + ), + } } diff --git a/src/main.rs b/src/main.rs index b6f78b4..cb0d97b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ -use std::{env, io}; +use std::{env, process::exit}; -use seahorse::App; +use seahorse::{ActionResult, App}; use crate::commands::{clear, get_value, init, list, remove_value, set_value}; @@ -11,7 +11,7 @@ mod error; mod flags; mod json_object; -fn main() -> io::Result<()> { +fn main() -> ActionResult { let args: Vec = env::args().collect(); let app = App::new(env!("CARGO_PKG_NAME")) .description(env!("CARGO_PKG_DESCRIPTION")) @@ -25,7 +25,11 @@ fn main() -> io::Result<()> { .command(remove_value()) .command(clear()); - app.run(args); - - Ok(()) + match app.run_with_result(args) { + Ok(_) => Ok(()), + Err(action_error) => { + eprintln!("{}", action_error.message); + exit(1) + } + } }