1- use std:: fs:: File ;
2- use std:: io:: Write ;
31use std:: path:: Path ;
42
5- use json:: JsonValue ;
63use seahorse:: { ActionError , ActionResult , Context } ;
74
85use crate :: config:: get_config_path;
96use crate :: error:: invalid;
10- use crate :: json_object:: { get_json_object_or_create, set_json_object} ;
7+ use crate :: storage:: json:: CfsJSONStore ;
8+ use crate :: storage:: { CfsStorage , CfsValue } ;
119
12- pub fn init_action ( c : & Context ) -> ActionResult {
10+ pub fn init_action ( _c : & Context ) -> ActionResult {
1311 let config_path = get_config_path ( ) ;
1412 let path = Path :: new ( & config_path) ;
1513
1614 if path. exists ( ) {
1715 println ! ( "config file already exists" ) ;
18- } else {
19- clear_action ( c) ?;
2016 }
2117
18+ CfsJSONStore :: with_force_create ( true ) ;
19+
2220 Ok ( ( ) )
2321}
2422
2523pub fn list_action ( c : & Context ) -> ActionResult {
26- let conf = get_json_object_or_create ( c. bool_flag ( "force-create" ) ) ;
24+ let store = CfsJSONStore :: with_force_create ( c. bool_flag ( "force-create" ) ) ;
2725
28- for ( key, value) in conf . entries ( ) {
26+ for ( key, value) in store . all ( ) . iter ( ) {
2927 println ! ( "{}\t {}" , key, value) ;
3028 }
3129
3230 Ok ( ( ) )
3331}
3432
3533pub fn clear_action ( _c : & Context ) -> ActionResult {
36- let mut file = File :: create ( get_config_path ( ) ) . unwrap ( ) ;
34+ let mut store = CfsJSONStore :: new ( ) ;
35+
36+ store. clear ( ) ;
3737
38- write ! ( file, "{}" , "{}" ) . expect ( "couldn't overwrite config file" ) ;
3938 println ! ( "cleared config file at '{:?}'" , get_config_path( ) ) ;
4039
4140 Ok ( ( ) )
@@ -46,24 +45,29 @@ pub fn get_action(c: &Context) -> ActionResult {
4645 return Err ( invalid ( "command" ) ) ;
4746 }
4847
49- let conf = get_json_object_or_create ( c. bool_flag ( "force-create" ) ) ;
50- let key = c. args . get ( 0 ) ;
48+ let key = c. args . get ( 0 ) . to_owned ( ) ;
5149
5250 let Some ( key) = key else {
5351 return Err ( invalid ( "key" ) ) ;
5452 } ;
5553
56- if conf. has_key ( & key) {
57- println ! ( "{}" , conf[ key] ) ;
58- return Ok ( ( ) ) ;
59- }
60-
61- if c. bool_flag ( "ignore-null" ) {
62- println ! ( ) ;
63- } else {
64- return Err ( ActionError {
65- message : format ! ( "could not find key '{}'" , key) ,
66- } ) ;
54+ let store = CfsJSONStore :: new ( ) ;
55+
56+ let value = store. get ( key) ;
57+
58+ match value {
59+ Some ( v) => {
60+ println ! ( "{}" , v)
61+ }
62+ None => {
63+ if c. bool_flag ( "ignore_null" ) {
64+ println ! ( ) ;
65+ } else {
66+ return Err ( ActionError {
67+ message : format ! ( "could not find key '{}'" , key) ,
68+ } ) ;
69+ }
70+ }
6771 }
6872
6973 Ok ( ( ) )
@@ -74,8 +78,6 @@ pub fn set_action(c: &Context) -> ActionResult {
7478 return Err ( invalid ( "command" ) ) ;
7579 }
7680
77- let mut conf = get_json_object_or_create ( c. bool_flag ( "force-create" ) ) ;
78-
7981 let Some ( key) = c. args . get ( 0 ) else {
8082 return Err ( invalid ( "key" ) ) ;
8183 } ;
@@ -84,39 +86,28 @@ pub fn set_action(c: &Context) -> ActionResult {
8486 return Err ( invalid ( "value" ) ) ;
8587 } ;
8688
87- let json_value = JsonValue :: from ( value_str. as_str ( ) ) ;
88- let value = json_value. as_str ( ) . unwrap ( ) ;
89+ let mut store = CfsJSONStore :: new ( ) ;
8990
90- if conf. has_key ( key) {
91- conf. remove ( key) ;
92- }
93-
94- conf. insert ( key, value) . unwrap ( ) ;
91+ let value = CfsValue :: Value ( value_str. to_owned ( ) ) ;
92+ store. set ( key, value. clone ( ) ) ;
9593
96- match set_json_object ( conf) {
97- Ok ( _) => println ! ( "'{}' -> '{}'" , key, value) ,
98- Err ( err) => eprintln ! ( "{}" , err) ,
99- }
94+ println ! ( "{}\t {}" , key, value) ;
10095
10196 Ok ( ( ) )
10297}
10398
10499pub fn remove_action ( c : & Context ) -> ActionResult {
105- let mut conf = get_json_object_or_create ( c. bool_flag ( "force-create" ) ) ;
106100 let Some ( key) = c. args . get ( 0 ) else {
107101 return Err ( invalid ( "key" ) ) ;
108102 } ;
109103
110- if !conf. has_key ( & key) {
111- println ! ( "key '{}' was not found" , key) ;
112- return Ok ( ( ) ) ;
113- }
114-
115- conf. remove ( & key) ;
104+ let mut store = CfsJSONStore :: new ( ) ;
116105
117- match set_json_object ( conf) {
118- Ok ( _) => println ! ( "updated config file" ) ,
119- Err ( err) => eprintln ! ( "{}" , err) ,
106+ match store. remove ( key) {
107+ Some ( value) => println ! ( "{}\t {}" , key, value) ,
108+ None => {
109+ println ! ( "key '{}' was not found" , key) ;
110+ }
120111 }
121112
122113 Ok ( ( ) )
0 commit comments