Compare commits
7 Commits
94bad8e89f
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95c9a3f192
|
||
|
|
c822e1d2eb
|
||
|
|
2c47074a45
|
||
|
|
2f6157b062
|
||
|
|
4365e9f739
|
||
|
|
cf1b732808
|
||
|
|
301179ee4e
|
@@ -6,7 +6,13 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "*", features = ["full"] }
|
tokio = { version = "*", features = ["full"] }
|
||||||
serde = { version = "*", features = ["derive"] }
|
serde = { version = "*", features = ["derive"] }
|
||||||
|
either = { version = "*", features = ["serde"] }
|
||||||
|
|
||||||
thiserror = "*"
|
thiserror = "*"
|
||||||
clap = { version = "*", features = ["derive"] }
|
anyhow = "*"
|
||||||
tracing = "*"
|
tracing = "*"
|
||||||
tracing-subscriber = "*"
|
tracing-subscriber = "*"
|
||||||
|
|
||||||
|
clap = { version = "*", features = ["derive"] }
|
||||||
|
figment = { version = "*", features = ["toml", "env", "json"] }
|
||||||
|
|
||||||
|
|||||||
@@ -6,60 +6,57 @@
|
|||||||
|
|
||||||
flake-parts.url = "github:hercules-ci/flake-parts";
|
flake-parts.url = "github:hercules-ci/flake-parts";
|
||||||
|
|
||||||
|
treefmt-nix = {
|
||||||
|
url = "github:numtide/treefmt-nix";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
|
||||||
crane.url = "github:ipetkov/crane";
|
crane.url = "github:ipetkov/crane";
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = {
|
outputs = {
|
||||||
flake-parts,
|
flake-parts,
|
||||||
|
treefmt-nix,
|
||||||
crane,
|
crane,
|
||||||
...
|
...
|
||||||
} @ inputs:
|
} @ inputs:
|
||||||
flake-parts.lib.mkFlake {inherit inputs;} {
|
flake-parts.lib.mkFlake {inherit inputs;} {
|
||||||
|
imports = [
|
||||||
|
treefmt-nix.flakeModule
|
||||||
|
];
|
||||||
|
|
||||||
systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
|
systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
|
||||||
|
|
||||||
perSystem = {pkgs, ...}: let
|
perSystem = {
|
||||||
|
pkgs,
|
||||||
|
lib,
|
||||||
|
...
|
||||||
|
}: let
|
||||||
craneLib = crane.mkLib pkgs;
|
craneLib = crane.mkLib pkgs;
|
||||||
src = ./.;
|
src = craneLib.cleanCargoSource ./.;
|
||||||
|
manifest = (lib.importTOML "${src}/Cargo.toml").package;
|
||||||
|
|
||||||
|
# runtime dependencies
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
# openssl
|
||||||
|
];
|
||||||
|
|
||||||
|
# build dependencies
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
# pkg-config
|
||||||
|
];
|
||||||
|
|
||||||
# Build *just* the cargo dependencies, so we can reuse
|
# Build *just* the cargo dependencies, so we can reuse
|
||||||
# all of that work (e.g. via cachix) when running in CI
|
# all of that work (e.g. via cachix) when running in CI
|
||||||
cargoArtifacts = craneLib.buildDepsOnly {
|
cargoArtifacts = craneLib.buildDepsOnly {
|
||||||
inherit src;
|
inherit src buildInputs nativeBuildInputs;
|
||||||
};
|
};
|
||||||
in rec {
|
in rec {
|
||||||
# Formatter for nix files, available through 'nix fmt'
|
|
||||||
formatter = pkgs.alejandra;
|
|
||||||
|
|
||||||
# Your custom packages
|
|
||||||
# Accessible through 'nix build', 'nix shell', 'nix run', etc
|
|
||||||
packages = {
|
|
||||||
default = craneLib.buildPackage {
|
|
||||||
inherit src cargoArtifacts;
|
|
||||||
|
|
||||||
# runtime dependencies
|
|
||||||
# buildInputs = [];
|
|
||||||
|
|
||||||
# build dependencies
|
|
||||||
# nativeBuildInputs = [pkgs.pkg-config];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
checks = {
|
|
||||||
clippy = craneLib.cargoClippy {
|
|
||||||
inherit src cargoArtifacts;
|
|
||||||
cargoClippyExtraArgs = "-- --deny warnings";
|
|
||||||
};
|
|
||||||
|
|
||||||
fmt = craneLib.cargoFmt {
|
|
||||||
inherit src cargoArtifacts;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
# Dev Shell that lets you enter an environment with all the necessary utilites
|
# Dev Shell that lets you enter an environment with all the necessary utilites
|
||||||
# Available through 'nix develop'
|
# Available through 'nix develop'
|
||||||
# Also can be activated automatically if direnv is installed on the system with 'direnv allow'
|
# Also can be activated automatically if direnv is installed on the system with 'direnv allow'
|
||||||
devShells.default = craneLib.devShell {
|
devShells.default = craneLib.devShell {
|
||||||
inherit cargoArtifacts checks;
|
inherit checks;
|
||||||
|
|
||||||
# extra tooling dependencies
|
# extra tooling dependencies
|
||||||
packages = with pkgs; [
|
packages = with pkgs; [
|
||||||
@@ -68,6 +65,30 @@
|
|||||||
|
|
||||||
inputsFrom = [packages.default];
|
inputsFrom = [packages.default];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Formatter for nix files, available through 'nix fmt'
|
||||||
|
treefmt = {
|
||||||
|
programs.alejandra.enable = true;
|
||||||
|
programs.rustfmt = {
|
||||||
|
enable = true;
|
||||||
|
edition = manifest.edition;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
# Your custom packages
|
||||||
|
# Accessible through 'nix build', 'nix shell', 'nix run', etc
|
||||||
|
packages = {
|
||||||
|
default = craneLib.buildPackage {
|
||||||
|
inherit src buildInputs nativeBuildInputs cargoArtifacts;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
checks = {
|
||||||
|
clippy = craneLib.cargoClippy {
|
||||||
|
inherit src buildInputs nativeBuildInputs cargoArtifacts;
|
||||||
|
cargoClippyExtraArgs = "-- --deny warnings";
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>),
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,27 @@
|
|||||||
|
use either::{Either, Either::Left};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
struct CommandConfig {}
|
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) }
|
||||||
|
}
|
||||||
|
|||||||
26
rust/src/error.rs
Normal file
26
rust/src/error.rs
Normal file
@@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,45 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use figment::{
|
||||||
|
Figment,
|
||||||
|
providers::{Env, Format, Json, Toml},
|
||||||
|
};
|
||||||
use tracing::{Level, event};
|
use tracing::{Level, event};
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
use args::CommandArgs;
|
use crate::args::{ActionType, ArgsError, CommandArgs};
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
use crate::args::ActionType;
|
mod error;
|
||||||
|
use crate::error::CommandError;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() -> Result<(), CommandError> {
|
||||||
let args = CommandArgs::parse();
|
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 {
|
match &args.action {
|
||||||
ActionType::Serve(serve_args) => {
|
ActionType::Serve(serve_args) => {
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
@@ -26,7 +54,16 @@ async fn main() {
|
|||||||
|
|
||||||
println!("{:#?}", args);
|
println!("{:#?}", args);
|
||||||
|
|
||||||
// println!("{:#?}", config);
|
println!("{:#?}", config);
|
||||||
}
|
|
||||||
|
println!("{:#?}", config.port.ports);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
CommandError::InvalidArgument(ArgsError::InvalidConfigExtension(Some(String::from("txt"))))
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user