refactor: new directory structure where the flake module lives in a directory with the template in a subdirectory
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use thiserror::Error;
|
||||
use tracing::Level;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(version, about)]
|
||||
pub struct CommandArgs {
|
||||
/// 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)]
|
||||
pub config: Option<String>,
|
||||
|
||||
#[clap(subcommand)]
|
||||
pub action: ActionType,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum ActionType {
|
||||
/// Run the daemon and listen for requests
|
||||
Serve(ServeAction),
|
||||
}
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct ServeAction {
|
||||
/// The log level to use for the server
|
||||
#[arg(short, long, default_value_t = Level::WARN)]
|
||||
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>),
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use either::{Either, Either::Left};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct CommandConfig {
|
||||
/// The port(s) to listen on.
|
||||
///
|
||||
/// If you are using Docker, don't change this, you'll need to map an
|
||||
/// external port to this.
|
||||
///
|
||||
/// To listen on multiple ports, specify a vector e.g. [8080, 8448]
|
||||
///
|
||||
/// default: 8008
|
||||
#[serde(default = "default_port")]
|
||||
pub port: ListeningPort,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
#[serde(transparent)]
|
||||
pub struct ListeningPort {
|
||||
#[serde(with = "either::serde_untagged")]
|
||||
pub ports: Either<u16, Vec<u16>>,
|
||||
}
|
||||
|
||||
fn default_port() -> ListeningPort {
|
||||
ListeningPort { ports: Left(8008) }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::args;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum CommandError {
|
||||
#[error("the configuration is invalid")]
|
||||
InvalidConfig(#[from] Box<figment::Error>),
|
||||
|
||||
#[error("a commandline argument is invalid")]
|
||||
InvalidArgument(#[from] args::ArgsError),
|
||||
|
||||
#[error(transparent)]
|
||||
Unknown(#[from] anyhow::Error),
|
||||
// examples
|
||||
// #[error("the data for key `{0}` is not available")]
|
||||
// Redaction(String),
|
||||
// #[error("invalid header (expected {expected:?}, found {found:?})")]
|
||||
// InvalidHeader { expected: String, found: String },
|
||||
}
|
||||
|
||||
impl From<figment::Error> for CommandError {
|
||||
fn from(error: figment::Error) -> Self {
|
||||
CommandError::InvalidConfig(Box::new(error))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
use std::path::Path;
|
||||
|
||||
use clap::Parser;
|
||||
use figment::{
|
||||
Figment,
|
||||
providers::{Env, Format, Json, Toml},
|
||||
};
|
||||
use tracing::{Level, event};
|
||||
|
||||
mod args;
|
||||
use crate::args::{ActionType, ArgsError, CommandArgs};
|
||||
|
||||
mod config;
|
||||
|
||||
mod error;
|
||||
use crate::error::CommandError;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), CommandError> {
|
||||
let args = CommandArgs::parse();
|
||||
|
||||
let figment = Figment::new()
|
||||
.admerge(Json::file("config.json"))
|
||||
.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) => {
|
||||
tracing_subscriber::fmt()
|
||||
.with_max_level(serve_args.log_level)
|
||||
.init();
|
||||
|
||||
event!(Level::ERROR, "error");
|
||||
event!(Level::WARN, "warn");
|
||||
event!(Level::INFO, "info");
|
||||
event!(Level::DEBUG, "debug");
|
||||
event!(Level::TRACE, "trace");
|
||||
|
||||
println!("{:#?}", args);
|
||||
|
||||
println!("{:#?}", config);
|
||||
|
||||
println!("{:#?}", config.port.ports);
|
||||
}
|
||||
};
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
CommandError::InvalidArgument(ArgsError::InvalidConfigExtension(Some(String::from("txt"))))
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user