Skip to content

Commit f64d175

Browse files
committed
feat(app): allow setting aws iot settings w/ env vars & cli flags, add verbose logging
Signed-off-by: Deep Panchal <[email protected]>
1 parent 705314c commit f64d175

File tree

3 files changed

+83
-19
lines changed

3 files changed

+83
-19
lines changed

Cargo.lock

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ edition = "2021"
66
[dependencies]
77
aws-iot-device-sdk-rust = "0.6.0"
88
chrono = "0.4.40"
9-
clap = { version = "4.5.31", features = ["derive"] }
9+
clap = { version = "4.5.31", features = ["derive", "env"] }
1010
colored = "3.0.0"
1111
regex = "1.11.1"
1212
serde_json = "1.0.140"
1313
term_size = "0.3.2"
1414
tokio = "1.44.0"
15+
env_logger = "0.11.6"
16+
log = "0.4.26"

src/main.rs

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use aws_iot_device_sdk_rust::{
33
};
44
use clap::{Parser, Subcommand};
55
use colored::*;
6+
use env_logger;
7+
use log::debug;
68
use regex::Regex;
79
use serde_json::Value;
810
use std::error::Error;
@@ -35,17 +37,41 @@ struct Args {
3537
#[command(subcommand)]
3638
command: Option<CliCommand>,
3739

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+
3848
/// 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+
)]
4054
root_ca: PathBuf,
4155

4256
/// 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+
)]
4462
device_cert: PathBuf,
4563

4664
/// 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+
)]
4870
private_key: PathBuf,
71+
72+
/// Enable verbose logging
73+
#[arg(short, long)]
74+
verbose: bool,
4975
}
5076

5177
/// Subcommands for the CLI
@@ -82,25 +108,30 @@ enum CliCommand {
82108
async fn main() -> Result<(), Box<dyn Error>> {
83109
let args = Args::parse();
84110

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);
94122

95123
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(),
101129
None,
102130
);
103131

132+
debug!("Connecting with client_id: {}", args.client_id.blue());
133+
debug!("Using endpoint: {}", args.endpoint);
134+
104135
let (iot_core_client, event_loop) = AWSIoTAsyncClient::new(aws_settings).await?;
105136
let iot_core_client = Arc::new(Mutex::new(iot_core_client));
106137

0 commit comments

Comments
 (0)