Skip to content

Commit 1e637ef

Browse files
committed
refactor: Simplify config loading logic in SurgeConfig
- Added default attribute to `http_api_key` for serialization. - Consolidated `from_env` and `load` methods to streamline configuration loading from files and environment variables. - Improved handling of default configuration paths and environment variable precedence.
1 parent 2751c4c commit 1e637ef

1 file changed

Lines changed: 29 additions & 34 deletions

File tree

src/config/config.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct SurgeConfig {
2323
pub http_api_port: u16,
2424

2525
/// HTTP API key
26+
#[serde(default)]
2627
pub http_api_key: String,
2728

2829
/// surge-cli path
@@ -89,55 +90,49 @@ impl Config {
8990
Ok(config)
9091
}
9192

92-
/// Load config from environment variables
93-
pub fn from_env() -> Self {
94-
let mut config = Config::default();
95-
93+
/// Load config (file + env var overlay)
94+
pub fn load(config_path: Option<PathBuf>) -> anyhow::Result<Self> {
95+
let file_config = if let Some(path) = config_path {
96+
if path.exists() {
97+
Self::from_file(&path).ok()
98+
} else {
99+
None
100+
}
101+
} else {
102+
// Build default paths with proper ~ expansion
103+
let home = std::env::var("HOME").unwrap_or_default();
104+
let default_paths = vec![
105+
PathBuf::from("surge-tui.toml"),
106+
PathBuf::from(format!("{}/.config/surge-tui/surge-tui.toml", home)),
107+
PathBuf::from(format!("{}/.config/surge-tui/config.toml", home)),
108+
];
109+
110+
default_paths
111+
.into_iter()
112+
.filter(|p| p.exists())
113+
.find_map(|p| Self::from_file(&p).ok())
114+
};
115+
116+
// Start from file config or defaults
117+
let mut config = file_config.unwrap_or_default();
118+
119+
// Always overlay env vars (env takes precedence over file for key/host/port)
96120
if let Ok(host) = std::env::var("SURGE_HTTP_API_HOST") {
97121
config.surge.http_api_host = host;
98122
}
99-
100123
if let Ok(port) = std::env::var("SURGE_HTTP_API_PORT") {
101124
if let Ok(port) = port.parse() {
102125
config.surge.http_api_port = port;
103126
}
104127
}
105-
106128
if let Ok(key) = std::env::var("SURGE_HTTP_API_KEY") {
107129
config.surge.http_api_key = key;
108130
}
109-
110131
if let Ok(path) = std::env::var("SURGE_CLI_PATH") {
111132
config.surge.cli_path = Some(path);
112133
}
113134

114-
config
115-
}
116-
117-
/// Load config (prioritize file, fallback to environment variables)
118-
pub fn load(config_path: Option<PathBuf>) -> anyhow::Result<Self> {
119-
if let Some(path) = config_path {
120-
if path.exists() {
121-
return Self::from_file(&path);
122-
}
123-
}
124-
125-
// Try default paths
126-
let default_paths = vec![
127-
PathBuf::from("surge-tui.toml"),
128-
PathBuf::from("~/.config/surge-tui/config.toml"),
129-
];
130-
131-
for path in default_paths {
132-
if path.exists() {
133-
if let Ok(config) = Self::from_file(&path) {
134-
return Ok(config);
135-
}
136-
}
137-
}
138-
139-
// Fallback to environment variables
140-
Ok(Self::from_env())
135+
Ok(config)
141136
}
142137

143138
/// Generate example config file

0 commit comments

Comments
 (0)