Skip to main content

hyprstream_core/cli/commands/
config.rs

1use clap::Args;
2use serde::Deserialize;
3
4/// Trait for configuration options that can be set via CLI, env vars, or config file
5pub trait ConfigOption {
6    /// Get the environment variable name for this option
7    fn env_var(&self) -> Option<&'static str>;
8
9    /// Get the long CLI flag for this option
10    fn cli_flag(&self) -> Option<&'static str>;
11
12    /// Get the short CLI flag for this option
13    fn cli_short(&self) -> Option<char>;
14
15    /// Get the config file path for this option (e.g., "server.host")
16    fn config_path(&self) -> &'static str;
17
18    /// Get the help text for this option
19    fn help_text(&self) -> &'static str;
20}
21
22/// A configuration option with its metadata
23#[derive(Debug, Clone)]
24pub struct ConfigOptionDef<T> {
25    /// The current value of the option
26    pub value: Option<T>,
27    /// The environment variable name
28    pub env_var: Option<&'static str>,
29    /// The long CLI flag
30    pub cli_flag: Option<&'static str>,
31    /// The short CLI flag
32    pub cli_short: Option<char>,
33    /// The config file path
34    pub config_path: &'static str,
35    /// The help text
36    pub help_text: &'static str,
37}
38
39impl<T> ConfigOptionDef<T> {
40    pub fn new(config_path: &'static str, help_text: &'static str) -> Self {
41        Self {
42            value: None,
43            env_var: None,
44            cli_flag: None,
45            cli_short: None,
46            config_path,
47            help_text,
48        }
49    }
50
51    pub fn with_env(mut self, env_var: &'static str) -> Self {
52        self.env_var = Some(env_var);
53        self
54    }
55
56    pub fn with_cli(mut self, flag: &'static str) -> Self {
57        self.cli_flag = Some(flag);
58        self
59    }
60
61    pub fn with_short(mut self, short: char) -> Self {
62        self.cli_short = Some(short);
63        self
64    }
65}
66
67impl<T> ConfigOption for ConfigOptionDef<T> {
68    fn env_var(&self) -> Option<&'static str> {
69        self.env_var
70    }
71
72    fn cli_flag(&self) -> Option<&'static str> {
73        self.cli_flag
74    }
75
76    fn cli_short(&self) -> Option<char> {
77        self.cli_short
78    }
79
80    fn config_path(&self) -> &'static str {
81        self.config_path
82    }
83
84    fn help_text(&self) -> &'static str {
85        self.help_text
86    }
87}
88
89/// Trait for configuration sections that can be loaded from multiple sources
90pub trait ConfigSection: Sized + Default {
91    /// Get all configuration options for this section
92    fn options() -> Vec<ConfigOptionDef<String>>;
93
94    /// Create a new instance from a config file section
95    fn from_config<'de, D>(deserializer: D) -> Result<Self, D::Error>
96    where
97        D: serde::Deserializer<'de>;
98}
99
100/// Logging configuration that can be set via CLI, env vars, or config file
101#[derive(Debug, Clone, Default, Args, Deserialize)]
102pub struct LoggingConfig {
103    /// Enable verbose logging (-v for debug, -vv for trace)
104    #[arg(short = 'v', long = "verbose", action = clap::ArgAction::Count)]
105    #[serde(skip)]
106    pub verbose: u8,
107
108    /// Log level (trace, debug, info, warn, error)
109    #[arg(long = "log-level", env = "HYPRSTREAM_LOG_LEVEL")]
110    pub log_level: Option<String>,
111
112    /// Log filter directives
113    #[arg(long = "log-filter", env = "HYPRSTREAM_LOG_FILTER")]
114    pub log_filter: Option<String>,
115}
116
117impl LoggingConfig {
118    pub fn get_effective_level(&self) -> &str {
119        match (self.verbose, self.log_level.as_deref()) {
120            (2, _) => "trace",         // -vv flag
121            (1, _) => "debug",         // -v flag
122            (0, Some(level)) => level, // Configured level
123            _ => "info",               // Default
124        }
125    }
126}
127
128impl ConfigSection for LoggingConfig {
129    fn options() -> Vec<ConfigOptionDef<String>> {
130        vec![
131            ConfigOptionDef::new(
132                "logging.level",
133                "Log level (trace, debug, info, warn, error)",
134            )
135            .with_env("HYPRSTREAM_LOG_LEVEL")
136            .with_cli("log-level"),
137            ConfigOptionDef::new("logging.filter", "Log filter directives")
138                .with_env("HYPRSTREAM_LOG_FILTER")
139                .with_cli("log-filter"),
140        ]
141    }
142
143    fn from_config<'de, D>(deserializer: D) -> Result<Self, D::Error>
144    where
145        D: serde::Deserializer<'de>,
146    {
147        #[derive(Deserialize)]
148        struct LoggingConfigFile {
149            level: Option<String>,
150            filter: Option<String>,
151        }
152
153        let config = LoggingConfigFile::deserialize(deserializer)?;
154        Ok(LoggingConfig {
155            verbose: 0,
156            log_level: config.level,
157            log_filter: config.filter,
158        })
159    }
160}
161
162/// Service management configuration
163#[derive(Debug, Clone, Default, Args, Deserialize)]
164pub struct ServicesConfigCli {
165    /// Comma-separated list of services to start at startup
166    ///
167    /// Example: --services-startup registry,policy,worker,event
168    /// Environment: HYPRSTREAM_SERVICES_STARTUP=registry,policy,worker,event
169    #[arg(
170        long = "services-startup",
171        env = "HYPRSTREAM_SERVICES_STARTUP",
172        value_delimiter = ','
173    )]
174    #[serde(skip)]
175    pub startup: Option<Vec<String>>,
176}
177
178impl ConfigSection for ServicesConfigCli {
179    fn options() -> Vec<ConfigOptionDef<String>> {
180        vec![
181            ConfigOptionDef::new(
182                "services.startup",
183                "Comma-separated list of services to start at startup (registry, policy, worker, event)",
184            )
185            .with_env("HYPRSTREAM_SERVICES_STARTUP")
186            .with_cli("services-startup"),
187        ]
188    }
189
190    fn from_config<'de, D>(deserializer: D) -> Result<Self, D::Error>
191    where
192        D: serde::Deserializer<'de>,
193    {
194        #[derive(Deserialize)]
195        struct ServicesConfigFile {
196            startup: Option<Vec<String>>,
197        }
198
199        let config = ServicesConfigFile::deserialize(deserializer)?;
200        Ok(ServicesConfigCli {
201            startup: config.startup,
202        })
203    }
204}
205
206/// Credentials for authentication
207#[derive(Debug, Clone, Deserialize)]
208pub struct Credentials {
209    /// Username for authentication
210    pub username: String,
211    /// Password for authentication
212    pub password: String,
213}