From 3256a323198e7afee430d375b64e225416b5264c Mon Sep 17 00:00:00 2001 From: ByteOtter Date: Mon, 4 Sep 2023 13:46:13 +0200 Subject: [PATCH] link cli to config parsing --- src/configuration/config_parser.rs | 26 +++++++++++++++++++++++++- src/main.rs | 4 +--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/configuration/config_parser.rs b/src/configuration/config_parser.rs index 0edecd9..9df13c2 100644 --- a/src/configuration/config_parser.rs +++ b/src/configuration/config_parser.rs @@ -27,7 +27,24 @@ pub struct ConfigData { } /// Set up configuration -pub fn set_up_configuration() -> Result { +/// +/// This function reads the configuration file located at `~/.nbs-config.toml`. If no file can be found, a warning is +/// displayed to the user and a default config file is written. +/// If command line arguments are given, the parameters read from the file will be overwritten. +/// +/// # Returns +/// +/// * `Ok(ConfigData)` - A `ConfigData` object containing the netbox URI and API token. +/// * `Err` - Prints an Error if the file cannot be validated. +/// +/// # Panics +/// +/// The function panics under these condition: +/// +/// * If the initialization of the config file raises an error. +/// * When using a default (empty) configuration file and not providing all required CLI arguments. +/// * If the configuration file cannot be read. +pub fn set_up_configuration(uri: String, token: String) -> Result { let conf_data: ConfigData; println!("Checking for existing configuration file..."); @@ -53,6 +70,13 @@ pub fn set_up_configuration() -> Result { panic!("FATAL: An error occurred while initializing the config!") } } + + if uri.is_empty() || token.is_empty() { + panic!( + "FATAL: No configuration parameters found in CLI while using an empty config file!\n + Please enter valid configuration parameters in the configuration file or provide them via the CLI." + ) + } } conf_data = ConfigData::read_config_file(); diff --git a/src/main.rs b/src/main.rs index 75e4c02..8ed62e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use clap::Parser; use collectors::{dmi_collector, network_collector}; use configuration::config_parser::set_up_configuration; - /// The arguments that netbox-sync expects to get via the cli. /// /// Arguments can be passed like this: @@ -34,7 +33,6 @@ fn main() { println!("Uri: {}\nToken: {}", args.uri, args.token); - let output: dmi_collector::DmiInformation = dmi_collector::construct_dmi_information(); println!("{:#?}", output); @@ -42,7 +40,7 @@ fn main() { println!("{:#?}", output2); - let config = match set_up_configuration() { + let config = match set_up_configuration(args.uri, args.token) { Ok(conf) => conf, Err(err) => { panic!("{}", err)