Skip to content

Commit 0032b9f

Browse files
[#171]: improved main.rs error structure
1 parent 68df671 commit 0032b9f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

cli/src/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ mod status;
99
mod uninstall;
1010

1111
use clap::{Args, Parser, Subcommand};
12-
use colored::Colorize;
1312
use std::result::Result::Ok;
1413
use tracing::debug;
1514

@@ -131,7 +130,9 @@ async fn args_parser() -> Result<(), CliError> {
131130
// pass the ip as a monitoring flag
132131
match policies_args.flags {
133132
None => {
134-
eprintln!("{}", "Insert at least one ip to create a blocklist".red());
133+
return Err(CliError::BaseError {
134+
reason: "Insert at least one ip to create a blocklist".to_string(),
135+
});
135136
}
136137
Some(ip) => {
137138
println!("inserted ip: {} ", ip);
@@ -142,18 +143,20 @@ async fn args_parser() -> Result<(), CliError> {
142143
let _ = update_config_metadata(&ip, "add").await?;
143144
}
144145
Err(e) => {
145-
eprintln!("{}", e);
146+
return Err(CliError::BaseError {
147+
reason: e.to_string(),
148+
});
146149
}
147150
}
148151
}
149152
}
150153
}
151154
PoliciesCommands::RemoveIpFromBlocklist => match policies_args.flags {
152155
None => {
153-
eprintln!(
154-
"{}",
155-
"Insert at least one ip to remove from the blocklist".red()
156-
);
156+
return Err(CliError::BaseError {
157+
reason: "Insert at least one ip to remove from the blocklist"
158+
.to_string(),
159+
});
157160
}
158161
Some(ip) => {
159162
println!("Inserted ip: {}", ip);
@@ -162,15 +165,19 @@ async fn args_parser() -> Result<(), CliError> {
162165
let _ = update_config_metadata(&ip, "delete").await?;
163166
}
164167
Err(e) => {
165-
eprintln!("{}", e);
168+
return Err(CliError::BaseError {
169+
reason: e.to_string(),
170+
});
166171
}
167172
}
168173
}
169174
},
170175
}
171176
}
172177
None => {
173-
eprintln!("CLI unknown argument. Cli arguments passed: {:?}", args.cmd);
178+
return Err(CliError::BaseError {
179+
reason: format!("CLI unknown argument. Cli arguments passed: {:?}", args.cmd),
180+
});
174181
}
175182
}
176183
Ok(())

0 commit comments

Comments
 (0)