@@ -3,6 +3,8 @@ use aws_iot_device_sdk_rust::{
3
3
} ;
4
4
use clap:: { Parser , Subcommand } ;
5
5
use colored:: * ;
6
+ use env_logger;
7
+ use log:: debug;
6
8
use regex:: Regex ;
7
9
use serde_json:: Value ;
8
10
use std:: error:: Error ;
@@ -35,17 +37,41 @@ struct Args {
35
37
#[ command( subcommand) ]
36
38
command : Option < CliCommand > ,
37
39
40
+ /// AWS IoT endpoint URL
41
+ #[ arg( long, env = "AWS_IOT_ENDPOINT" ) ]
42
+ endpoint : String ,
43
+
44
+ /// Client ID for MQTT connection
45
+ #[ arg( long, env = "AWS_IOT_CLIENT_ID" ) ]
46
+ client_id : String ,
47
+
38
48
/// Path to the root CA certificate
39
- #[ arg( long, default_value = "./certs/AmazonRootCA1.pem" ) ]
49
+ #[ arg(
50
+ long,
51
+ env = "AWS_IOT_ROOT_CA_PATH" ,
52
+ default_value = "./certs/AmazonRootCA1.pem"
53
+ ) ]
40
54
root_ca : PathBuf ,
41
55
42
56
/// Path to the device certificate
43
- #[ arg( long, default_value = "./certs/cert.crt" ) ]
57
+ #[ arg(
58
+ long,
59
+ env = "AWS_IOT_DEVICE_CERT_PATH" ,
60
+ default_value = "./certs/cert.crt"
61
+ ) ]
44
62
device_cert : PathBuf ,
45
63
46
64
/// Path to the device private key
47
- #[ arg( long, default_value = "./certs/key.pem" ) ]
65
+ #[ arg(
66
+ long,
67
+ env = "AWS_IOT_PRIVATE_KEY_PATH" ,
68
+ default_value = "./certs/key.pem"
69
+ ) ]
48
70
private_key : PathBuf ,
71
+
72
+ /// Enable verbose logging
73
+ #[ arg( short, long) ]
74
+ verbose : bool ,
49
75
}
50
76
51
77
/// Subcommands for the CLI
@@ -82,25 +108,30 @@ enum CliCommand {
82
108
async fn main ( ) -> Result < ( ) , Box < dyn Error > > {
83
109
let args = Args :: parse ( ) ;
84
110
85
- let endpoint = std:: env:: var ( "AWS_IOT_ENDPOINT" ) . expect ( "AWS_IOT_ENDPOINT not set" ) ;
86
- let client_id = std:: env:: var ( "AWS_IOT_CLIENT_ID" ) . expect ( "AWS_IOT_CLIENT_ID not set" ) ;
87
- let root_ca_path = args. root_ca . to_str ( ) . unwrap ( ) . to_string ( ) ;
88
- let device_cert_path = args. device_cert . to_str ( ) . unwrap ( ) . to_string ( ) ;
89
- let private_key_path = args. private_key . to_str ( ) . unwrap ( ) . to_string ( ) ;
90
- println ! (
91
- "{}" ,
92
- format!( "Connecting with client_id: {}" , client_id) . blue( )
93
- ) ;
111
+ if args. verbose {
112
+ env_logger:: Builder :: new ( )
113
+ . filter_level ( log:: LevelFilter :: Debug )
114
+ . init ( ) ;
115
+ } else {
116
+ env_logger:: Builder :: new ( )
117
+ . filter_level ( log:: LevelFilter :: Info )
118
+ . init ( ) ;
119
+ }
120
+
121
+ debug ! ( "Parsed CLI arguments: {:?}" , args) ;
94
122
95
123
let aws_settings = AWSIoTSettings :: new (
96
- client_id,
97
- root_ca_path ,
98
- device_cert_path ,
99
- private_key_path ,
100
- endpoint,
124
+ args . client_id . clone ( ) ,
125
+ args . root_ca . to_str ( ) . unwrap ( ) . to_string ( ) ,
126
+ args . device_cert . to_str ( ) . unwrap ( ) . to_string ( ) ,
127
+ args . private_key . to_str ( ) . unwrap ( ) . to_string ( ) ,
128
+ args . endpoint . clone ( ) ,
101
129
None ,
102
130
) ;
103
131
132
+ debug ! ( "Connecting with client_id: {}" , args. client_id. blue( ) ) ;
133
+ debug ! ( "Using endpoint: {}" , args. endpoint) ;
134
+
104
135
let ( iot_core_client, event_loop) = AWSIoTAsyncClient :: new ( aws_settings) . await ?;
105
136
let iot_core_client = Arc :: new ( Mutex :: new ( iot_core_client) ) ;
106
137
0 commit comments