Skip to content

Commit fda35e1

Browse files
[Code Refactoring]: updated dependencies. Improved error handling in main.rs for better clarity
1 parent 9e8de33 commit fda35e1

4 files changed

Lines changed: 49 additions & 53 deletions

File tree

cli/Cargo.lock

Lines changed: 23 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ license = "Apache-2.0"
1111
readme = "../README.md"
1212

1313
[dependencies]
14-
clap = { version = "4.5.38", features = ["derive"] }
14+
clap = { version = "4.5.51", features = ["derive"] }
1515
colored = "3.0.0"
1616
directories = "6.0.0"
1717
serde = { version = "1.0.219", features = ["derive"] }
1818
tracing = "0.1.41"
1919
tokio = {version = "1.47.0",features = ["macros",'rt-multi-thread']}
20-
anyhow = "1.0.98"
21-
tonic = "0.14.1"
22-
tonic-reflection = "0.14.1"
20+
anyhow = "1.0.100"
21+
tonic = "0.14.2"
22+
tonic-reflection = "0.14.2"
2323
prost-types = "0.14.1"
2424
prost = "0.14.1"
2525
cortexflow_agent_api = {path = "../core/api",features = ["client"]}

cli/src/essential.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fmt::format;
21
use std::{ collections::BTreeMap, fmt, process::Command, result::Result::Ok };
32

43
use kube::core::ErrorResponse;
@@ -67,6 +66,11 @@ impl From<anyhow::Error> for CliError {
6766
CliError::MonitoringError { reason: format!("{}", e) }
6867
}
6968
}
69+
impl From<()> for CliError {
70+
fn from (v: ()) -> Self{
71+
return ().into()
72+
}
73+
}
7074

7175
// docs:
7276
// fmt::Display implementation for CliError type. Creates a user friendly message error message.

cli/src/main.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod service;
88
mod status;
99
mod uninstall;
1010

11-
use clap::command;
1211
use clap::{ Args, Parser, Subcommand };
1312
use colored::Colorize;
1413
use std::result::Result::Ok;
@@ -72,81 +71,66 @@ async fn args_parser() -> Result<(), CliError> {
7271
Some(Commands::Install(installation_args)) =>
7372
match installation_args.install_cmd {
7473
InstallCommands::All => {
75-
install_cortexflow().await?;
76-
Ok(())
74+
install_cortexflow().await.map_err(|e| eprintln!("{}",e) )?;
7775
}
7876
InstallCommands::TestPods => {
79-
install_simple_example().await?;
80-
Ok(())
77+
install_simple_example().await.map_err(|e| eprintln!("{}",e) )?;
8178
}
8279
}
8380
Some(Commands::Uninstall) => {
84-
uninstall().await?;
85-
Ok(())
81+
uninstall().await.map_err(|e| eprintln!("{}",e) )?;
8682
}
8783
Some(Commands::Update) => {
8884
update_cli();
89-
Ok(())
9085
}
9186
Some(Commands::Info) => {
9287
info();
93-
Ok(())
9488
}
9589
Some(Commands::Service(service_args)) =>
9690
match service_args.service_cmd {
9791
ServiceCommands::List { namespace } => {
98-
list_services(namespace).await?;
99-
Ok(())
92+
list_services(namespace).await.map_err(|e| eprintln!("{}",e) )?;
10093
}
10194
ServiceCommands::Describe { service_name, namespace } => {
102-
describe_service(service_name, &namespace).await?;
103-
Ok(())
95+
describe_service(service_name, &namespace).await.map_err(|e| eprintln!("{}",e) )?;
10496
}
10597
}
10698
Some(Commands::Status(status_args)) => {
107-
status_command(status_args.output, status_args.namespace).await?;
108-
Ok(())
99+
status_command(status_args.output, status_args.namespace).await.map_err(|e| eprintln!("{}",e) )?;
109100
}
110101
Some(Commands::Logs(logs_args)) => {
111-
logs_command(logs_args.service, logs_args.component, logs_args.namespace).await?;
112-
Ok(())
102+
logs_command(logs_args.service, logs_args.component, logs_args.namespace).await.map_err(|e| eprintln!("{}",e) )?;
113103
}
114104
Some(Commands::Monitor(monitor_args)) =>
115105
match monitor_args.monitor_cmd {
116106
MonitorCommands::List => {
117-
let _ = list_features().await?;
118-
Ok(())
107+
let _ = list_features().await.map_err(|e| eprintln!("{}",e) )?;
119108
}
120109
MonitorCommands::Connections => {
121-
let _ = monitor_identity_events().await?;
122-
Ok(())
110+
let _ = monitor_identity_events().await.map_err(|e| eprintln!("{}",e) )?;
123111
}
124112
}
125113
Some(Commands::Policies(policies_args)) => {
126114
match policies_args.policy_cmd {
127115
PoliciesCommands::CheckBlocklist => {
128-
let _ = check_blocklist().await?;
129-
Ok(())
116+
let _ = check_blocklist().await.map_err(|e| eprintln!("{}",e) )?;
130117
}
131118
PoliciesCommands::CreateBlocklist => {
132119
// pass the ip as a monitoring flag
133120
match policies_args.flags {
134121
None => {
135-
println!("{}", "Insert at least one ip to create a blocklist".red());
136-
Ok(())
122+
eprintln!("{}", "Insert at least one ip to create a blocklist".red());
137123
}
138124
Some(ip) => {
139125
println!("inserted ip: {} ", ip);
140126
//insert the ip in the blocklist
141127
match create_blocklist(&ip).await {
142128
Ok(_) => {
143129
//update the config metadata
144-
let _ = update_config_metadata(&ip, "add").await?;
145-
Ok(())
130+
let _ = update_config_metadata(&ip, "add").await.map_err(|e| eprintln!("{}",e) )?;
146131
}
147132
Err(e) => {
148-
println!("{}", e);
149-
Ok(())
133+
eprintln!("{}", e);
150134
}
151135
}
152136
}
@@ -155,22 +139,19 @@ async fn args_parser() -> Result<(), CliError> {
155139
PoliciesCommands::RemoveIpFromBlocklist =>
156140
match policies_args.flags {
157141
None => {
158-
println!(
142+
eprintln!(
159143
"{}",
160144
"Insert at least one ip to remove from the blocklist".red()
161145
);
162-
Ok(())
163146
}
164147
Some(ip) => {
165148
println!("Inserted ip: {}", ip);
166149
match remove_ip(&ip).await {
167150
Ok(_) => {
168-
let _ = update_config_metadata(&ip, "delete").await?;
169-
Ok(())
151+
let _ = update_config_metadata(&ip, "delete").await.map_err(|e| eprintln!("{}",e) )?;
170152
}
171153
Err(e) => {
172-
println!("{}", e);
173-
Ok(())
154+
eprintln!("{}", e);
174155
}
175156
}
176157
}
@@ -179,9 +160,9 @@ async fn args_parser() -> Result<(), CliError> {
179160
}
180161
None => {
181162
eprintln!("CLI unknown argument. Cli arguments passed: {:?}", args.cmd);
182-
Ok(())
183163
}
184164
}
165+
Ok(())
185166
}
186167

187168
#[tokio::main]

0 commit comments

Comments
 (0)