Skip to content

Commit

Permalink
make console arguments optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Sep 7, 2023
1 parent 3380816 commit 3da3485
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
52 changes: 35 additions & 17 deletions src/configuration/config_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,61 @@ pub struct ConfigData {
/// * 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;
pub fn set_up_configuration(
uri: Option<String>,
token: Option<String>,
) -> Result<ConfigData, String> {
let mut conf_data: ConfigData;

println!("Checking for existing configuration file...");

if file_exists(&get_config_dir()) {
println!("Configuration file already exists. Validating...");
// TODO Rewrite validation logic to properly condition here
match ConfigData::validate_config_file() {
Ok(_) => {
println!("Configuration file valid. Loading defaults...");
conf_data = ConfigData::read_config_file();

if uri.is_some() {
conf_data.netbox_uri = uri.unwrap();
}

if token.is_some() {
conf_data.netbox_api_token = token.unwrap();
}

return Ok(conf_data);
}
Err(err) => return Err(err),
}
} else {
println!("No config file found. Creating default...");
}

match ConfigData::initialize_config_file() {
Ok(_) => {
println!("\x1b[32mDefault configuration file created successfully.\x1b[0m")
}
Err(_) => {
panic!("FATAL: An error occurred while initializing the config!")
}
}
println!("No config file found. Creating default...");

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."
)
match ConfigData::initialize_config_file() {
Ok(_) => {
println!("\x1b[32mDefault configuration file created successfully.\x1b[0m")
}
Err(_) => {
panic!("FATAL: An error occurred while initializing the config!")
}
}

if uri.is_none() || token.is_none() {
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();

if uri.is_some() && token.is_some() {
conf_data.netbox_uri = uri.unwrap();
conf_data.netbox_api_token = token.unwrap();
}

println!("\x1b[32mConfiguration loaded.\x1b[0m");
Ok(conf_data)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ use configuration::config_parser::set_up_configuration;
struct Args {
/// URI to your Netbox instance
#[arg(short, long)]
uri: String,
uri: Option<String>,

/// Your API authentication token
#[arg(short, long)]
token: String,
token: Option<String>,
}

fn main() {
let args: Args = Args::parse();

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

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

0 comments on commit 3da3485

Please sign in to comment.