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 @@
|
||||
use flake
|
||||
@@ -0,0 +1,30 @@
|
||||
name: "Flake.lock: update Nix dependencies for development environment only"
|
||||
|
||||
on:
|
||||
workflow_dispatch: # allows manual triggering
|
||||
schedule:
|
||||
- cron: '00 00 02 1/2 *' # runs the first of day the month every 2 months at 7:00pm local time (midnight UTC)
|
||||
|
||||
jobs:
|
||||
nix-flake-update:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: "install nix"
|
||||
uses: cachix/install-nix-action@v31
|
||||
|
||||
- name: "setup git user"
|
||||
uses: fregante/setup-git-user@v2
|
||||
|
||||
- name: "update flake.lock"
|
||||
run: |
|
||||
nix flake update --commit-lock-file
|
||||
|
||||
- name: "check flake for errors"
|
||||
run: |
|
||||
nix flake check
|
||||
|
||||
- name: "push update"
|
||||
run: |
|
||||
git push
|
||||
@@ -0,0 +1,11 @@
|
||||
# Generated by nix
|
||||
.direnv/
|
||||
result
|
||||
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "*", features = ["full"] }
|
||||
serde = { version = "*", features = ["derive"] }
|
||||
either = { version = "*", features = ["serde"] }
|
||||
|
||||
thiserror = "*"
|
||||
anyhow = "*"
|
||||
tracing = "*"
|
||||
tracing-subscriber = "*"
|
||||
|
||||
clap = { version = "*", features = ["derive"] }
|
||||
figment = { version = "*", features = ["toml", "env", "json"] }
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
description = "Rust tool";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
|
||||
flake-parts.url = "github:hercules-ci/flake-parts";
|
||||
|
||||
project-templates = {
|
||||
url = "git+ssh://gitea@git.krantz.one/reed/project-templates";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
inputs.flake-parts.follows = "flake-parts";
|
||||
};
|
||||
};
|
||||
|
||||
outputs = {
|
||||
flake-parts,
|
||||
project-templates,
|
||||
...
|
||||
} @ inputs:
|
||||
flake-parts.lib.mkFlake {inherit inputs;} {
|
||||
imports = [
|
||||
project-templates.flakeModules.rust
|
||||
];
|
||||
|
||||
systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin"];
|
||||
|
||||
perSystem = {pkgs, ...}: {
|
||||
krantz.rust = {
|
||||
enable = true;
|
||||
src = ./.;
|
||||
|
||||
# runtimeDeps = with pkgs; [];
|
||||
# buildDeps = with pkgs; [];
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -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