@@ -2,7 +2,7 @@ use std::path::PathBuf;
22
33use clap:: { CommandFactory , Parser , Subcommand } ;
44use cosmian_config_utils:: ConfigUtils ;
5- use cosmian_findex_cli:: reexports:: cosmian_findex_client:: FindexRestClient ;
5+ use cosmian_findex_cli:: { CoreFindexActions , reexports:: cosmian_findex_client:: FindexRestClient } ;
66use cosmian_kms_cli:: { KmsActions , reexport:: cosmian_kms_client:: KmsClient } ;
77use cosmian_logger:: log_init;
88use tracing:: { info, trace} ;
@@ -19,10 +19,10 @@ use crate::{
1919pub struct Cli {
2020 /// Configuration file location
2121 ///
22- /// This is an alternative to the env variable `KMS_CLI_CONF `.
23- /// Takes precedence over `KMS_CLI_CONF ` env variable.
24- #[ arg( short, long) ]
25- conf : Option < PathBuf > ,
22+ /// This is an alternative to the env variable `COSMIAN_CLI_CONF_PATH `.
23+ /// Takes precedence over `COSMIAN_CLI_CONF_PATH ` env variable.
24+ #[ arg( short, env = "COSMIAN_CLI_CONF_PATH" , long) ]
25+ conf_path : Option < PathBuf > ,
2626
2727 #[ command( subcommand) ]
2828 pub command : CliCommands ,
@@ -36,12 +36,12 @@ pub struct Cli {
3636 /// `accept_invalid_certs` is useful if the CLI needs to connect to an HTTPS KMS server
3737 /// running an invalid or insecure SSL certificate
3838 #[ arg( long) ]
39- pub kms_accept_invalid_certs : Option < bool > ,
39+ pub kms_accept_invalid_certs : bool ,
4040
4141 /// Output the KMS JSON KMIP request and response.
4242 /// This is useful to understand JSON POST requests and responses
4343 /// required to programmatically call the KMS on the `/kmip/2_1` endpoint
44- #[ arg( long, default_value = "false" ) ]
44+ #[ arg( long) ]
4545 pub kms_print_json : bool ,
4646
4747 /// The URL of the Findex server
@@ -53,7 +53,7 @@ pub struct Cli {
5353 /// `accept_invalid_certs` is useful if the CLI needs to connect to an HTTPS KMS server
5454 /// running an invalid or insecure SSL certificate
5555 #[ arg( long) ]
56- pub findex_accept_invalid_certs : Option < bool > ,
56+ pub findex_accept_invalid_certs : bool ,
5757}
5858
5959#[ derive( Subcommand ) ]
@@ -84,65 +84,74 @@ pub enum CliCommands {
8484/// - The command-line arguments cannot be parsed.
8585/// - The configuration file cannot be located or loaded.
8686/// - Any of the subcommands fail during their execution.
87- #[ allow( clippy:: future_not_send) ]
87+ #[ allow( clippy:: future_not_send, clippy :: cognitive_complexity ) ]
8888pub async fn cosmian_main ( ) -> CosmianResult < ( ) > {
8989 log_init ( None ) ;
9090 info ! ( "Starting Cosmian CLI" ) ;
9191 let cli = Cli :: parse ( ) ;
9292
93- let conf_path = ClientConf :: location ( cli. conf ) ?;
94- let mut conf = ClientConf :: from_toml ( & conf_path) ?;
93+ let conf_path = ClientConf :: location ( cli. conf_path ) ?;
94+ let mut config = ClientConf :: from_toml ( & conf_path) ?;
9595
96- // Override the configuration with the CLI arguments
97- let mut has_been_overridden = false ;
96+ // Handle KMS configuration
9897 if let Some ( url) = cli. kms_url . clone ( ) {
99- conf. kms_config . http_config . server_url = url;
100- has_been_overridden = true ;
98+ config. kms_config . http_config . server_url = url;
10199 }
102- if let Some ( accept_invalid_certs) = cli. kms_accept_invalid_certs {
103- conf. kms_config . http_config . accept_invalid_certs = accept_invalid_certs;
104- has_been_overridden = true ;
100+ if cli. kms_accept_invalid_certs {
101+ config. kms_config . http_config . accept_invalid_certs = true ;
105102 }
106- if let Some ( url) = cli. findex_url . clone ( ) {
107- if let Some ( findex_conf) = conf. findex_config . as_mut ( ) {
108- findex_conf. http_config . server_url = url;
109- has_been_overridden = true ;
103+ config. kms_config . print_json = Some ( cli. kms_print_json ) ;
104+
105+ // Handle Findex server configuration
106+ if let Some ( findex_config) = config. findex_config . as_mut ( ) {
107+ if let Some ( url) = cli. findex_url . clone ( ) {
108+ findex_config. http_config . server_url = url;
110109 }
111- }
112- if let Some ( accept_invalid_certs) = cli. findex_accept_invalid_certs {
113- if let Some ( findex_conf) = conf. findex_config . as_mut ( ) {
114- findex_conf. http_config . accept_invalid_certs = accept_invalid_certs;
115- has_been_overridden = true ;
110+ if cli. findex_accept_invalid_certs {
111+ findex_config. http_config . accept_invalid_certs = true ;
116112 }
117113 }
118- conf. kms_config . print_json = Some ( cli. kms_print_json ) ;
119- if has_been_overridden {
120- conf. to_toml ( & conf_path) ?;
121- }
122114
123- trace ! ( "Configuration: {conf :?}" ) ;
115+ trace ! ( "Configuration: {config :?}" ) ;
124116
125- // Instantiate the KMS and Findex clients
126- let kms_rest_client = KmsClient :: new ( conf . kms_config ) ?;
117+ // Instantiate the KMS client
118+ let mut kms_rest_client = KmsClient :: new ( config . kms_config . clone ( ) ) ?;
127119
128- match cli. command {
120+ match & cli. command {
129121 CliCommands :: Markdown ( action) => {
130122 let command = <Cli as CommandFactory >:: command ( ) ;
131123 action. process ( & command) ?;
132124 return Ok ( ( ) )
133125 }
134126 CliCommands :: Kms ( kms_actions) => {
135- kms_actions. process ( & kms_rest_client) . await ?;
127+ kms_actions. process ( & mut kms_rest_client) . await ?;
128+ config. kms_config = kms_rest_client. config . clone ( ) ;
136129 }
137130 CliCommands :: FindexServer ( findex_actions) => {
138- let findex_config = conf . findex_config . ok_or_else ( || {
131+ let findex_config = config . findex_config . as_ref ( ) . ok_or_else ( || {
139132 cli_error ! ( "Findex server configuration is missing in the configuration file" )
140133 } ) ?;
141- let findex_rest_client = FindexRestClient :: new ( findex_config) ?;
134+ let mut findex_rest_client = FindexRestClient :: new ( findex_config. clone ( ) ) ?;
142135 findex_actions
143- . run ( findex_rest_client, & kms_rest_client)
136+ . run ( & mut findex_rest_client, & kms_rest_client)
144137 . await ?;
138+ config. findex_config = Some ( findex_rest_client. config . clone ( ) ) ;
139+ }
140+ }
141+
142+ // Save the configuration
143+ match cli. command {
144+ CliCommands :: Kms ( KmsActions :: Login ( _) | KmsActions :: Logout ( _) ) => {
145+ config. to_toml ( & conf_path) ?;
146+ info ! ( "Saving configuration to: {conf_path:?}" ) ;
147+ }
148+ CliCommands :: FindexServer ( FindexActions :: Findex (
149+ CoreFindexActions :: Login ( _) | CoreFindexActions :: Logout ( _) ,
150+ ) ) => {
151+ config. to_toml ( & conf_path) ?;
152+ info ! ( "Saving configuration to: {conf_path:?}" ) ;
145153 }
154+ _ => { }
146155 }
147156
148157 Ok ( ( ) )
0 commit comments