feat(rust): implemented the config argument
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::path::Path;
|
||||
|
||||
use clap::Parser;
|
||||
use figment::{
|
||||
Figment,
|
||||
@@ -6,22 +8,37 @@ use figment::{
|
||||
use tracing::{Level, event};
|
||||
|
||||
mod args;
|
||||
use args::{ActionType, CommandArgs};
|
||||
use crate::args::{ActionType, ArgsError, CommandArgs};
|
||||
|
||||
mod config;
|
||||
|
||||
mod error;
|
||||
use error::CommandError;
|
||||
use crate::error::CommandError;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), CommandError> {
|
||||
let args = CommandArgs::parse();
|
||||
|
||||
let config: config::CommandConfig = Figment::new()
|
||||
let figment = Figment::new()
|
||||
.admerge(Json::file("config.json"))
|
||||
.admerge(Toml::file("config.toml"))
|
||||
.admerge(Env::prefixed("APP_"))
|
||||
.extract()?;
|
||||
.admerge(Toml::file("config.toml"));
|
||||
|
||||
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 {
|
||||
ActionType::Serve(serve_args) => {
|
||||
@@ -41,7 +58,9 @@ async fn main() -> Result<(), CommandError> {
|
||||
|
||||
println!("{:#?}", config.port.ports);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
println!("{}", CommandError::InvalidArgument(ArgsError::InvalidConfigExtension(Some(String::from("txt")))));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user