Skip to content

Commit 1074087

Browse files
committed
fix(app): update cli path flags, add setup_logging, refactor code
Signed-off-by: Deep Panchal <[email protected]>
1 parent d4351b1 commit 1074087

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/main.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use regex::Regex;
99
use serde_json::Value;
1010
use std::error::Error;
1111
use std::hash::{Hash, Hasher};
12-
use std::path::PathBuf;
1312
use std::sync::Arc;
1413
use tokio::sync::Mutex;
1514
use tokio::task;
@@ -54,23 +53,23 @@ struct Args {
5453
env = "AWS_IOT_ROOT_CA_PATH",
5554
default_value = "./certs/AmazonRootCA1.pem"
5655
)]
57-
root_ca: PathBuf,
56+
root_ca_path: String,
5857

5958
/// Path to the device certificate
6059
#[arg(
6160
long,
6261
env = "AWS_IOT_DEVICE_CERT_PATH",
6362
default_value = "./certs/cert.crt"
6463
)]
65-
device_cert: PathBuf,
64+
device_cert_path: String,
6665

6766
/// Path to the device private key
6867
#[arg(
6968
long,
70-
env = "AWS_IOT_PRIVATE_KEY_PATH",
69+
env = "AWS_IOT_DEVICE_PRIVATE_KEY_PATH",
7170
default_value = "./certs/key.pem"
7271
)]
73-
private_key: PathBuf,
72+
device_private_key_path: String,
7473

7574
/// Enable verbose logging
7675
#[arg(short, long)]
@@ -107,12 +106,8 @@ enum CliCommand {
107106
},
108107
}
109108

110-
#[tokio::main]
111-
async fn main() -> Result<(), Box<dyn Error>> {
112-
let args = Args::parse();
113-
let mut cmd = Args::command();
114-
115-
if args.verbose {
109+
fn setup_logging(verbose: bool) {
110+
if verbose {
116111
env_logger::Builder::new()
117112
.filter_level(log::LevelFilter::Debug)
118113
.init();
@@ -121,11 +116,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
121116
.filter_level(log::LevelFilter::Info)
122117
.init();
123118
}
119+
}
124120

121+
#[tokio::main]
122+
async fn main() -> Result<(), Box<dyn Error>> {
123+
let args = Args::parse();
124+
let mut cmd = Args::command();
125+
126+
setup_logging(args.verbose);
125127
debug!("Parsed CLI arguments: {:?}", args);
126128

129+
let endpoint = args.endpoint;
130+
let port = args.port;
131+
let client_id = args.client_id;
132+
let root_ca_path = args.root_ca_path;
133+
let device_cert_path = args.device_cert_path;
134+
let device_private_key_path = args.device_private_key_path;
127135
let mqtt_option_overrides = MQTTOptionsOverrides {
128-
port: Some(args.port),
136+
port: Some(port),
129137
clean_session: Some(true),
130138
keep_alive: None,
131139
max_packet_size: None,
@@ -137,16 +145,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
137145
transport: None,
138146
};
139147
let aws_settings = AWSIoTSettings::new(
140-
args.client_id.clone(),
141-
args.root_ca.to_str().unwrap().to_string(),
142-
args.device_cert.to_str().unwrap().to_string(),
143-
args.private_key.to_str().unwrap().to_string(),
144-
args.endpoint.clone(),
148+
client_id.clone(),
149+
root_ca_path.clone(),
150+
device_cert_path.clone(),
151+
device_private_key_path.clone(),
152+
endpoint.clone(),
145153
Some(mqtt_option_overrides),
146154
);
147155

148-
debug!("Connecting with client_id: {}", args.client_id.blue());
149-
debug!("Using endpoint: {}", args.endpoint);
156+
debug!(
157+
"Connecting to {} with client_id: {}",
158+
endpoint.clone().blue(),
159+
client_id.clone().blue(),
160+
);
150161

151162
let (iot_core_client, (event_loop, sender)) = AWSIoTAsyncClient::new(aws_settings).await?;
152163
let raw_client = iot_core_client.get_client().await;

0 commit comments

Comments
 (0)