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 fe71ccd..4fb4a01 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: @@ -37,7 +36,6 @@ fn main() { println!("Uri: {}\nToken: {}", args.uri, args.token); - let output: dmi_collector::DmiInformation = dmi_collector::construct_dmi_information(); println!("{:#?}", output); @@ -45,7 +43,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)