1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::path::PathBuf;
4
5#[derive(Debug, Default, Clone, Serialize, Deserialize)]
8pub struct Config {
9 pub theme: Option<String>,
11 #[serde(default)]
13 pub warehouses: HashMap<String, (String, String)>,
14}
15
16fn path() -> Option<PathBuf> {
17 let home = std::env::var_os("HOME")?;
18 Some(
19 PathBuf::from(home)
20 .join(".config")
21 .join("databricks-tui")
22 .join("config.json"),
23 )
24}
25
26impl Config {
27 pub fn load() -> Self {
28 path()
29 .and_then(|p| std::fs::read_to_string(p).ok())
30 .and_then(|s| serde_json::from_str(&s).ok())
31 .unwrap_or_default()
32 }
33
34 pub fn save(&self) {
35 let Some(p) = path() else {
36 return;
37 };
38 if let Some(dir) = p.parent() {
39 let _ = std::fs::create_dir_all(dir);
40 restrict(dir, 0o700);
41 }
42 if let Ok(json) = serde_json::to_string_pretty(self) {
43 let _ = std::fs::write(&p, json);
44 restrict(&p, 0o600);
45 }
46 }
47}
48
49pub fn restrict(path: &std::path::Path, mode: u32) {
51 #[cfg(unix)]
52 {
53 use std::os::unix::fs::PermissionsExt;
54 let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode));
55 }
56 #[cfg(not(unix))]
57 let _ = (path, mode);
58}