From 9887d5196f5d429df6d0f56b22f4f8f36403db62 Mon Sep 17 00:00:00 2001 From: askiiart Date: Thu, 15 May 2025 00:23:21 -0500 Subject: [PATCH] add example config and just overall improve the config data stuff --- src/cli.rs | 9 +++++-- src/data.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 41 +++++++------------------------- 3 files changed, 83 insertions(+), 35 deletions(-) create mode 100644 src/data.rs diff --git a/src/cli.rs b/src/cli.rs index 62cb03b..d5c25b1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,10 +1,15 @@ +use clap::{Parser, Subcommand}; use std::path::PathBuf; -use clap::Parser; - #[derive(Parser)] #[command(version, about, long_about = None)] pub(crate) struct Cli { #[arg(short, long, value_name = "FILE", default_value = "shoe.conf")] pub(crate) config: PathBuf, + /// Prints an example config + #[arg(long, action)] + pub(crate) print_example_config: bool, } + +#[derive(Subcommand)] +pub(crate) enum Commands {} diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..c715776 --- /dev/null +++ b/src/data.rs @@ -0,0 +1,68 @@ +use std::{collections::HashMap, path::PathBuf, str::FromStr}; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, PartialEq)] +struct SiteInfo { + name: String, + aliases: Vec, + /// Aliases for this subdomain - will have the same exact settings + /// The socket address (e.g. `192.168.1.8:8080`) or port number (if on `localhost`) of the service to reverse proxy + address: String, + /// What auth file to use - 0 means none + #[serde(skip_serializing_if = "Option::is_none")] + auth: Option, + #[serde(skip_serializing_if = "Option::is_none")] + conf_file: Option, +} + +#[derive(Serialize, Deserialize, Debug, PartialEq)] +struct Config { + /// What directory to output the conf files to + #[serde(default = "default_output_dir")] + output_dir: PathBuf, + domain: String, + /// Subdomains/services in the format `subdomain: SiteInfo` + subdomains: HashMap, + /// Paths to the htpasswd file for each auth number - starts at 1, goes to 255; see [`SiteInfo`] + #[serde(skip_serializing_if = "Option::is_none")] + htpasswd_files: Option>, + /// Default IP for if it's not specified in `socket_address_or_port` in [`SiteInfo`]. Defaults to localhost if not specified. + #[serde(default = "default_default_ip")] + default_ip: String, +} + +/// Generates the default +pub(crate) fn example_config() -> String { + let mut subdomains: HashMap = HashMap::new(); + subdomains.insert( + "qbittorrent".to_string(), + SiteInfo { + name: "Qbittorrent".to_string(), + aliases: vec!["qb".to_string()], + address: "6011".to_string(), + auth: None, + conf_file: None, + }, + ); + return serde_yml::to_string( + &(Config { + output_dir: default_output_dir(), + domain: "example.net".to_string(), + subdomains: subdomains, + htpasswd_files: None, + default_ip: default_default_ip(), + }), + ) + .unwrap(); +} + +// ----------------------------------------------------- DEFAULTS ----------------------------------------------------- + +fn default_output_dir() -> PathBuf { + return PathBuf::from_str("conf.d").unwrap(); +} + +fn default_default_ip() -> String { + return "localhost".to_string(); +} diff --git a/src/main.rs b/src/main.rs index 64b15bb..0d4b6c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,9 @@ use clap::Parser; -use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, path::PathBuf}; +use cli::Commands; +use std::{collections::HashMap, path::PathBuf, process::exit}; mod build; mod cli; - -#[derive(Serialize, Deserialize, Debug, PartialEq)] -struct SiteInfo { - name: String, - subdomain: String, - /// - socket_address_or_port: String, - /// What auth file to use - auth: u8, - conf_file: PathBuf, - -} - -#[derive(Serialize, Deserialize, Debug, PartialEq)] -struct Config { - output_dir: String, - domain: String, - /// Subdomains/services in the format `subdomain: SiteInfo` - subdomains: HashMap, - /// Paths to the htpasswd file for each auth number - starts at 0, goes to 255; see [`SiteInfo`] - htpasswd_files: Vec, - /// Default IP for if it's not specified in `socket_address_or_port` in [`SiteInfo`]. Defaults to localhost if not specified. - #[serde(default = "default_default_ip")] - default_ip: String -} +mod data; fn main() { build::completion_builder().unwrap(); @@ -36,11 +12,10 @@ fn main() { let config = args.config; + if args.print_example_config { + println!("{}", data::example_config()); + exit(0); + } + println!("Config: {}", config.to_str().unwrap()) } - -// --------------------------------------------------------------------------------- - -fn default_default_ip() -> String { - return "localhost".to_string(); -} \ No newline at end of file