Skip to main content

ralph_workflow/cli/init/config_generation/
global.rs

1//! Global configuration file creation.
2//!
3//! Handles `--init-global` flag to create the unified config file at
4//! `~/.config/ralph-workflow.toml`.
5
6use crate::config::{ConfigEnvironment, RealConfigEnvironment};
7use crate::logger::Colors;
8
9/// Handle the `--init-global` flag with a custom path resolver.
10///
11/// Creates a unified config file at the path determined by the resolver.
12/// This is the recommended way to configure Ralph globally.
13///
14/// # Arguments
15///
16/// * `colors` - Terminal color configuration for output
17/// * `env` - Path resolver for determining config file location
18///
19/// # Returns
20///
21/// Returns `Ok(true)` if the flag was handled (program should exit after),
22/// or an error if config creation failed.
23pub fn handle_init_global_with<R: ConfigEnvironment>(
24    colors: Colors,
25    env: &R,
26) -> anyhow::Result<bool> {
27    let global_path = env
28        .unified_config_path()
29        .ok_or_else(|| anyhow::anyhow!("Cannot determine config directory (no home directory)"))?;
30
31    // Check if config already exists using the environment
32    if env.file_exists(&global_path) {
33        println!(
34            "{}Unified config already exists:{} {}",
35            colors.yellow(),
36            colors.reset(),
37            global_path.display()
38        );
39        println!("Edit the file to customize, or delete it to regenerate from defaults.");
40        println!();
41        println!("Next steps:");
42        println!("  1. Create a PROMPT.md for your task:");
43        println!("       ralph --init <work-guide>");
44        println!("       ralph --list-work-guides  # Show all Work Guides");
45        println!("  2. Or run ralph directly with default settings:");
46        println!("       ralph \"your commit message\"");
47        return Ok(true);
48    }
49
50    // Create config using the environment's file operations
51    env.write_file(&global_path, crate::config::unified::DEFAULT_UNIFIED_CONFIG)
52        .map_err(|e| {
53            anyhow::anyhow!(
54                "Failed to create config file {}: {}",
55                global_path.display(),
56                e
57            )
58        })?;
59
60    println!(
61        "{}Created unified config: {}{}{}\n",
62        colors.green(),
63        colors.bold(),
64        global_path.display(),
65        colors.reset()
66    );
67    println!("This is the primary configuration file for Ralph.");
68    println!();
69    println!("Features:");
70    println!("  - General settings (verbosity, iterations, etc.)");
71    println!("  - CCS aliases for Claude Code Switch integration");
72    println!("  - Custom agent definitions");
73    println!("  - Agent chain configuration with fallbacks");
74    println!();
75    println!("Environment variables (RALPH_*) override these settings.");
76    println!();
77    println!("Next steps:");
78    println!("  1. Create a PROMPT.md for your task:");
79    println!("       ralph --init <work-guide>");
80    println!("       ralph --list-work-guides  # Show all Work Guides");
81    println!("  2. Or run ralph directly with default settings:");
82    println!("       ralph \"your commit message\"");
83    Ok(true)
84}
85
86/// Handle the `--init-global` flag using the default path resolver.
87///
88/// Creates a unified config file at `~/.config/ralph-workflow.toml` if it doesn't exist.
89/// This is a convenience wrapper that uses [`RealConfigEnvironment`] internally.
90///
91/// # Arguments
92///
93/// * `colors` - Terminal color configuration for output
94///
95/// # Returns
96///
97/// Returns `Ok(true)` if the flag was handled (program should exit after),
98/// or an error if config creation failed.
99pub fn handle_init_global(colors: Colors) -> anyhow::Result<bool> {
100    handle_init_global_with(colors, &RealConfigEnvironment)
101}
102
103/// Handle --init when neither config nor PROMPT.md exists, using the provided environment.
104///
105/// This creates a global config file as the first step in setting up Ralph.
106pub fn handle_init_none_exist_with_env<R: ConfigEnvironment>(
107    _config_path: &std::path::Path,
108    colors: Colors,
109    env: &R,
110) -> anyhow::Result<bool> {
111    println!(
112        "{}No config found. Creating unified config...{}",
113        colors.dim(),
114        colors.reset()
115    );
116    println!();
117    handle_init_global_with(colors, env)?;
118    Ok(true)
119}