vika_cli/config/
loader.rs

1use crate::config::model::Config;
2use crate::error::{ConfigError, Result, VikaError};
3use std::path::PathBuf;
4
5const CONFIG_FILE: &str = ".vika.json";
6
7pub fn load_config() -> Result<Config> {
8    let config_path = PathBuf::from(CONFIG_FILE);
9
10    if !config_path.exists() {
11        return Ok(Config::default());
12    }
13
14    let content = std::fs::read_to_string(&config_path)
15        .map_err(|e| VikaError::from(ConfigError::ReadError(e)))?;
16
17    let config: Config =
18        serde_json::from_str(&content).map_err(|e| VikaError::from(ConfigError::ParseError(e)))?;
19
20    Ok(config)
21}
22
23pub fn save_config(config: &Config) -> Result<()> {
24    let config_path = PathBuf::from(CONFIG_FILE);
25
26    // Ensure $schema is set (use default if not present)
27    let mut config_to_save = config.clone();
28    if config_to_save.schema.is_empty() {
29        config_to_save.schema = crate::config::model::default_schema();
30    }
31
32    let content = serde_json::to_string_pretty(&config_to_save)
33        .map_err(|e| VikaError::from(ConfigError::ParseError(e)))?;
34
35    std::fs::write(&config_path, content)
36        .map_err(|e| VikaError::from(ConfigError::ReadError(e)))?;
37
38    Ok(())
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use std::env;
45
46    #[test]
47    fn test_save_and_load_config() {
48        let temp_dir = tempfile::tempdir().unwrap();
49        let original_dir = env::current_dir().ok();
50
51        let _ = env::set_current_dir(&temp_dir);
52
53        // Save config should succeed
54        let mut config = Config::default();
55        config.specs = vec![crate::config::model::SpecEntry {
56            name: "test".to_string(),
57            path: "test.yaml".to_string(),
58            schemas: crate::config::model::SchemasConfig::default(),
59            apis: crate::config::model::ApisConfig::default(),
60            modules: crate::config::model::ModulesConfig::default(),
61        }];
62        if save_config(&config).is_ok() {
63            // Load config should succeed and match
64            if let Ok(loaded) = load_config() {
65                assert_eq!(loaded.root_dir, config.root_dir);
66                assert_eq!(loaded.specs.len(), config.specs.len());
67            }
68        }
69
70        if let Some(orig) = original_dir {
71            let _ = env::set_current_dir(orig);
72        }
73    }
74
75    #[test]
76    fn test_load_config_not_exists() {
77        let temp_dir = tempfile::tempdir().unwrap();
78        let original_dir = env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
79
80        env::set_current_dir(&temp_dir).unwrap();
81
82        let config = load_config().unwrap();
83        // Should return default config when file doesn't exist
84        assert_eq!(config.root_dir, "src");
85
86        env::set_current_dir(original_dir).unwrap();
87    }
88
89    #[test]
90    fn test_save_config_with_empty_schema() {
91        let temp_dir = tempfile::tempdir().unwrap();
92        let original_dir = env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
93
94        env::set_current_dir(&temp_dir).unwrap();
95
96        let config = Config {
97            schema: String::new(),
98            ..Default::default()
99        };
100        save_config(&config).unwrap();
101
102        let loaded = load_config().unwrap();
103        // Schema should be set to default
104        assert!(!loaded.schema.is_empty());
105
106        env::set_current_dir(original_dir).unwrap();
107    }
108}