Skip to content

Commit

Permalink
Merge pull request #11 from timkolloch/features/command-line-parameters
Browse files Browse the repository at this point in the history
Added command line parameter parsing
  • Loading branch information
timkolloch authored Aug 15, 2024
2 parents bbe383a + 6b72018 commit 54b90a6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
55 changes: 54 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tandoor_importer"
version = "0.1.0"
version = "1.2.0"
edition = "2021"

[dependencies]
Expand All @@ -9,4 +9,5 @@ serde_json = "1.0"
reqwest = { version = "0.12", features = ["json", "blocking"] }
regex = "1.10.5"
log = "0.4.21"
env_logger = "0.11.5"
env_logger = "0.11.5"
clap = { version = "4.5.15", features = ["derive"] }
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use regex::Regex;
use reqwest::blocking::{Client};
use log::{debug, info, warn, error, trace};
use env_logger;
use clap::Parser;

mod models;
use models::configuration::Configuration;
Expand All @@ -22,12 +23,18 @@ use models::tandoor::api_tandoor_food::ApiTandoorFood;
use models::tandoor::api_tandoor_food_property::ApiTandoorFoodProperty;
use models::usda::usda_food::USDAFood;
use models::usda::usda_api_response::USDAApiResponse;
use models::command_line_arguments::Args;

fn main(){
// Create clint for api requests.

// Get command line arguments.
let args = Args::parse();

// Create client for api requests.
let client = Client::new();

env_logger::init();
// Initialize logger (with set log level for the crate
env_logger::Builder::new().filter(Some(env!("CARGO_PKG_NAME")), args.log_level.into()).init();

// Read app settings
let app_settings = fs::read_to_string("./appsettings.json").expect("The appsettings were not loaded successfully.");
Expand Down
42 changes: 42 additions & 0 deletions src/models/command_line_arguments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Holds all the command line parameters and the types associated with them
use clap::Parser;
use log::LevelFilter;

/// Struct containing all possible command line parameters.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args{
/// Override existing properties
#[arg(short, long="override", help = "When set the program overrides already present properties.")]
pub override_properties: bool,

/// Interactive mode
#[arg(short, long, help = "When set the program asks the user to enter a FDC ID when none was found.")]
pub interactive: bool,

/// Log level
#[arg(short, long, default_value = "info", help = "Sets the log level.",)]
pub log_level: LogLevel,
}

/// Possible log levels.
#[derive(clap::ValueEnum, Clone, Debug)]
pub enum LogLevel{
Trace,
Debug,
Info,
Warning,
Error
}

impl Into<LevelFilter> for LogLevel{
fn into(self) -> LevelFilter {
match self {
LogLevel::Trace => LevelFilter::Trace,
LogLevel::Debug => LevelFilter::Debug,
LogLevel::Info => LevelFilter::Info,
LogLevel::Warning => LevelFilter::Warn,
LogLevel::Error => LevelFilter::Error
}
}
}
3 changes: 2 additions & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Models to hold objects retrieved from Tandoor or the USDA FDC database and models used to push objects to Tandoor.
pub mod tandoor;
pub mod usda;
pub mod configuration;
pub mod configuration;
pub mod command_line_arguments;

0 comments on commit 54b90a6

Please sign in to comment.