1use crate::Result;
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7pub trait ConfigManager: Send + Sync {
9 fn get_tool_config(&self, tool_name: &str) -> Result<Option<ToolConfig>>;
11
12 fn set_tool_config(&mut self, tool_name: &str, config: ToolConfig) -> Result<()>;
14
15 fn get_global_config(&self) -> Result<GlobalConfig>;
17
18 fn set_global_config(&mut self, config: GlobalConfig) -> Result<()>;
20
21 fn save(&self) -> Result<()>;
23
24 fn load(&mut self) -> Result<()>;
26
27 fn config_path(&self) -> PathBuf;
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct ToolConfig {
34 pub name: String,
36
37 pub default_version: Option<String>,
39
40 pub download_urls: std::collections::HashMap<String, String>,
42
43 pub install_dir: Option<PathBuf>,
45
46 pub environment: std::collections::HashMap<String, String>,
48
49 pub settings: std::collections::HashMap<String, serde_json::Value>,
51
52 pub auto_install: bool,
54
55 pub auto_update: bool,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct GlobalConfig {
62 pub install_dir: PathBuf,
64
65 pub cache_dir: PathBuf,
67
68 pub config_dir: PathBuf,
70
71 pub use_system_path: bool,
73
74 pub isolation_level: crate::package_manager::IsolationLevel,
76
77 pub proxy: Option<ProxyConfig>,
79
80 pub update_check: UpdateCheckConfig,
82
83 pub telemetry: TelemetryConfig,
85
86 pub plugins: PluginConfig,
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ProxyConfig {
93 pub http_proxy: Option<String>,
94 pub https_proxy: Option<String>,
95 pub no_proxy: Vec<String>,
96}
97
98#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct UpdateCheckConfig {
101 pub enabled: bool,
102 pub frequency: UpdateFrequency,
103 pub include_prerelease: bool,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
108pub enum UpdateFrequency {
109 Never,
110 Daily,
111 Weekly,
112 Monthly,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct TelemetryConfig {
118 pub enabled: bool,
119 pub anonymous: bool,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize)]
124pub struct PluginConfig {
125 pub enabled_plugins: Vec<String>,
126 pub plugin_dirs: Vec<PathBuf>,
127 pub auto_discover: bool,
128}
129
130impl Default for ToolConfig {
131 fn default() -> Self {
132 Self {
133 name: String::new(),
134 default_version: None,
135 download_urls: std::collections::HashMap::new(),
136 install_dir: None,
137 environment: std::collections::HashMap::new(),
138 settings: std::collections::HashMap::new(),
139 auto_install: true,
140 auto_update: false,
141 }
142 }
143}
144
145impl Default for GlobalConfig {
146 fn default() -> Self {
147 let home_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
148 let vx_dir = home_dir.join(".vx");
149
150 Self {
151 install_dir: vx_dir.join("tools"),
152 cache_dir: vx_dir.join("cache"),
153 config_dir: vx_dir.join("config"),
154 use_system_path: false,
155 isolation_level: crate::package_manager::IsolationLevel::Project,
156 proxy: None,
157 update_check: UpdateCheckConfig {
158 enabled: true,
159 frequency: UpdateFrequency::Weekly,
160 include_prerelease: false,
161 },
162 telemetry: TelemetryConfig {
163 enabled: false,
164 anonymous: true,
165 },
166 plugins: PluginConfig {
167 enabled_plugins: Vec::new(),
168 plugin_dirs: vec![vx_dir.join("plugins")],
169 auto_discover: true,
170 },
171 }
172 }
173}