ferrous-forge 1.9.4

System-wide Rust development standards enforcer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Config command implementation
//!
//! @task T015
//! @task T018
//! @epic T014

use crate::commands::ConfigCommand;
use crate::config::{
    Config, ConfigValidator, HierarchicalConfig, HierarchicalLockManager, ImportOptions,
    SharedConfig, audit_log, import_shared_config,
};
use crate::{Error, Result};
use console::style;

/// Execute the config command with subcommand
///
/// # Errors
///
/// Returns an error if any configuration operation fails.
pub async fn execute_with_subcommand(command: ConfigCommand) -> Result<()> {
    match command {
        ConfigCommand::Get { key } => handle_get(&key).await,
        ConfigCommand::Set { value } => handle_set(&value).await,
        ConfigCommand::List => handle_list().await,
        ConfigCommand::Reset => handle_reset().await,
        ConfigCommand::Sources => show_sources().await,
        ConfigCommand::Migrate => migrate_config().await,
        ConfigCommand::Lock {
            key,
            value,
            reason,
            level,
        } => handle_lock(&key, value, &reason, level.to_config_level()).await,
        ConfigCommand::Unlock { key, reason, level } => {
            handle_unlock(&key, &reason, level.to_config_level()).await
        }
        ConfigCommand::LockStatus => handle_lock_status().await,
        ConfigCommand::LockAudit { limit } => handle_lock_audit(limit).await,
        ConfigCommand::Export {
            level,
            output,
            description,
        } => handle_export(level.to_config_level(), output, description).await,
        ConfigCommand::Import {
            file,
            level,
            no_locks,
            force,
        } => handle_import(file, level.to_config_level(), no_locks, force).await,
    }
}

/// Handle the get command
async fn handle_get(key: &str) -> Result<()> {
    let config = Config::load_or_default().await?;

    if let Some(value) = config.get(key) {
        println!("{}", value);
    } else {
        println!(
            "{}",
            style(&format!("Unknown configuration key: {}", key)).red()
        );
        std::process::exit(1);
    }
    Ok(())
}

/// Handle the set command with lock validation
async fn handle_set(set_value: &str) -> Result<()> {
    let mut config = Config::load_or_default().await?;

    if let Some((key, value)) = set_value.split_once('=') {
        // Validate against locks before setting
        ConfigValidator::validate_change(key, value).await?;

        match config.set(key, value) {
            Ok(()) => {
                config.save().await?;
                println!("{}", style(&format!("✅ Set {} = {}", key, value)).green());
            }
            Err(e) => {
                println!("{}", style(&format!("❌ Error: {}", e)).red());
                std::process::exit(1);
            }
        }
    } else {
        println!("{}", style("❌ Invalid format. Use: key=value").red());
        std::process::exit(1);
    }
    Ok(())
}

/// Handle the list command
async fn handle_list() -> Result<()> {
    let config = Config::load_or_default().await?;
    let lock_manager = HierarchicalLockManager::load().await?;

    println!(
        "{}",
        style("⚙️  Ferrous Forge Configuration:").bold().cyan()
    );
    println!();

    for (key, value) in config.list() {
        if lock_manager.is_locked(&key).is_some() {
            println!(
                "  {} {}: {}",
                style(&key).bold(),
                style("🔒").yellow(),
                value
            );
        } else {
            println!("  {}: {}", style(&key).bold(), value);
        }
    }

    // Show locked keys separately if any
    let locks = lock_manager.get_effective_locks();
    if !locks.is_empty() {
        println!();
        println!(
            "{}",
            style("🔒 Locked Configuration Values:").bold().yellow()
        );
        println!();

        for (key, (level, entry)) in locks {
            println!(
                "  {} = {} (locked at {} level)",
                style(&key).bold(),
                entry.value,
                level.display_name()
            );
            println!("    Reason: {}", entry.reason);
            println!(
                "    Locked by {} at {}",
                entry.locked_by,
                entry.locked_at.format("%Y-%m-%d %H:%M:%S UTC")
            );
            println!();
        }
    }

    Ok(())
}

/// Handle the reset command
#[allow(clippy::collapsible_if)]
async fn handle_reset() -> Result<()> {
    let mut config = Config::load_or_default().await?;

    // Check for locked keys before reset
    let lock_manager = HierarchicalLockManager::load().await?;
    let locks = lock_manager.get_effective_locks();

    if !locks.is_empty() {
        println!(
            "{}",
            style("⚠️  Warning: Some configuration values are locked and will not be reset:")
                .yellow()
        );
        for key in locks.keys() {
            println!("  - {}", key);
        }
        println!();
    }

    // Reset only non-locked values
    let default = Config::default();
    for key in config.list().iter().map(|(k, _)| k) {
        if !locks.contains_key(key) {
            if let Some(default_value) = default.get(key) {
                let _ = config.set(key, &default_value);
            }
        }
    }

    config.save().await?;
    println!(
        "{}",
        style("✅ Configuration reset to defaults (locked values preserved)").green()
    );
    Ok(())
}

/// Handle the lock command
async fn handle_lock(
    key: &str,
    value: Option<String>,
    reason: &str,
    level: crate::config::ConfigLevel,
) -> Result<()> {
    // Get current value if not provided
    let value_to_lock = if let Some(v) = value {
        v
    } else {
        let config = Config::load_or_default().await?;
        config
            .get(key)
            .ok_or_else(|| Error::config(format!("Unknown configuration key: {}", key)))?
    };

    let mut lock_manager = HierarchicalLockManager::load().await?;

    match lock_manager
        .lock(key, value_to_lock.clone(), reason, level)
        .await
    {
        Ok(()) => {
            println!(
                "{}",
                style(format!(
                    "🔒 Locked {} = {} at {} level",
                    key,
                    value_to_lock,
                    level.display_name()
                ))
                .green()
                .bold()
            );
            println!("  Reason: {}", reason);
        }
        Err(e) => {
            println!("{}", style(format!("❌ Failed to lock: {}", e)).red());
            std::process::exit(1);
        }
    }

    Ok(())
}

/// Handle the unlock command
async fn handle_unlock(key: &str, reason: &str, level: crate::config::ConfigLevel) -> Result<()> {
    let mut lock_manager = HierarchicalLockManager::load().await?;

    match lock_manager.unlock(key, level, reason).await {
        Ok(entry) => {
            println!(
                "{}",
                style(format!(
                    "🔓 Unlocked {} at {} level",
                    key,
                    level.display_name()
                ))
                .green()
                .bold()
            );
            println!("  Previous value: {}", entry.value);
            println!("  Unlock reason: {}", reason);
            println!(
                "  Originally locked by {} at {}",
                entry.locked_by,
                entry.locked_at.format("%Y-%m-%d %H:%M:%S UTC")
            );
        }
        Err(e) => {
            println!("{}", style(format!("❌ Failed to unlock: {}", e)).red());
            std::process::exit(1);
        }
    }

    Ok(())
}

/// Handle the lock status command
async fn handle_lock_status() -> Result<()> {
    let lock_manager = HierarchicalLockManager::load().await?;
    println!("{}", lock_manager.status_report());
    Ok(())
}

/// Handle the lock audit command
async fn handle_lock_audit(limit: usize) -> Result<()> {
    let entries = audit_log::read_audit_log().await?;

    println!("{}", style("📋 Configuration Lock Audit Log").bold().cyan());
    println!();

    if entries.is_empty() {
        println!("No audit entries found.");
        return Ok(());
    }

    // Show most recent entries first
    for entry in entries.iter().rev().take(limit) {
        let status_icon = if entry.success {
            style("").green()
        } else {
            style("").red()
        };

        println!(
            "{} {} - {} at {} level",
            status_icon,
            entry.timestamp.format("%Y-%m-%d %H:%M:%S UTC"),
            entry.operation.to_uppercase(),
            entry.level
        );
        println!("   Key: {}", entry.key);
        if let Some(ref value) = entry.value {
            println!("   Value: {}", value);
        }
        println!("   User: {}", entry.user);
        println!("   Reason: {}", entry.reason);
        if let Some(ref error) = entry.error {
            println!("   Error: {}", error);
        }
        println!();
    }

    if entries.len() > limit {
        println!(
            "... and {} more entries (use --limit to show more)",
            entries.len() - limit
        );
    }

    Ok(())
}

/// Show configuration help
pub fn show_help() {
    println!("{}", style("⚙️  Ferrous Forge Configuration").bold().cyan());
    println!();
    println!("Usage:");
    println!("  ferrous-forge config list                    # Show all settings");
    println!("  ferrous-forge config get <key>              # Get a setting");
    println!("  ferrous-forge config set <key=value>        # Set a setting");
    println!("  ferrous-forge config reset                   # Reset to defaults");
    println!("  ferrous-forge config sources                 # Show config hierarchy");
    println!("  ferrous-forge config migrate                 # Migrate old config");
    println!();
    println!("Locking Commands:");
    println!("  ferrous-forge config lock <key> --reason=\"...\" [--level=project]");
    println!("  ferrous-forge config unlock <key> --reason=\"...\" [--level=project]");
    println!("  ferrous-forge config lock-status             # Show all locked values");
    println!("  ferrous-forge config lock-audit              # View lock audit log");
    println!();
    println!("Sharing Commands:");
    println!("  ferrous-forge config export --level=user > team-config.toml");
    println!("  ferrous-forge config import team-config.toml [--level=project]");
    println!("  ferrous-forge config export --output=shared.toml --description=\"Team standards\"");
    println!();
    println!("Configuration Hierarchy:");
    println!("  1. System: /etc/ferrous-forge/config.toml");
    println!("  2. User: ~/.config/ferrous-forge/config.toml");
    println!("  3. Project: ./.ferrous-forge/config.toml");
    println!();
    println!("Lock Levels:");
    println!("  Project-level locks override User-level and System-level locks");
    println!("  User-level locks override System-level locks");
    println!();
    println!("Sharing:");
    println!("  Export creates a git-friendly TOML file with config and locks");
    println!("  Import merges safely, respecting existing locks at higher levels");
    println!();
    println!("Available settings:");
    println!("  update_channel (stable, beta, nightly)");
    println!("  auto_update (true, false)");
    println!("  max_file_lines (number)");
    println!("  max_function_lines (number)");
    println!("  required_edition (2015, 2018, 2021, 2024) 🔒 lockable");
    println!("  required_rust_version (version string) 🔒 lockable");
    println!("  ban_underscore_bandaid (true, false)");
    println!("  require_documentation (true, false)");
}

/// Show configuration sources
///
/// # Errors
///
/// Returns an error if the hierarchical configuration cannot be loaded.
pub async fn show_sources() -> Result<()> {
    println!("{}", style("🔍 Configuration Hierarchy").bold().cyan());
    println!();

    let hier = HierarchicalConfig::load().await?;
    println!("{}", hier.sources_report());

    // Also show lock sources
    let lock_manager = HierarchicalLockManager::load().await?;
    println!("{}", lock_manager.status_report());

    Ok(())
}

/// Migrate old configuration to hierarchical system
///
/// # Errors
///
/// Returns an error if the configuration migration fails.
pub async fn migrate_config() -> Result<()> {
    println!("{}", style("📦 Migrating configuration...").bold().cyan());

    crate::config::hierarchy::migrate_config().await?;

    println!(
        "{}",
        style("✅ Configuration migrated successfully").green()
    );
    Ok(())
}

/// Handle config export command
///
/// # Errors
///
/// Returns an error if the export fails.
async fn handle_export(
    level: crate::config::ConfigLevel,
    output: Option<std::path::PathBuf>,
    description: Option<String>,
) -> Result<()> {
    let mut shared = SharedConfig::from_level(level).await?;

    if let Some(desc) = description {
        shared = shared.with_description(desc);
    }

    // Print summary
    println!("{}", style("📤 Exporting Configuration").bold().cyan());
    println!();
    println!("{}", shared.summary());

    if let Some(path) = output {
        // Export to file
        shared.export_to_file(&path).await?;
        println!(
            "{}",
            style(format!("✅ Exported to {}", path.display())).green()
        );
    } else {
        // Export to stdout
        let toml = shared.to_toml()?;
        println!("{}", style("--- Configuration (TOML) ---").dim());
        println!("{}", toml);
    }

    Ok(())
}

/// Handle config import command
///
/// # Errors
///
/// Returns an error if the import fails.
async fn handle_import(
    file: std::path::PathBuf,
    level: crate::config::ConfigLevel,
    no_locks: bool,
    force: bool,
) -> Result<()> {
    println!("{}", style("📥 Importing Configuration").bold().cyan());
    println!();

    // Load the shared config
    let shared = SharedConfig::import_from_file(&file).await?;
    println!("{}", shared.summary());

    // Configure import options
    let options = ImportOptions {
        target_level: level,
        overwrite: force,
        import_locks: !no_locks,
        require_justification: true,
    };

    // Perform import
    match import_shared_config(&shared, options).await {
        Ok(report) => {
            if !report.conflicts.is_empty() {
                println!(
                    "{}",
                    style("⚠️ Import blocked by lock conflicts:")
                        .yellow()
                        .bold()
                );
                for conflict in &report.conflicts {
                    println!(
                        "{} is locked at {} level with value '{}'",
                        style(&conflict.key).bold(),
                        conflict.locked_at.display_name(),
                        conflict.current_value
                    );
                    println!("    Attempted value: '{}'", conflict.attempted_value);
                }
                println!();
                println!(
                    "{}",
                    style("Use --force to override (requires unlock first)").dim()
                );
                return Err(Error::config("Import blocked by lock conflicts"));
            }

            println!("{}", style("✅ Import successful!").green().bold());
            println!(
                "{} configuration keys updated",
                report.config_keys_updated
            );
            println!("{} locks imported", report.locks_imported.len());
            if !report.locks_skipped.is_empty() {
                println!(
                    "{} locks skipped (already exist)",
                    report.locks_skipped.len()
                );
            }
        }
        Err(e) => {
            println!("{}", style(format!("❌ Import failed: {}", e)).red());
            return Err(e);
        }
    }

    Ok(())
}