1
- use std:: { collections:: VecDeque , fs} ;
1
+ use std:: { collections:: VecDeque , fs, path :: PathBuf } ;
2
2
3
3
use clap:: { Args , Subcommand } ;
4
4
use toml:: Value ;
5
5
6
- use crate :: settings:: { get_user_config_path, Settings } ;
6
+ use crate :: settings:: { get_local_config_path , get_user_config_path, Settings } ;
7
7
use anyhow:: { bail, Result } ;
8
8
9
9
#[ derive( Subcommand , Debug ) ]
@@ -17,9 +17,20 @@ pub(crate) enum ConfigAction {
17
17
/// Read config value
18
18
Get { key : String } ,
19
19
/// Set config value
20
- Set { key : String , value : String } ,
20
+ Set {
21
+ key : String ,
22
+ value : String ,
23
+ /// if set, modifies the local config. Default behavior modifies global config
24
+ #[ clap( long) ]
25
+ local : bool ,
26
+ } ,
21
27
/// Clear config value
22
- Delete { key : String } ,
28
+ Delete {
29
+ key : String ,
30
+ /// if set, modifies the local config. Default behavior modifies global config
31
+ #[ clap( long) ]
32
+ local : bool ,
33
+ } ,
23
34
}
24
35
25
36
#[ derive( Args , Debug ) ]
@@ -34,28 +45,36 @@ pub(crate) async fn main(settings: Settings, args: ConfigArgs) -> Result<()> {
34
45
match args. action {
35
46
ConfigAction :: List { save } => list ( settings, save) . await ,
36
47
ConfigAction :: Get { key } => get ( settings, key) . await ,
37
- ConfigAction :: Set { key, value } => set ( settings, key, value) . await ,
38
- ConfigAction :: Delete { key } => delete ( settings, key) . await ,
48
+ ConfigAction :: Set { key, value, local } => set ( settings, key, value, local ) . await ,
49
+ ConfigAction :: Delete { key, local } => delete ( settings, key, local ) . await ,
39
50
}
40
51
}
41
52
42
- async fn delete ( _settings : Settings , full_key : String ) -> Result < ( ) > {
53
+ async fn delete ( _settings : Settings , full_key : String , local : bool ) -> Result < ( ) > {
43
54
let settings = & Settings :: from_clear ( & full_key) ?;
44
55
let toml_string = toml:: to_string_pretty ( settings) . unwrap ( ) ;
45
- let user_config_path = get_user_config_path ( ) . expect ( "Could not find user config path" ) ;
46
- fs:: write ( & user_config_path, toml_string) ?;
56
+ let config_path: PathBuf = if local {
57
+ get_local_config_path ( ) . expect ( "Could not find repo-local config path" )
58
+ } else {
59
+ get_user_config_path ( ) . expect ( "Could not find user config path" )
60
+ } ;
61
+ fs:: write ( & config_path, toml_string) ?;
47
62
println ! ( "Cleared {full_key}" ) ;
48
- println ! ( "Config saved to {}" , user_config_path . display( ) ) ;
63
+ println ! ( "Config saved to {}" , config_path . display( ) ) ;
49
64
Ok ( ( ) )
50
65
}
51
66
52
- async fn set ( _settings : Settings , full_key : String , value : String ) -> Result < ( ) > {
67
+ async fn set ( _settings : Settings , full_key : String , value : String , local : bool ) -> Result < ( ) > {
53
68
let settings = & Settings :: from_set_override ( & full_key, & value) ?;
54
69
let toml_string = toml:: to_string_pretty ( settings) . unwrap ( ) ;
55
- let user_config_path = get_user_config_path ( ) . expect ( "Could not find user config path" ) ;
56
- fs:: write ( & user_config_path, toml_string) ?;
70
+ let config_path: PathBuf = if local {
71
+ get_local_config_path ( ) . expect ( "Could not find repo-local config path" )
72
+ } else {
73
+ get_user_config_path ( ) . expect ( "Could not find user config path" )
74
+ } ;
75
+ fs:: write ( & config_path, toml_string) ?;
57
76
println ! ( "{full_key} = {value}" ) ;
58
- println ! ( "Config saved to {}" , user_config_path . display( ) ) ;
77
+ println ! ( "Config saved to {}" , config_path . display( ) ) ;
59
78
Ok ( ( ) )
60
79
}
61
80
0 commit comments