subx_cli/cli/config_args.rs
1//! Configuration management command-line arguments and operations.
2//!
3//! This module defines the command-line interface for configuration management
4//! in SubX. It provides comprehensive tools for viewing, modifying, and maintaining
5//! configuration settings that control various aspects of subtitle processing,
6//! AI integration, and application behavior.
7//!
8//! # Configuration System
9//!
10//! SubX uses a hierarchical configuration system with multiple sources:
11//! - **System-wide**: Global defaults for all users
12//! - **User-specific**: Personal settings in user config directory
13//! - **Project-specific**: Local settings for specific projects
14//! - **Environment variables**: Runtime configuration overrides
15//! - **Command-line**: Highest priority, temporary overrides
16//!
17//! # Configuration Categories
18//!
19//! - **General**: Basic application behavior and preferences
20//! - **AI Settings**: API endpoints, models, and parameters
21//! - **Audio Processing**: Audio analysis and synchronization settings
22//! - **Format Options**: Default output formats and encoding preferences
23//! - **Performance**: Parallel processing and caching configuration
24//!
25//! # Examples
26//!
27//! ```bash
28//! # View all configuration settings
29//! subx config list
30//!
31//! # Get specific setting
32//! subx config get ai.provider
33//!
34//! # Set AI provider
35//! subx config set ai.provider openai
36//!
37//! # Reset to defaults
38//! subx config reset
39//! ```
40
41// src/cli/config_args.rs
42use clap::{Args, Subcommand};
43
44/// Command-line arguments for configuration management operations.
45///
46/// The config command provides comprehensive tools for managing SubX
47/// configuration settings. It supports viewing, modifying, and resetting
48/// configuration values across all configuration categories and sources.
49///
50/// # Configuration Management Features
51///
52/// - **Hierarchical Settings**: Manage settings across multiple configuration levels
53/// - **Type Safety**: Automatic validation of configuration values
54/// - **Interactive Editing**: User-friendly prompts for complex settings
55/// - **Backup/Restore**: Automatic backup before destructive operations
56/// - **Import/Export**: Share configuration profiles between systems
57///
58/// # Configuration Scope
59///
60/// Configuration changes can affect:
61/// - **User Settings**: Personal preferences and credentials
62/// - **Project Settings**: Local project-specific overrides
63/// - **Runtime Behavior**: How the application processes files
64/// - **Performance**: Processing speed and resource usage
65/// - **Integration**: External service connections and API settings
66///
67/// # Examples
68///
69/// ```rust
70/// use subx_cli::cli::{ConfigArgs, ConfigAction};
71///
72/// // Set a configuration value
73/// let set_args = ConfigArgs {
74/// action: ConfigAction::Set {
75/// key: "ai.provider".to_string(),
76/// value: "openai".to_string(),
77/// },
78/// };
79///
80/// // Get a configuration value
81/// let get_args = ConfigArgs {
82/// action: ConfigAction::Get {
83/// key: "ai.provider".to_string(),
84/// },
85/// };
86/// ```
87#[derive(Args, Debug)]
88pub struct ConfigArgs {
89 /// The configuration management action to perform.
90 ///
91 /// Specifies which configuration operation should be executed.
92 /// Each action provides different capabilities for viewing and
93 /// modifying configuration settings with appropriate validation
94 /// and error handling.
95 #[command(subcommand)]
96 pub action: ConfigAction,
97}
98
99/// Configuration management operations and subcommands.
100///
101/// Defines the available configuration management operations that can be
102/// performed through the SubX CLI. Each operation provides specific
103/// functionality for different aspects of configuration management.
104///
105/// # Operation Categories
106///
107/// - **Viewing**: Get and list operations for inspecting settings
108/// - **Modification**: Set operation for changing configuration values
109/// - **Maintenance**: Reset operation for restoring defaults
110///
111/// # Validation and Safety
112///
113/// All configuration operations include:
114/// - **Type validation**: Ensure values match expected data types
115/// - **Range checking**: Validate numeric values are within acceptable ranges
116/// - **Format verification**: Check string values follow required patterns
117/// - **Dependency checking**: Verify related settings are compatible
118///
119/// # Examples
120///
121/// ```rust
122/// use subx_cli::cli::ConfigAction;
123///
124/// // Different configuration operations
125/// let set_provider = ConfigAction::Set {
126/// key: "ai.provider".to_string(),
127/// value: "openai".to_string(),
128/// };
129///
130/// let get_provider = ConfigAction::Get {
131/// key: "ai.provider".to_string(),
132/// };
133///
134/// let list_all = ConfigAction::List;
135/// let reset_config = ConfigAction::Reset;
136/// ```
137#[derive(Subcommand, Debug)]
138pub enum ConfigAction {
139 /// Set a configuration value with validation and type checking.
140 ///
141 /// Updates a specific configuration setting with the provided value.
142 /// The operation includes comprehensive validation to ensure the new
143 /// value is compatible with the setting's type and constraints.
144 ///
145 /// # Key Format
146 ///
147 /// Configuration keys use dot notation to navigate the hierarchy:
148 /// - `ai.provider` - AI service provider selection
149 /// - `ai.openai.api_key` - OpenAI API key
150 /// - `general.enable_progress_bar` - Progress bar display preference
151 /// - `audio.max_offset_seconds` - Maximum sync offset range
152 ///
153 /// # Value Types and Examples
154 ///
155 /// ```bash
156 /// # String values
157 /// subx config set ai.provider "openai"
158 /// subx config set ai.openai.api_key "sk-..."
159 ///
160 /// # Boolean values
161 /// subx config set general.enable_progress_bar true
162 /// subx config set ai.enable_cache false
163 ///
164 /// # Numeric values
165 /// subx config set audio.max_offset_seconds 30.0
166 /// subx config set ai.max_sample_length 2000
167 ///
168 /// # Array values (JSON format)
169 /// subx config set ai.supported_models '["gpt-4.1"]'
170 /// ```
171 ///
172 /// # Validation Rules
173 ///
174 /// - **API Keys**: Must match provider-specific format requirements
175 /// - **URLs**: Must be valid HTTP/HTTPS endpoints
176 /// - **File Paths**: Must be accessible and have appropriate permissions
177 /// - **Numeric Ranges**: Must fall within acceptable min/max values
178 /// - **Enum Values**: Must match predefined valid options
179 ///
180 /// # Error Handling
181 ///
182 /// Common validation errors and solutions:
183 /// - **Invalid key**: Check key spelling and available options
184 /// - **Type mismatch**: Verify value format matches expected type
185 /// - **Range error**: Ensure numeric values are within valid range
186 /// - **Permission error**: Check file/directory access permissions
187 Set {
188 /// Configuration key in dot notation (e.g., "ai.provider", "general.timeout").
189 ///
190 /// Specifies the configuration setting to modify using hierarchical
191 /// dot notation. The key must correspond to a valid configuration
192 /// setting as defined in the application's configuration schema.
193 ///
194 /// # Key Categories
195 ///
196 /// - **ai.***: AI service configuration and credentials
197 /// - **audio.***: Audio processing and synchronization settings
198 /// - **general.***: Basic application behavior and preferences
199 /// - **cache.***: Caching behavior and storage settings
200 /// - **format.***: Default output formats and encoding options
201 ///
202 /// # Examples
203 /// ```bash
204 /// subx config set ai.provider openai
205 /// subx config set general.enable_progress_bar false
206 /// subx config set audio.correlation_threshold 0.8
207 /// ```
208 key: String,
209
210 /// New value for the configuration setting.
211 ///
212 /// The value to assign to the specified configuration key. The value
213 /// will be validated against the setting's type and constraints before
214 /// being applied. String values containing spaces should be quoted.
215 ///
216 /// # Type Conversion
217 ///
218 /// Values are automatically converted to the appropriate type:
219 /// - **Strings**: Used as-is or with quotes for spaces
220 /// - **Booleans**: "true", "false", "1", "0", "yes", "no"
221 /// - **Numbers**: Integer or floating-point notation
222 /// - **Arrays**: JSON array format for complex values
223 ///
224 /// # Special Values
225 ///
226 /// - **Empty string**: `""` to clear string settings
227 /// - **Null/None**: `null` to unset optional settings
228 /// - **Environment variables**: `${VAR_NAME}` for dynamic values
229 ///
230 /// # Examples
231 /// ```bash
232 /// subx config set ai.openai.api_key "sk-1234567890abcdef"
233 /// subx config set general.timeout 30
234 /// subx config set audio.enabled true
235 /// subx config set cache.max_size_mb 512
236 /// ```
237 value: String,
238 },
239
240 /// Retrieve and display a specific configuration value.
241 ///
242 /// Displays the current value of a configuration setting along with
243 /// metadata such as the source of the setting (user config, system
244 /// default, environment variable, etc.) and any applicable constraints.
245 ///
246 /// # Output Format
247 ///
248 /// The command displays:
249 /// - **Current Value**: The effective value being used
250 /// - **Source**: Where the value originates (user, system, environment)
251 /// - **Default Value**: The built-in default if different from current
252 /// - **Type Information**: Expected value type and constraints
253 /// - **Description**: Human-readable explanation of the setting
254 ///
255 /// # Examples
256 ///
257 /// ```bash
258 /// subx config get ai.provider
259 /// # Output:
260 /// # ai.provider = "openai"
261 /// # Source: user config (/home/user/.config/subx/config.toml)
262 /// # Default: "openai"
263 /// # Type: String (enum: openai, anthropic, local)
264 /// # Description: AI service provider for subtitle matching
265 ///
266 /// subx config get general.enable_progress_bar
267 /// # Output:
268 /// # general.enable_progress_bar = true
269 /// # Source: system default
270 /// # Type: Boolean
271 /// # Description: Show progress bars during long operations
272 /// ```
273 ///
274 /// # Use Cases
275 ///
276 /// - **Debugging**: Verify current configuration values
277 /// - **Documentation**: Understand setting sources and constraints
278 /// - **Validation**: Confirm settings are applied correctly
279 /// - **Troubleshooting**: Identify configuration-related issues
280 Get {
281 /// Configuration key to retrieve (e.g., "ai.provider", "general.timeout").
282 ///
283 /// Specifies which configuration setting to display. The key must
284 /// correspond to a valid configuration setting. Use `subx config list`
285 /// to see all available configuration keys.
286 ///
287 /// # Wildcard Support
288 ///
289 /// Future enhancement will support wildcard patterns:
290 /// - `ai.*` - All AI-related settings
291 /// - `*.timeout` - All timeout settings
292 /// - `general.*` - All general application settings
293 ///
294 /// # Examples
295 /// ```bash
296 /// subx config get ai.provider
297 /// subx config get general.enable_progress_bar
298 /// subx config get audio.correlation_threshold
299 /// ```
300 key: String,
301 },
302
303 /// List all configuration settings with their current values.
304 ///
305 /// Displays a comprehensive overview of all configuration settings
306 /// organized by category. This provides a complete view of the current
307 /// configuration state and helps identify settings that may need adjustment.
308 ///
309 /// # Output Organization
310 ///
311 /// Settings are grouped by category:
312 /// - **General**: Basic application behavior
313 /// - **AI**: AI service configuration and parameters
314 /// - **Audio**: Audio processing and synchronization
315 /// - **Cache**: Caching behavior and storage
316 /// - **Format**: Output format and encoding preferences
317 ///
318 /// # Information Displayed
319 ///
320 /// For each setting:
321 /// - **Key**: Full configuration key path
322 /// - **Value**: Current effective value
323 /// - **Source**: Configuration source (user/system/env/default)
324 /// - **Type**: Data type and constraints
325 /// - **Status**: Modified/default indicator
326 ///
327 /// # Filtering Options
328 ///
329 /// Future enhancements will include:
330 /// - Category filtering (`--category ai`)
331 /// - Modified-only view (`--modified-only`)
332 /// - Source filtering (`--source user`)
333 /// - Output format options (`--format json`)
334 ///
335 /// # Examples
336 ///
337 /// ```bash
338 /// subx config list
339 /// # Output:
340 /// # [General]
341 /// # enable_progress_bar = true (default)
342 /// # timeout = 30 (user)
343 /// #
344 /// # [AI]
345 /// # provider = "openai" (user)
346 /// # openai.api_key = "sk-***" (env: OPENAI_API_KEY)
347 /// # max_sample_length = 2000 (default)
348 /// #
349 /// # [Audio]
350 /// # max_offset_seconds = 30.0 (default)
351 /// # correlation_threshold = 0.8 (user)
352 /// ```
353 ///
354 /// # Use Cases
355 ///
356 /// - **Configuration Review**: Audit current settings
357 /// - **Troubleshooting**: Identify problematic configurations
358 /// - **Documentation**: Generate configuration documentation
359 /// - **Migration**: Prepare for configuration transfer
360 List,
361
362 /// Reset configuration to default values with backup creation.
363 ///
364 /// Restores all configuration settings to their built-in default values.
365 /// This operation creates a backup of the current configuration before
366 /// making changes, allowing for easy recovery if needed.
367 ///
368 /// # Reset Scope
369 ///
370 /// The reset operation affects:
371 /// - **User Configuration**: Settings in user config directory
372 /// - **Project Configuration**: Local project-specific settings
373 /// - **Cached Values**: Processed configuration cache
374 ///
375 /// # Data Preserved
376 ///
377 /// The following are NOT affected by reset:
378 /// - **Environment Variables**: Runtime configuration overrides
379 /// - **Command-line Arguments**: Temporary session overrides
380 /// - **System Configuration**: Global system-wide settings
381 /// - **Application Data**: Caches, logs, and processing results
382 ///
383 /// # Backup Process
384 ///
385 /// Before resetting:
386 /// 1. **Backup Creation**: Current config saved with timestamp
387 /// 2. **Verification**: Ensure backup was created successfully
388 /// 3. **Reset Application**: Apply default values
389 /// 4. **Validation**: Verify new configuration is valid
390 ///
391 /// # Backup Location
392 ///
393 /// Backups are stored in the configuration directory:
394 /// ```text
395 /// ~/.config/subx/backups/config_backup_YYYYMMDD_HHMMSS.toml
396 /// ```
397 ///
398 /// # Recovery
399 ///
400 /// To restore from backup:
401 /// ```bash
402 /// # Copy backup back to main config
403 /// cp ~/.config/subx/backups/config_backup_20240101_120000.toml \
404 /// ~/.config/subx/config.toml
405 /// ```
406 ///
407 /// # Confirmation
408 ///
409 /// This is a destructive operation that requires user confirmation:
410 /// ```bash
411 /// subx config reset
412 /// # Output:
413 /// # ⚠ This will reset all configuration to defaults
414 /// # ⚠ Current config will be backed up to: ~/.config/subx/backups/...
415 /// # Continue? [y/N]:
416 /// ```
417 ///
418 /// # Use Cases
419 ///
420 /// - **Fresh Start**: Clean slate for new configuration
421 /// - **Troubleshooting**: Eliminate configuration-related issues
422 /// - **Testing**: Ensure tests run with predictable defaults
423 /// - **Migration**: Prepare for major version upgrades
424 ///
425 /// # Examples
426 ///
427 /// ```bash
428 /// subx config reset
429 /// # Interactive confirmation and backup creation
430 ///
431 /// subx config reset --force
432 /// # Skip confirmation (future enhancement)
433 ///
434 /// subx config reset --dry-run
435 /// # Show what would be reset without making changes
436 /// ```
437 Reset,
438}