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 #[serde(default)]
16 pub pane_order: Vec<String>,
17 #[serde(default)]
19 pub hidden_panes: Vec<String>,
20}
21
22fn path() -> Option<PathBuf> {
23 let home = std::env::var_os("HOME")?;
24 Some(
25 PathBuf::from(home)
26 .join(".config")
27 .join("databricks-tui")
28 .join("config.json"),
29 )
30}
31
32impl Config {
33 pub fn load() -> Self {
34 path()
35 .and_then(|p| std::fs::read_to_string(p).ok())
36 .and_then(|s| serde_json::from_str(&s).ok())
37 .unwrap_or_default()
38 }
39
40 pub fn save(&self) {
41 let Some(p) = path() else {
42 return;
43 };
44 if let Some(dir) = p.parent() {
45 let _ = std::fs::create_dir_all(dir);
46 restrict(dir, 0o700);
47 }
48 if let Ok(json) = serde_json::to_string_pretty(self) {
49 let _ = std::fs::write(&p, json);
50 restrict(&p, 0o600);
51 }
52 }
53}
54
55pub fn restrict(path: &std::path::Path, mode: u32) {
57 #[cfg(unix)]
58 {
59 use std::os::unix::fs::PermissionsExt;
60 let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode));
61 }
62 #[cfg(not(unix))]
63 let _ = (path, mode);
64}