Skip to main content

sync_auth/
config.rs

1//! Configuration for sync-auth.
2
3use std::path::PathBuf;
4
5/// Configuration for the sync engine.
6#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
7#[serde(default)]
8pub struct SyncConfig {
9    /// URL of the Git repository to sync credentials through.
10    pub repo_url: String,
11
12    /// Local path where the sync repository is cloned.
13    pub local_path: PathBuf,
14
15    /// List of provider names to sync (e.g. \["gh", "claude"\]).
16    /// Empty means sync all available providers.
17    pub providers: Vec<String>,
18
19    /// Whether to use shallow clone (--depth 1) for initial clone.
20    pub shallow_clone: bool,
21
22    /// Git branch to use for sync.
23    pub branch: String,
24
25    /// Interval in seconds for watch mode.
26    pub watch_interval_secs: u64,
27}
28
29impl Default for SyncConfig {
30    fn default() -> Self {
31        Self {
32            repo_url: String::new(),
33            local_path: default_sync_path(),
34            providers: Vec::new(),
35            shallow_clone: true,
36            branch: "main".to_string(),
37            watch_interval_secs: 60,
38        }
39    }
40}
41
42/// Returns the default path for the sync repository.
43fn default_sync_path() -> PathBuf {
44    dirs::data_local_dir()
45        .unwrap_or_else(|| PathBuf::from("/tmp"))
46        .join("sync-auth")
47        .join("repo")
48}
49
50impl SyncConfig {
51    /// Load config from a TOML file, falling back to defaults for missing fields.
52    pub fn load_from_file(path: &std::path::Path) -> Result<Self, crate::SyncError> {
53        if !path.exists() {
54            return Err(crate::SyncError::Config(format!(
55                "config file not found: {}",
56                path.display()
57            )));
58        }
59        let content =
60            std::fs::read_to_string(path).map_err(|e| crate::SyncError::Config(e.to_string()))?;
61        toml::from_str(&content)
62            .map_err(|e| crate::SyncError::Config(format!("invalid config TOML: {e}")))
63    }
64
65    /// Returns the default config file path.
66    pub fn default_config_path() -> PathBuf {
67        dirs::config_dir()
68            .unwrap_or_else(|| PathBuf::from("~/.config"))
69            .join("sync-auth")
70            .join("config.toml")
71    }
72}