1
1
use anyhow:: anyhow;
2
- use config:: ConfigBuilder ;
3
- use config:: builder:: DefaultState ;
4
2
use slog:: Logger ;
5
- use std:: collections:: HashMap ;
6
3
7
4
use mithril_client:: MithrilResult ;
8
5
9
6
use crate :: configuration:: ConfigParameters ;
10
7
11
8
/// Context for the command execution
12
9
pub struct CommandContext {
13
- config_builder : ConfigBuilder < DefaultState > ,
10
+ config_parameters : ConfigParameters ,
14
11
unstable_enabled : bool ,
12
+ json : bool ,
15
13
logger : Logger ,
16
14
}
17
15
18
16
impl CommandContext {
19
17
/// Create a new command context
20
18
pub fn new (
21
- config_builder : ConfigBuilder < DefaultState > ,
19
+ config_parameters : ConfigParameters ,
22
20
unstable_enabled : bool ,
21
+ json : bool ,
23
22
logger : Logger ,
24
23
) -> Self {
25
24
Self {
26
- config_builder ,
25
+ config_parameters ,
27
26
unstable_enabled,
27
+ json,
28
28
logger,
29
29
}
30
30
}
@@ -34,6 +34,11 @@ impl CommandContext {
34
34
self . unstable_enabled
35
35
}
36
36
37
+ /// Check if JSON output is enabled
38
+ pub fn is_json_output_enabled ( & self ) -> bool {
39
+ self . json
40
+ }
41
+
37
42
/// Ensure that unstable commands are enabled
38
43
pub fn require_unstable (
39
44
& self ,
@@ -51,11 +56,14 @@ impl CommandContext {
51
56
}
52
57
}
53
58
54
- /// Get the configured parameters
55
- pub fn config_parameters ( & self ) -> MithrilResult < ConfigParameters > {
56
- let config = self . config_builder . clone ( ) . build ( ) ?;
57
- let config_hash_map = config. try_deserialize :: < HashMap < String , String > > ( ) ?;
58
- Ok ( ConfigParameters :: new ( config_hash_map) )
59
+ /// Get a reference to the configured parameters
60
+ pub fn config_parameters ( & self ) -> & ConfigParameters {
61
+ & self . config_parameters
62
+ }
63
+
64
+ /// Get a mutable reference to the configured parameters
65
+ pub fn config_parameters_mut ( & mut self ) -> & mut ConfigParameters {
66
+ & mut self . config_parameters
59
67
}
60
68
61
69
/// Get the shared logger
@@ -67,15 +75,19 @@ impl CommandContext {
67
75
#[ cfg( test) ]
68
76
mod tests {
69
77
use slog:: o;
78
+ use std:: collections:: HashMap ;
79
+
80
+ use crate :: configuration:: { ConfigError , ConfigSource } ;
70
81
71
82
use super :: * ;
72
83
73
84
#[ test]
74
85
fn require_unstable_return_ok_if_unstable_enabled ( ) {
75
86
let unstable_enabled = true ;
76
87
let context = CommandContext :: new (
77
- ConfigBuilder :: default ( ) ,
88
+ ConfigParameters :: default ( ) ,
78
89
unstable_enabled,
90
+ true ,
79
91
Logger :: root ( slog:: Discard , o ! ( ) ) ,
80
92
) ;
81
93
@@ -87,12 +99,48 @@ mod tests {
87
99
fn require_unstable_return_err_if_unstable_disabled ( ) {
88
100
let unstable_enabled = false ;
89
101
let context = CommandContext :: new (
90
- ConfigBuilder :: default ( ) ,
102
+ ConfigParameters :: default ( ) ,
91
103
unstable_enabled,
104
+ true ,
92
105
Logger :: root ( slog:: Discard , o ! ( ) ) ,
93
106
) ;
94
107
95
108
let result = context. require_unstable ( "test" , None ) ;
96
109
assert ! ( result. is_err( ) , "Expected Err, got {result:?}" ) ;
97
110
}
111
+
112
+ #[ test]
113
+ fn can_edit_config_parameters ( ) {
114
+ struct ParamSource {
115
+ key : String ,
116
+ value : String ,
117
+ }
118
+ impl ConfigSource for ParamSource {
119
+ fn collect ( & self ) -> Result < HashMap < String , String > , ConfigError > {
120
+ Ok ( HashMap :: from ( [ ( self . key . clone ( ) , self . value . clone ( ) ) ] ) )
121
+ }
122
+ }
123
+
124
+ let mut context = CommandContext :: new (
125
+ ConfigParameters :: default ( ) ,
126
+ false ,
127
+ true ,
128
+ Logger :: root ( slog:: Discard , o ! ( ) ) ,
129
+ ) ;
130
+
131
+ assert_eq ! ( context. config_parameters_mut( ) . get( "key" ) , None , ) ;
132
+
133
+ context
134
+ . config_parameters_mut ( )
135
+ . add_source ( & ParamSource {
136
+ key : "key" . to_string ( ) ,
137
+ value : "value" . to_string ( ) ,
138
+ } )
139
+ . unwrap ( ) ;
140
+
141
+ assert_eq ! (
142
+ context. config_parameters_mut( ) . get( "key" ) ,
143
+ Some ( "value" . to_string( ) )
144
+ ) ;
145
+ }
98
146
}
0 commit comments