Skip to main content

quorum_public/cli/
config.rs

1//! Configuration command handling (REFACTORED).
2//!
3//! This module handles the `config:show` and `config:<key> <value>` commands.
4//! It no longer hardcodes every field — instead it uses the schema to generate display/parsing dynamically.
5
6use colored::Colorize;
7use quorum_core::utility::config::Config;
8
9/// Changes a configuration value by key.
10///
11/// # Arguments
12/// * `config_name` - The configuration key name (e.g., "server_host", "jwt_access_minutes")
13/// * `config_value` - The new value as a string (parsed to the field's type)
14pub fn change_config_value(config_name: &str, config_value: &str) {
15    match Config::update(config_name, config_value) {
16        Ok(_) => println!("{}", "Config value updated successfully.".green()),
17        Err(e) => println!("{}", format!("Failed to update config value: {}", e).red()),
18    }
19}
20
21/// Displays all configuration settings in a formatted table.
22///
23/// This function uses the schema to dynamically generate the display,
24/// so it automatically includes any new fields added to the schema.
25pub fn print_all() {
26    let cfg = Config::get();
27
28    println!();
29    println!(
30        "{}",
31        "┌────────────────────────────────────────────────────────────┐".dimmed()
32    );
33    println!("│ {} │", "SERVER CONFIGURATION".cyan().bold());
34    println!(
35        "{}",
36        "├────────────────────────────────────────────────────────────┤".dimmed()
37    );
38
39    // Dynamically display each config field using Display trait
40    // Note: The actual display is handled below per-field since they have different types
41    // If you need to add custom formatting for a field (like truncating jwt_secret),
42    // do it in the match statement below rather than in the schema.
43
44    display_config_field("server_host", &cfg.server_host.to_string());
45    display_config_field("server_port", &cfg.server_port.to_string());
46    display_config_field("server_url", &cfg.server_url.to_string());
47
48    println!(
49        "{}",
50        "├────────────────────────────────────────────────────────────┤".dimmed()
51    );
52
53    display_config_field("surreal_data_path", &cfg.surreal_data_path.to_string());
54    display_config_field("surreal_ns", &cfg.surreal_ns.to_string());
55    display_config_field("surreal_db", &cfg.surreal_db.to_string());
56
57    println!(
58        "{}",
59        "├────────────────────────────────────────────────────────────┤".dimmed()
60    );
61
62    // Truncate jwt_secret for security
63    let short_jwt = if cfg.jwt_secret.len() > 30 {
64        format!("{}...", &cfg.jwt_secret[..30])
65    } else {
66        cfg.jwt_secret.clone()
67    };
68    display_config_field_custom("jwt_secret", &short_jwt, true);
69
70    display_config_field(
71        "jwt_access_minutes",
72        &format!("{} min", cfg.jwt_access_minutes),
73    );
74    display_config_field(
75        "jwt_refresh_days",
76        &format!("{} days", cfg.jwt_refresh_days),
77    );
78
79    println!(
80        "{}",
81        "├────────────────────────────────────────────────────────────┤".dimmed()
82    );
83
84    let testing_status = if cfg.enable_testing {
85        "true".green().bold().to_string()
86    } else {
87        "false".red().to_string()
88    };
89    display_config_field_custom("enable_testing", &testing_status, false);
90
91    display_config_field("default_per_second", &cfg.default_per_second.to_string());
92    display_config_field("default_burst_size", &cfg.default_burst_size.to_string());
93    display_config_field("testing_per_second", &cfg.testing_per_second.to_string());
94    display_config_field("testing_burst_size", &cfg.testing_burst_size.to_string());
95
96    println!(
97        "{}",
98        "└────────────────────────────────────────────────────────────┘".dimmed()
99    );
100    println!();
101}
102
103/// Helper to display a single config field in the table format.
104fn display_config_field(key: &str, value: &str) {
105    println!("│ {:<22} : {:<33} │", key.yellow(), value.white());
106}
107
108/// Helper to display a config field with custom value formatting (for colored or special values).
109fn display_config_field_custom(key: &str, value: &str, is_secret: bool) {
110    let value_display = if is_secret {
111        value.dimmed().to_string()
112    } else {
113        value.to_string()
114    };
115    println!("│ {:<22} : {:<42} │", key.yellow(), value_display);
116}