added config file for some options

This commit is contained in:
2025-11-10 16:28:55 +01:00
parent 0ae02c8950
commit 88dc0e47b1
9 changed files with 457 additions and 62 deletions

View File

@@ -3,7 +3,7 @@ use std::io;
use clap::CommandFactory;
use clap_complete::{generate, shells::{Bash, Fish, Zsh}};
use crate::app::args::structure::{Args, ShellArg};
use crate::app::config::arguments::{Args, ShellArg};
pub fn completions(shell: ShellArg) {
let mut command = Args::command();

View File

@@ -10,7 +10,7 @@ use std::{env, path::Path};
use crate::app::{ watcher::watcher, build::{docki_build, DockiBuildResult}, commands::build::build, log::display_status};
pub async fn serve(port: Option<u16>) {
pub async fn serve(port: u16) {
build(false).await;
tokio::join!(watch_and_build(), start_server(port));
}
@@ -22,8 +22,7 @@ async fn watch_and_build() {
}
async fn start_server(port: Option<u16>) {
let port = port.unwrap_or(8080);
async fn start_server(port: u16) {
let link = &format!("http://localhost:{}", port);
let hyperlink = Green.paint(link).hyperlink(link);

View File

@@ -1,9 +1,13 @@
use super::config::Config;
use clap::{Parser, Subcommand};
#[derive(Parser)]
pub struct Args {
#[command(subcommand)]
pub command: CommandArg,
#[arg(short, long)]
pub docs_dir: Option<String>,
}
#[derive(Subcommand)]
@@ -17,8 +21,8 @@ pub enum ShellArg {
pub enum CommandArg {
/// Builds the documentation into a dist folder
Build {
/// When set to true, docki will download revealjs before building the documentation.
/// Otherwise it will use the cdn for revealjs
/// When set to true, docki will download revealjs before building the documentation.
/// Otherwise it will use the cdn for revealjs
#[arg(short, long)]
offline_reveal: bool,
},
@@ -38,3 +42,5 @@ pub enum CommandArg {
shell: ShellArg,
},
}
impl Args {}

48
src/app/config/config.rs Normal file
View File

@@ -0,0 +1,48 @@
use serde::Deserialize;
use crate::app::config::arguments::CommandArg;
#[derive(Deserialize, Debug)]
#[serde(default)]
pub struct Config {
pub port: u16,
pub docs_dir: String,
pub offline_reveal: bool,
}
impl Config {
pub fn load() -> Result<Self, config::ConfigError> {
let s = config::Config::builder()
.add_source(config::File::with_name("./docki.config.toml"))
.build()?;
s.try_deserialize()
}
pub fn merge_with_args(self, args: &super::arguments::Args) -> Self {
Self {
port: match args.command {
CommandArg::Serve { port } => port.unwrap_or(self.port),
_ => self.port,
},
docs_dir: args.docs_dir.clone().unwrap_or(self.docs_dir),
offline_reveal: {
if let CommandArg::Build { offline_reveal } = args.command {
offline_reveal
} else {
self.offline_reveal
}
}
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
port: 8080,
docs_dir: "./docs".to_string(),
offline_reveal: false,
}
}
}

View File

@@ -1,10 +1,10 @@
use clap::Parser;
use self::structure::Args;
use self::arguments::Args;
pub mod structure;
pub mod arguments;
pub mod config;
pub fn args() -> Args {
return Args::parse();
}

View File

@@ -3,11 +3,13 @@ pub mod build;
pub mod fs_util;
pub mod watcher;
pub mod log;
mod args;
mod config;
use std::env;
use self::args::{args, structure::CommandArg};
use crate::app::config::config::Config;
use self::config::{args, arguments::CommandArg};
use self::commands::build::build;
use self::commands::completions::completions;
use self::commands::health::health;
@@ -20,13 +22,14 @@ impl App {
pub async fn start(&self) {
let args = args();
let config = Config::load().unwrap_or(Config::default()).merge_with_args(&args);
Self::setup_environment_variables();
match args.command {
CommandArg::Build { offline_reveal } => build(offline_reveal).await,
CommandArg::Build { .. } => build(config.offline_reveal).await,
CommandArg::Health => health(),
CommandArg::InstallReveal => install_reveal().await,
CommandArg::Serve { port } => serve(port).await,
CommandArg::Serve { .. } => serve(config.port).await,
CommandArg::Completions { shell } => completions(shell)
};
}