Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/actions.rs
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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);
Expand All @@ -106,4 +118,6 @@ pub fn remove_action(c: &Context) {
Ok(_) => println!("updated config file"),
Err(err) => eprintln!("{}", err),
}

Ok(())
}
12 changes: 6 additions & 6 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ 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 {
Command::new("list")
.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())
}

Expand All @@ -27,23 +27,23 @@ 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 {
Command::new("remove")
.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 {
Command::new("get")
.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())
}
Expand All @@ -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())
}
13 changes: 9 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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")
),
}
}
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -11,7 +11,7 @@ mod error;
mod flags;
mod json_object;

fn main() -> io::Result<()> {
fn main() -> ActionResult {
let args: Vec<String> = env::args().collect();
let app = App::new(env!("CARGO_PKG_NAME"))
.description(env!("CARGO_PKG_DESCRIPTION"))
Expand All @@ -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)
}
}
}