dotstate 0.3.4

A modern, secure, and user-friendly dotfile manager built with Rust
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
//! Common CLI utilities shared across all CLI commands.
//!
//! This module provides:
//! - `CliContext`: Shared context for loading config/manifest
//! - Output helpers: `print_success`, `print_error`, `print_warning`, `print_info`
//! - Prompt helpers: `prompt_string`, `prompt_string_optional`, `prompt_select`, `prompt_confirm`

use crate::config::Config;
use crate::services::PackageService;
use crate::utils::profile_manifest::PackageManager;
use crate::utils::ProfileManifest;
use anyhow::{Context, Result};
use std::io::{self, Write};
use std::path::PathBuf;

/// Shared context for CLI commands.
///
/// Provides access to configuration, profile manifest, and common utilities.
pub struct CliContext {
    /// The loaded configuration
    pub config: Config,
    /// The profile manifest from the repository
    pub manifest: ProfileManifest,
    /// Path to the config file
    pub config_path: PathBuf,
}

impl CliContext {
    /// Load the CLI context from the configuration file.
    ///
    /// This will exit with an error message if:
    /// - Config file cannot be loaded
    /// - Repository is not configured
    /// - Profile manifest cannot be loaded
    pub fn load() -> Result<Self> {
        let config_path = crate::utils::get_config_path();

        let config =
            Config::load_or_create(&config_path).context("Failed to load configuration")?;

        // Check if repository is configured
        if !config.is_repo_configured() {
            print_error("Repository not configured. Please run 'dotstate' to set up repository.");
            std::process::exit(1);
        }

        let manifest = ProfileManifest::load_or_backfill(&config.repo_path)
            .context("Failed to load profile manifest")?;

        Ok(Self {
            config,
            manifest,
            config_path,
        })
    }

    /// Resolve a profile name: returns provided profile or falls back to active profile.
    ///
    /// # Arguments
    /// * `profile` - Optional profile name provided by the user
    ///
    /// # Returns
    /// The resolved profile name
    #[must_use]
    pub fn resolve_profile(&self, profile: Option<&str>) -> String {
        profile.map_or_else(
            || self.config.active_profile.clone(),
            std::string::ToString::to_string,
        )
    }

    /// Check if the given profile name is the active profile.
    ///
    /// # Arguments
    /// * `profile_name` - Profile name to check
    ///
    /// # Returns
    /// `true` if the profile is the active profile
    #[must_use]
    pub fn is_active_profile(&self, profile_name: &str) -> bool {
        self.config.active_profile == profile_name
    }

    /// Check if a profile exists in the manifest.
    ///
    /// # Arguments
    /// * `profile_name` - Profile name to check
    ///
    /// # Returns
    /// `true` if the profile exists
    #[must_use]
    pub fn profile_exists(&self, profile_name: &str) -> bool {
        self.manifest
            .profiles
            .iter()
            .any(|p| p.name == profile_name)
    }

    /// Get a reference to a profile by name.
    ///
    /// # Arguments
    /// * `profile_name` - Profile name to find
    ///
    /// # Returns
    /// Reference to the profile info if found
    #[must_use]
    pub fn get_profile(&self, profile_name: &str) -> Option<&crate::utils::ProfileInfo> {
        self.manifest
            .profiles
            .iter()
            .find(|p| p.name == profile_name)
    }

    /// Save the manifest to disk.
    pub fn save_manifest(&self) -> Result<()> {
        self.manifest
            .save(&self.config.repo_path)
            .context("Failed to save profile manifest")
    }
}

// =============================================================================
// Output Helpers
// =============================================================================

/// Print a success message with a checkmark prefix.
///
/// # Arguments
/// * `msg` - The message to print
pub fn print_success(msg: &str) {
    println!("\u{2713} {msg}");
}

/// Print an error message with an X prefix to stderr.
///
/// # Arguments
/// * `msg` - The message to print
pub fn print_error(msg: &str) {
    eprintln!("\u{2717} {msg}");
}

/// Print a warning message with a warning sign prefix.
///
/// # Arguments
/// * `msg` - The message to print
pub fn print_warning(msg: &str) {
    println!("\u{26A0}\u{FE0F} {msg}");
}

/// Print an info message with an info sign prefix.
///
/// # Arguments
/// * `msg` - The message to print
pub fn print_info(msg: &str) {
    println!("\u{2139}\u{FE0F} {msg}");
}

// =============================================================================
// Prompt Helpers
// =============================================================================

/// Prompt the user for a string input with an optional default value.
///
/// # Arguments
/// * `label` - The prompt label to display
/// * `default` - Optional default value shown in brackets
///
/// # Returns
/// The user's input, or the default if they pressed Enter
pub fn prompt_string(label: &str, default: Option<&str>) -> Result<String> {
    if let Some(def) = default {
        print!("{label} [{def}]: ");
    } else {
        print!("{label}: ");
    }
    io::stdout().flush().context("Failed to flush stdout")?;

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .context("Failed to read input")?;

    let trimmed = input.trim();
    if trimmed.is_empty() {
        Ok(default.unwrap_or("").to_string())
    } else {
        Ok(trimmed.to_string())
    }
}

/// Prompt the user for an optional string input.
///
/// # Arguments
/// * `label` - The prompt label to display
///
/// # Returns
/// `Some(input)` if the user entered text, `None` if they pressed Enter
pub fn prompt_string_optional(label: &str) -> Result<Option<String>> {
    print!("{label} (optional): ");
    io::stdout().flush().context("Failed to flush stdout")?;

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .context("Failed to read input")?;

    let trimmed = input.trim();
    if trimmed.is_empty() {
        Ok(None)
    } else {
        Ok(Some(trimmed.to_string()))
    }
}

/// Prompt the user to select from a numbered list of options.
///
/// # Arguments
/// * `label` - The prompt label to display
/// * `options` - List of options to display (shown as 1-indexed)
///
/// # Returns
/// The 0-indexed position of the selected option
///
/// # Panics
/// Exits with error if options is empty or user enters invalid input
pub fn prompt_select(label: &str, options: &[&str]) -> Result<usize> {
    if options.is_empty() {
        print_error("No options available for selection");
        std::process::exit(1);
    }

    println!("{label}:");
    for (i, option) in options.iter().enumerate() {
        println!("  {}. {}", i + 1, option);
    }
    print!("Enter choice [1-{}]: ", options.len());
    io::stdout().flush().context("Failed to flush stdout")?;

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .context("Failed to read input")?;

    let trimmed = input.trim();
    match trimmed.parse::<usize>() {
        Ok(n) if n >= 1 && n <= options.len() => Ok(n - 1),
        _ => {
            print_error(&format!(
                "Invalid choice. Please enter a number between 1 and {}",
                options.len()
            ));
            std::process::exit(1);
        }
    }
}

/// Prompt the user to select from a numbered list of options with optional suffixes.
///
/// # Arguments
/// * `label` - The prompt label to display
/// * `options` - List of (name, `optional_suffix`) tuples to display (shown as 1-indexed)
///
/// # Returns
/// The 0-indexed position of the selected option
///
/// # Panics
/// Exits with error if options is empty or user enters invalid input
pub fn prompt_select_with_suffix(label: &str, options: &[(&str, Option<&str>)]) -> Result<usize> {
    if options.is_empty() {
        print_error("No options available for selection");
        std::process::exit(1);
    }

    println!("{label}:");
    for (i, (name, suffix)) in options.iter().enumerate() {
        if let Some(s) = suffix {
            println!("  {}. {} {}", i + 1, name, s);
        } else {
            println!("  {}. {}", i + 1, name);
        }
    }
    print!("Enter choice [1-{}]: ", options.len());
    io::stdout().flush().context("Failed to flush stdout")?;

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .context("Failed to read input")?;

    let trimmed = input.trim();
    match trimmed.parse::<usize>() {
        Ok(n) if n >= 1 && n <= options.len() => Ok(n - 1),
        _ => {
            print_error(&format!(
                "Invalid choice. Please enter a number between 1 and {}",
                options.len()
            ));
            std::process::exit(1);
        }
    }
}

/// Prompt the user for a yes/no confirmation.
///
/// # Arguments
/// * `message` - The confirmation message to display
///
/// # Returns
/// `true` if the user confirmed (y/yes), `false` otherwise
pub fn prompt_confirm(message: &str) -> Result<bool> {
    print!("{message} [y/N]: ");
    io::stdout().flush().context("Failed to flush stdout")?;

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .context("Failed to read input")?;

    let trimmed = input.trim().to_lowercase();
    Ok(trimmed == "y" || trimmed == "yes")
}

// =============================================================================
// Package Manager Helpers
// =============================================================================

/// Get all supported package managers (for non-active profiles).
fn all_package_managers() -> Vec<PackageManager> {
    vec![
        PackageManager::Brew,
        PackageManager::Apt,
        PackageManager::Yum,
        PackageManager::Dnf,
        PackageManager::Pacman,
        PackageManager::Snap,
        PackageManager::Cargo,
        PackageManager::Npm,
        PackageManager::Pip,
        PackageManager::Pip3,
        PackageManager::Gem,
        PackageManager::Custom,
    ]
}

/// Prompt for package manager selection.
///
/// For active profiles: shows only managers available on current OS with "(installed)" markers.
/// For non-active profiles: shows ALL supported managers (profile may be for different OS).
///
/// # Arguments
/// * `is_active_profile` - Whether this is the active profile
///
/// # Returns
/// The selected `PackageManager`
pub fn prompt_manager(is_active_profile: bool) -> Result<PackageManager> {
    // For active profile: use OS-detected managers with installed markers
    // For non-active profile: show ALL managers (may be for different OS)
    let managers = if is_active_profile {
        PackageService::get_available_managers()
    } else {
        all_package_managers()
    };

    let options: Vec<(&str, Option<&str>)> = managers
        .iter()
        .map(|m| {
            let name = match m {
                PackageManager::Brew => "brew",
                PackageManager::Apt => "apt",
                PackageManager::Yum => "yum",
                PackageManager::Dnf => "dnf",
                PackageManager::Pacman => "pacman",
                PackageManager::Snap => "snap",
                PackageManager::Cargo => "cargo",
                PackageManager::Npm => "npm",
                PackageManager::Pip => "pip",
                PackageManager::Pip3 => "pip3",
                PackageManager::Gem => "gem",
                PackageManager::Custom => "custom",
            };
            // Only show "(installed)" markers for active profile
            let suffix = if is_active_profile && PackageService::is_manager_installed(m) {
                Some("(installed)")
            } else {
                None
            };
            (name, suffix)
        })
        .collect();

    let index = prompt_select_with_suffix("Manager", &options)?;
    Ok(managers[index].clone())
}

/// Parse package manager from string.
///
/// # Arguments
/// * `s` - The string to parse
///
/// # Returns
/// The parsed `PackageManager`, or None if invalid
#[must_use]
pub fn parse_manager(s: &str) -> Option<PackageManager> {
    match s.to_lowercase().as_str() {
        "brew" | "homebrew" => Some(PackageManager::Brew),
        "apt" | "apt-get" => Some(PackageManager::Apt),
        "yum" => Some(PackageManager::Yum),
        "dnf" => Some(PackageManager::Dnf),
        "pacman" => Some(PackageManager::Pacman),
        "snap" => Some(PackageManager::Snap),
        "cargo" => Some(PackageManager::Cargo),
        "npm" => Some(PackageManager::Npm),
        "pip" => Some(PackageManager::Pip),
        "pip3" => Some(PackageManager::Pip3),
        "gem" => Some(PackageManager::Gem),
        "custom" => Some(PackageManager::Custom),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_output_helpers_compile() {
        // Just verify that the output helper unicode characters are valid
        // We can't easily test stdout/stderr in unit tests
        // but we can verify the format strings are valid
        let _ = format!("\u{2713} {}", "test"); // checkmark
        let _ = format!("\u{2717} {}", "test"); // X mark
        let _ = format!("\u{26A0}\u{FE0F} {}", "test"); // warning sign
        let _ = format!("\u{2139}\u{FE0F} {}", "test"); // info sign
    }
}