11use anyhow:: anyhow;
2- use config:: ConfigBuilder ;
3- use config:: builder:: DefaultState ;
42use slog:: Logger ;
5- use std:: collections:: HashMap ;
63
74use mithril_client:: MithrilResult ;
85
96use crate :: configuration:: ConfigParameters ;
107
118/// Context for the command execution
129pub struct CommandContext {
13- config_builder : ConfigBuilder < DefaultState > ,
10+ config_parameters : ConfigParameters ,
1411 unstable_enabled : bool ,
12+ json : bool ,
1513 logger : Logger ,
1614}
1715
1816impl CommandContext {
1917 /// Create a new command context
2018 pub fn new (
21- config_builder : ConfigBuilder < DefaultState > ,
19+ config_parameters : ConfigParameters ,
2220 unstable_enabled : bool ,
21+ json : bool ,
2322 logger : Logger ,
2423 ) -> Self {
2524 Self {
26- config_builder ,
25+ config_parameters ,
2726 unstable_enabled,
27+ json,
2828 logger,
2929 }
3030 }
@@ -34,6 +34,11 @@ impl CommandContext {
3434 self . unstable_enabled
3535 }
3636
37+ /// Check if JSON output is enabled
38+ pub fn is_json_output_enabled ( & self ) -> bool {
39+ self . json
40+ }
41+
3742 /// Ensure that unstable commands are enabled
3843 pub fn require_unstable (
3944 & self ,
@@ -51,11 +56,14 @@ impl CommandContext {
5156 }
5257 }
5358
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
5967 }
6068
6169 /// Get the shared logger
@@ -67,15 +75,19 @@ impl CommandContext {
6775#[ cfg( test) ]
6876mod tests {
6977 use slog:: o;
78+ use std:: collections:: HashMap ;
79+
80+ use crate :: configuration:: { ConfigError , ConfigSource } ;
7081
7182 use super :: * ;
7283
7384 #[ test]
7485 fn require_unstable_return_ok_if_unstable_enabled ( ) {
7586 let unstable_enabled = true ;
7687 let context = CommandContext :: new (
77- ConfigBuilder :: default ( ) ,
88+ ConfigParameters :: default ( ) ,
7889 unstable_enabled,
90+ true ,
7991 Logger :: root ( slog:: Discard , o ! ( ) ) ,
8092 ) ;
8193
@@ -87,12 +99,48 @@ mod tests {
8799 fn require_unstable_return_err_if_unstable_disabled ( ) {
88100 let unstable_enabled = false ;
89101 let context = CommandContext :: new (
90- ConfigBuilder :: default ( ) ,
102+ ConfigParameters :: default ( ) ,
91103 unstable_enabled,
104+ true ,
92105 Logger :: root ( slog:: Discard , o ! ( ) ) ,
93106 ) ;
94107
95108 let result = context. require_unstable ( "test" , None ) ;
96109 assert ! ( result. is_err( ) , "Expected Err, got {result:?}" ) ;
97110 }
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+ }
98146}
0 commit comments