Skip to content

Commit

Permalink
add basic cli
Browse files Browse the repository at this point in the history
link cli to config parsing
  • Loading branch information
ByteOtter committed Sep 7, 2023
1 parent 602c842 commit c70c096
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.2", features = ["derive"] }
network-interface = "1.0.1"
serde = { version = "1.0.188", features = ["derive"] }
toml = "0.7.6"
Expand Down
66 changes: 54 additions & 12 deletions src/configuration/config_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,78 @@ pub struct ConfigData {
}

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

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

if uri.is_none() || token.is_none() {
panic!(
"x1b[31mFATAL:x1b[0m 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
38 changes: 35 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
mod collectors;
pub mod configuration;

use clap::Parser;
use collectors::{dmi_collector, network_collector};
use configuration::config_parser::set_up_configuration;
use configuration::config_parser::{set_up_configuration, ConfigData};

/// The arguments that netbox-sync expects to get via the cli.
///
/// Arguments can be passed like this:
///
/// ```
/// netbox-sync --uri <NETBOX_URI> --token <NETBOX_TOKEN>
/// ```
///
/// These arguments override the ones defined in the `.nbs-config.toml`.
///
/// # Members
///
/// * `uri: String` - The URI to your Netbox instance.
/// * `token: String` - The authentication token for the Netbox API.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about=None)]
struct Args {
/// URI to your Netbox instance
#[arg(short, long)]
uri: Option<String>,

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

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

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

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

let output2 = network_collector::construct_network_information().unwrap();
let output2: Vec<network_collector::NetworkInformation> =
network_collector::construct_network_information().unwrap();

println!("{:#?}", output2);

let config = match set_up_configuration() {
let config: ConfigData = match set_up_configuration(args.uri, args.token) {
Ok(conf) => conf,
Err(err) => {
panic!("{}", err)
Expand Down

0 comments on commit c70c096

Please sign in to comment.