Skip to content

Commit

Permalink
Setup logging conditionally
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Nov 20, 2023
1 parent 1d3b917 commit efafaa5
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ const SYSTEM_CONNECTIONS_DIR: &str = "/etc/NetworkManager/system-connections";
const HOST_MAPPING_FILE: &str = "host_config.yaml";

fn main() {
env_logger::init();

let app = clap::Command::new(APP_NAME)
.version(clap::crate_version!())
.about("Command line of NM configurator")
Expand Down Expand Up @@ -50,6 +48,12 @@ fn main() {
.help("Config dir containing host mapping ('host_config.yaml') \
and subdirectories containing *.nmconnection files per host")
)
.arg(
clap::Arg::new("VERBOSE")
.long("verbose")
.action(clap::ArgAction::SetTrue)
.help("Enables DEBUG log level")
)
);

let matches = app.get_matches();
Expand All @@ -63,6 +67,8 @@ fn main() {
.get_one::<String>("OUTPUT-DIR")
.expect("--output-dir is required");

setup_logger(cmd);

match generate(config_dir, output_dir) {
Ok(..) => {
info!("Successfully generated and stored network config");
Expand All @@ -78,6 +84,8 @@ fn main() {
.get_one::<String>("CONFIG-DIR")
.expect("--config-dir is required");

setup_logger(cmd);

match apply(config_dir, SYSTEM_CONNECTIONS_DIR) {
Ok(..) => {
info!("Successfully applied config");
Expand All @@ -91,3 +99,15 @@ fn main() {
_ => unreachable!("Unrecognized subcommand"),
}
}

fn setup_logger(matches: &clap::ArgMatches) {
let verbose_arg = "VERBOSE";

let mut log_builder = env_logger::Builder::new();
if matches.try_contains_id(verbose_arg).is_ok() && matches.get_flag(verbose_arg) {
log_builder.filter(None, log::LevelFilter::Debug);
} else {
log_builder.filter(None, log::LevelFilter::Info);
}
log_builder.init();
}

0 comments on commit efafaa5

Please sign in to comment.