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