feat(rust): implemented the config argument

This commit is contained in:
Reed Krantz
2026-02-18 21:37:41 -06:00
parent 2f6157b062
commit 2c47074a45
3 changed files with 39 additions and 8 deletions

View File

@@ -1,10 +1,11 @@
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use thiserror::Error;
use tracing::Level; use tracing::Level;
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[clap(version, about)] #[clap(version, about)]
pub struct CommandArgs { pub struct CommandArgs {
/// The path to the TOML configuration. This will override all other configuration sources /// The path to a TOML or JSON configuration to use. This will me merged with all other configuration sources, taking priority over other files but not environment variables
#[arg(short, long)] #[arg(short, long)]
pub config: Option<String>, pub config: Option<String>,
@@ -24,3 +25,9 @@ pub struct ServeAction {
#[arg(short, long, default_value_t = Level::WARN)] #[arg(short, long, default_value_t = Level::WARN)]
pub log_level: Level, pub log_level: Level,
} }
#[derive(Error, Debug)]
pub enum ArgsError {
#[error("the file extension of the provided config file is not one of .json or .toml")]
InvalidConfigExtension(Option<String>),
}

View File

@@ -1,10 +1,15 @@
use thiserror::Error; use thiserror::Error;
use crate::args;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum CommandError { pub enum CommandError {
#[error("the configuration is invalid")] #[error("the configuration is invalid")]
InvalidConfig(#[from] Box<figment::Error>), InvalidConfig(#[from] Box<figment::Error>),
#[error("a commandline argument is invalid")]
InvalidArgument(#[from] args::ArgsError),
#[error(transparent)] #[error(transparent)]
Unknown(#[from] anyhow::Error), Unknown(#[from] anyhow::Error),
// examples // examples

View File

@@ -1,3 +1,5 @@
use std::path::Path;
use clap::Parser; use clap::Parser;
use figment::{ use figment::{
Figment, Figment,
@@ -6,22 +8,37 @@ use figment::{
use tracing::{Level, event}; use tracing::{Level, event};
mod args; mod args;
use args::{ActionType, CommandArgs}; use crate::args::{ActionType, ArgsError, CommandArgs};
mod config; mod config;
mod error; mod error;
use error::CommandError; use crate::error::CommandError;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), CommandError> { async fn main() -> Result<(), CommandError> {
let args = CommandArgs::parse(); let args = CommandArgs::parse();
let config: config::CommandConfig = Figment::new() let figment = Figment::new()
.admerge(Json::file("config.json")) .admerge(Json::file("config.json"))
.admerge(Toml::file("config.toml")) .admerge(Toml::file("config.toml"));
.admerge(Env::prefixed("APP_"))
.extract()?; let config: config::CommandConfig = match &args.config {
None => figment,
Some(f) => {
let file = Path::new(&f);
match file.extension().and_then(|e| e.to_str()) {
Some("toml") => Ok(figment.admerge(Toml::file(file))),
Some("json") => Ok(figment.admerge(Json::file(file))),
_ => Err(ArgsError::InvalidConfigExtension(
file.extension().and_then(|e| e.to_str().map(String::from)),
)),
}
}?,
}
.admerge(Env::prefixed("APP_"))
.extract()?;
match &args.action { match &args.action {
ActionType::Serve(serve_args) => { ActionType::Serve(serve_args) => {
@@ -41,7 +58,9 @@ async fn main() -> Result<(), CommandError> {
println!("{:#?}", config.port.ports); println!("{:#?}", config.port.ports);
} }
} };
println!("{}", CommandError::InvalidArgument(ArgsError::InvalidConfigExtension(Some(String::from("txt")))));
Ok(()) Ok(())
} }