Skip to content

Commit

Permalink
link cli to config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Sep 7, 2023
1 parent ea7c7e7 commit 3380816
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/configuration/config_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@ pub struct ConfigData {
}

/// Set up configuration
pub fn set_up_configuration() -> Result<ConfigData, String> {
///
/// 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<ConfigData, String> {
let conf_data: ConfigData;

println!("Checking for existing configuration file...");
Expand All @@ -53,6 +70,13 @@ pub fn set_up_configuration() -> Result<ConfigData, String> {
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();
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -37,15 +36,14 @@ fn main() {

println!("Uri: {}\nToken: {}", args.uri, args.token);


let output: dmi_collector::DmiInformation = dmi_collector::construct_dmi_information();
println!("{:#?}", output);

let output2 = network_collector::construct_network_information().unwrap();

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)
Expand Down

0 comments on commit 3380816

Please sign in to comment.