-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.rs
More file actions
70 lines (64 loc) · 2.03 KB
/
common.rs
File metadata and controls
70 lines (64 loc) · 2.03 KB
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
//! Common types and utilities for command execution
use ccsync::config::{Config, ConfigManager};
/// Execution options for sync commands
#[allow(clippy::struct_excessive_bools)]
pub struct SyncOptions<'a> {
/// Enable verbose output
pub verbose: bool,
/// Preview changes without applying (dry-run)
pub dry_run: bool,
/// Auto-approve all operations without prompting
pub yes_all: bool,
/// Path to custom config file
pub config_path: Option<&'a std::path::Path>,
/// Skip loading all config files
pub no_config: bool,
}
impl<'a> SyncOptions<'a> {
/// Create new sync options
#[must_use]
#[allow(clippy::fn_params_excessive_bools)]
pub const fn new(
verbose: bool,
dry_run: bool,
yes_all: bool,
config_path: Option<&'a std::path::Path>,
no_config: bool,
) -> Self {
Self {
verbose,
dry_run,
yes_all,
config_path,
no_config,
}
}
/// Load configuration from files or use defaults
///
/// # Errors
///
/// Returns an error if config file is explicitly specified but cannot be loaded.
pub fn load_config(&self) -> anyhow::Result<Config> {
if self.no_config {
if self.verbose {
println!("Skipping config file loading (--no-config)");
}
return Ok(Config::default());
}
match ConfigManager::load(self.config_path) {
Ok(config) => Ok(config),
Err(e) => {
// If user explicitly specified a config file, fail hard
if self.config_path.is_some() {
anyhow::bail!("Failed to load config file: {e}");
}
// Otherwise, warn and use defaults
if self.verbose {
eprintln!("Warning: Failed to load config files: {e}");
eprintln!("Using default configuration");
}
Ok(Config::default())
}
}
}
}