11use std:: fs:: File ;
22use std:: io:: Write ;
33use std:: path:: Path ;
4- use std:: process:: exit;
54
65use 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;
910use 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}
0 commit comments