Skip to content

Commit 035ed15

Browse files
committed
feat: use seahorse ActionResult
1 parent fa7033c commit 035ed15

4 files changed

Lines changed: 61 additions & 42 deletions

File tree

src/actions.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,87 @@
11
use std::fs::File;
22
use std::io::Write;
33
use std::path::Path;
4-
use std::process::exit;
54

65
use json::JsonValue;
7-
use seahorse::Context;
6+
use seahorse::{ActionError, ActionResult, Context};
87

8+
use crate::config::get_config_path;
9+
use crate::error::invalid;
910
use crate::json_object::{get_json_object_or_create, set_json_object};
10-
use crate::{config::get_config_path, error::invalid};
1111

12-
pub fn init_action(c: &Context) {
12+
pub fn init_action(c: &Context) -> ActionResult {
1313
let config_path = get_config_path();
1414
let path = Path::new(&config_path);
1515

1616
if path.exists() {
1717
println!("config file already exists");
1818
} else {
19-
clear_action(c);
19+
clear_action(c)?;
2020
}
21+
22+
Ok(())
2123
}
2224

23-
pub fn list_action(c: &Context) {
25+
pub fn list_action(c: &Context) -> ActionResult {
2426
let conf = get_json_object_or_create(c.bool_flag("force-create"));
2527

2628
for (key, value) in conf.entries() {
2729
println!("{}\t{}", key, value);
2830
}
31+
32+
Ok(())
2933
}
3034

31-
pub fn clear_action(_c: &Context) {
35+
pub fn clear_action(_c: &Context) -> ActionResult {
3236
let mut file = File::create(get_config_path()).unwrap();
33-
write!(file, "{}", "{}").unwrap();
37+
38+
write!(file, "{}", "{}").expect("couldn't overwrite config file");
3439
println!("cleared config file at '{:?}'", get_config_path());
40+
41+
Ok(())
3542
}
3643

37-
pub fn get_action(c: &Context) {
44+
pub fn get_action(c: &Context) -> ActionResult {
3845
if c.args.len() != 1 {
39-
return invalid("command");
46+
return Err(invalid("command"));
4047
}
4148

4249
let conf = get_json_object_or_create(c.bool_flag("force-create"));
4350
let key = c.args.get(0);
4451

4552
let Some(key) = key else {
46-
return invalid("key");
53+
return Err(invalid("key"));
4754
};
4855

4956
if conf.has_key(&key) {
5057
println!("{}", conf[key]);
51-
return;
58+
return Ok(());
5259
}
5360

5461
if c.bool_flag("ignore-null") {
5562
println!();
5663
} else {
57-
eprintln!("could not find key '{}'", key);
58-
exit(1);
64+
return Err(ActionError {
65+
message: format!("could not find key '{}'", key),
66+
});
5967
}
68+
69+
Ok(())
6070
}
6171

62-
pub fn set_action(c: &Context) {
72+
pub fn set_action(c: &Context) -> ActionResult {
6373
if c.args.len() != 2 {
64-
return invalid("command");
74+
return Err(invalid("command"));
6575
}
6676

6777
let mut conf = get_json_object_or_create(c.bool_flag("force-create"));
6878

6979
let Some(key) = c.args.get(0) else {
70-
return invalid("key");
80+
return Err(invalid("key"));
7181
};
7282

7383
let Some(value_str) = c.args.get(1) else {
74-
return invalid("value");
84+
return Err(invalid("value"));
7585
};
7686

7787
let json_value = JsonValue::from(value_str.as_str());
@@ -84,20 +94,22 @@ pub fn set_action(c: &Context) {
8494
conf.insert(key, value).unwrap();
8595

8696
match set_json_object(conf) {
87-
Ok(_) => println!("updated config file"),
97+
Ok(_) => println!("{}\t{}", key, value),
8898
Err(err) => eprintln!("{}", err),
8999
}
100+
101+
Ok(())
90102
}
91103

92-
pub fn remove_action(c: &Context) {
104+
pub fn remove_action(c: &Context) -> ActionResult {
93105
let mut conf = get_json_object_or_create(c.bool_flag("force-create"));
94106
let Some(key) = c.args.get(0) else {
95-
return invalid("key");
107+
return Err(invalid("key"));
96108
};
97109

98110
if !conf.has_key(&key) {
99111
println!("key '{}' was not found", key);
100-
return;
112+
return Ok(());
101113
}
102114

103115
conf.remove(&key);
@@ -106,4 +118,6 @@ pub fn remove_action(c: &Context) {
106118
Ok(_) => println!("updated config file"),
107119
Err(err) => eprintln!("{}", err),
108120
}
121+
122+
Ok(())
109123
}

src/commands.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ pub fn init() -> Command {
1010
.description("inits config file")
1111
.alias("i")
1212
.usage(format!("{} init", env!("CARGO_PKG_NAME")))
13-
.action(init_action)
13+
.action_with_result(init_action)
1414
}
1515

1616
pub fn list() -> Command {
1717
Command::new("list")
1818
.description("list all keys and values")
1919
.alias("l")
2020
.usage(format!("{} list", env!("CARGO_PKG_NAME")))
21-
.action(list_action)
21+
.action_with_result(list_action)
2222
.flag(force_create())
2323
}
2424

@@ -27,23 +27,23 @@ pub fn clear() -> Command {
2727
.description("clear your config file")
2828
.alias("c")
2929
.usage(format!("{} clear", env!("CARGO_PKG_NAME")))
30-
.action(clear_action)
30+
.action_with_result(clear_action)
3131
}
3232

3333
pub fn remove_value() -> Command {
3434
Command::new("remove")
3535
.description("remove a value")
3636
.alias("r")
3737
.usage(format!("{} remove foo", env!("CARGO_PKG_NAME")))
38-
.action(remove_action)
38+
.action_with_result(remove_action)
3939
}
4040

4141
pub fn get_value() -> Command {
4242
Command::new("get")
4343
.description("get a value")
4444
.alias("g")
4545
.usage(format!("{} get foo", env!("CARGO_PKG_NAME")))
46-
.action(get_action)
46+
.action_with_result(get_action)
4747
.flag(ignore_null())
4848
.flag(force_create())
4949
}
@@ -53,6 +53,6 @@ pub fn set_value() -> Command {
5353
.description("set a value")
5454
.alias("s")
5555
.usage(format!("{} set foo bar", env!("CARGO_PKG_NAME")))
56-
.action(set_action)
56+
.action_with_result(set_action)
5757
.flag(force_create())
5858
}

src/error.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use std::process::exit;
1+
use seahorse::ActionError;
22

3-
pub fn invalid(cause: &str) {
4-
eprintln!(
5-
"invalid {}. get help by running `{} --help`",
6-
cause,
7-
env!("CARGO_PKG_NAME")
8-
);
9-
exit(1);
3+
pub fn invalid(cause: &str) -> ActionError {
4+
ActionError {
5+
message: format!(
6+
"invalid {}. get help by running `{} --help`",
7+
cause,
8+
env!("CARGO_PKG_NAME")
9+
),
10+
}
1011
}

src/main.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{env, io};
1+
use std::{env, process::exit};
22

3-
use seahorse::App;
3+
use seahorse::{ActionResult, App};
44

55
use crate::commands::{clear, get_value, init, list, remove_value, set_value};
66

@@ -11,7 +11,7 @@ mod error;
1111
mod flags;
1212
mod json_object;
1313

14-
fn main() -> io::Result<()> {
14+
fn main() -> ActionResult {
1515
let args: Vec<String> = env::args().collect();
1616
let app = App::new(env!("CARGO_PKG_NAME"))
1717
.description(env!("CARGO_PKG_DESCRIPTION"))
@@ -25,7 +25,11 @@ fn main() -> io::Result<()> {
2525
.command(remove_value())
2626
.command(clear());
2727

28-
app.run(args);
29-
30-
Ok(())
28+
match app.run_with_result(args) {
29+
Ok(_) => Ok(()),
30+
Err(action_error) => {
31+
eprintln!("{}", action_error.message);
32+
exit(1)
33+
}
34+
}
3135
}

0 commit comments

Comments
 (0)