1
1
use std:: { collections:: HashMap , fs, path:: PathBuf , str:: FromStr , sync:: LazyLock , time:: { Duration , Instant } } ;
2
2
3
- use clap:: { Args , CommandFactory , FromArgMatches , Parser } ;
3
+ use clap:: Parser ;
4
4
use reqwest:: { Certificate , Client , Url } ;
5
5
use anyhow:: anyhow;
6
6
use tokio:: sync:: RwLock ;
7
7
use tracing:: info;
8
8
9
- use crate :: { ttp:: { self , greifswald :: GreifswaldConfig , mainzelliste :: MlConfig , Ttp } , CONFIG } ;
9
+ use crate :: { ttp:: Ttp , CLIENT } ;
10
10
11
- #[ derive( Parser , Clone , Debug ) ]
11
+ #[ derive( Debug , Parser ) ]
12
12
#[ clap( author, version, about, long_about = None ) ]
13
- pub struct Config {
13
+ pub struct CliArgs {
14
+ #[ clap( subcommand) ]
15
+ pub subcommand : SubCommand ,
16
+
17
+ /// Trusted tls root certificates
18
+ #[ clap( long, env) ]
19
+ pub tls_ca_certificates_dir : Option < PathBuf > ,
20
+ /// Disable TLS verification
21
+ #[ clap( long, env, default_value_t = false ) ]
22
+ pub tls_disable : bool ,
23
+ }
24
+
25
+ impl CliArgs {
26
+ pub fn build_client ( & self ) -> Client {
27
+ let mut client_builder = Client :: builder ( ) ;
28
+ client_builder = client_builder
29
+ . danger_accept_invalid_hostnames ( self . tls_disable )
30
+ . danger_accept_invalid_certs ( self . tls_disable ) ;
31
+ if let Some ( tls_ca_dir) = & self . tls_ca_certificates_dir {
32
+ info ! ( "Loading available custom ca certificates from {:?}" , self . tls_ca_certificates_dir) ;
33
+ for path_res in tls_ca_dir. read_dir ( ) . expect ( & format ! ( "Unable to read {:?}" , self . tls_ca_certificates_dir) ) {
34
+ if let Ok ( path_buf) = path_res {
35
+ info ! ( "Adding custom ca certificate {:?}" , path_buf. path( ) ) ;
36
+ client_builder = client_builder. add_root_certificate (
37
+ Certificate :: from_pem (
38
+ & fs:: read ( path_buf. path ( ) ) . expect ( & format ! ( "Unable to read file provided: {:?}" , path_buf. path( ) ) )
39
+ ) . expect ( & format ! ( "Unable to convert {:?} to a certificate. Please verify it is a valid pem file" , path_buf. path( ) ) )
40
+ ) ;
41
+ }
42
+ }
43
+ }
44
+
45
+ client_builder. build ( ) . expect ( "Unable to initially build reqwest client" )
46
+ }
47
+ }
48
+
49
+ #[ derive( Debug , clap:: Subcommand ) ]
50
+ pub enum SubCommand {
51
+ Dic ( DicConfig )
52
+ }
53
+
54
+ #[ derive( Parser , Clone , Debug ) ]
55
+ pub struct DicConfig {
14
56
// Definition of necessary parameters for communicating with a ttp
15
- #[ clap( skip ) ]
57
+ #[ clap( subcommand ) ]
16
58
pub ttp : Option < Ttp > ,
17
59
// Either an id well-known to both, project and dic, or a temporary identifier created by the ttp
18
60
#[ clap( long, env, default_value = "TOKEN" ) ]
@@ -35,55 +77,6 @@ pub struct Config {
35
77
pub fhir_output_url : Url ,
36
78
#[ clap( long, env, default_value = "" ) ]
37
79
pub fhir_output_credentials : Auth ,
38
- /// Trusted tls root certificates
39
- #[ clap( long, env) ]
40
- pub tls_ca_certificates_dir : Option < PathBuf > ,
41
- /// Disable TLS verification
42
- #[ clap( long, env, default_value_t = false ) ]
43
- pub tls_disable : bool ,
44
-
45
- #[ clap( skip) ]
46
- pub client : Client ,
47
- }
48
-
49
- impl Config {
50
- pub fn parse ( ) -> Self {
51
- let cmd = Config :: command ( ) ;
52
- let ttp_cmd = ttp:: Ttp :: augment_args ( cmd. clone ( ) ) ;
53
- let args_matches = cmd. get_matches ( ) ;
54
- let mut this = Self :: from_arg_matches ( & args_matches) . map_err ( |e| e. exit ( ) ) . unwrap ( ) ;
55
- let ca_client = build_client ( & this. tls_ca_certificates_dir , this. tls_disable ) ;
56
- this. client = ca_client. clone ( ) ;
57
- let mut ttp = ttp_cmd. try_get_matches ( ) . ok ( ) . and_then ( |matches| Ttp :: from_arg_matches ( & matches) . ok ( ) ) ;
58
- if let Some ( ref mut ttp) = ttp {
59
- let ( Ttp :: Mainzelliste ( MlConfig { base, ..} ) | Ttp :: Greifswald ( GreifswaldConfig { base, ..} ) ) = ttp;
60
- base. client = ca_client. clone ( ) ;
61
- }
62
- this. ttp = ttp;
63
- this
64
- }
65
- }
66
-
67
- fn build_client ( tls_ca_certificates_dir : & Option < PathBuf > , disable_tls : bool ) -> Client {
68
- let mut client_builder = Client :: builder ( ) ;
69
- client_builder = client_builder
70
- . danger_accept_invalid_hostnames ( disable_tls)
71
- . danger_accept_invalid_certs ( disable_tls) ;
72
- if let Some ( tls_ca_dir) = tls_ca_certificates_dir {
73
- info ! ( "Loading available custom ca certificates from {:?}" , tls_ca_certificates_dir) ;
74
- for path_res in tls_ca_dir. read_dir ( ) . expect ( & format ! ( "Unable to read {:?}" , tls_ca_certificates_dir) ) {
75
- if let Ok ( path_buf) = path_res {
76
- info ! ( "Adding custom ca certificate {:?}" , path_buf. path( ) ) ;
77
- client_builder = client_builder. add_root_certificate (
78
- Certificate :: from_pem (
79
- & fs:: read ( path_buf. path ( ) ) . expect ( & format ! ( "Unable to read file provided: {:?}" , path_buf. path( ) ) )
80
- ) . expect ( & format ! ( "Unable to convert {:?} to a certificate. Please verify it is a valid pem file" , path_buf. path( ) ) )
81
- ) ;
82
- }
83
- }
84
- }
85
-
86
- client_builder. build ( ) . expect ( "Unable to initially build reqwest client" )
87
80
}
88
81
89
82
#[ derive( Debug , Clone ) ]
@@ -144,7 +137,7 @@ impl ClientBuilderExt for reqwest::RequestBuilder {
144
137
expires_in : u64 ,
145
138
access_token : String ,
146
139
}
147
- let TokenRes { expires_in, access_token } = CONFIG . client
140
+ let TokenRes { expires_in, access_token } = CLIENT
148
141
. post ( token_url. clone ( ) )
149
142
. form ( & serde_json:: json!( {
150
143
"grant_type" : "client_credentials" ,
0 commit comments