use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Config {
pub theme: Option<String>,
#[serde(default)]
pub warehouses: HashMap<String, (String, String)>,
#[serde(default)]
pub pane_order: Vec<String>,
#[serde(default)]
pub hidden_panes: Vec<String>,
}
fn path() -> Option<PathBuf> {
let home = std::env::var_os("HOME")?;
Some(
PathBuf::from(home)
.join(".config")
.join("databricks-tui")
.join("config.json"),
)
}
impl Config {
pub fn load() -> Self {
path()
.and_then(|p| std::fs::read_to_string(p).ok())
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
}
pub fn save(&self) {
let Some(p) = path() else {
return;
};
if let Some(dir) = p.parent() {
let _ = std::fs::create_dir_all(dir);
restrict(dir, 0o700);
}
if let Ok(json) = serde_json::to_string_pretty(self) {
let _ = std::fs::write(&p, json);
restrict(&p, 0o600);
}
}
}
pub fn restrict(path: &std::path::Path, mode: u32) {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode));
}
#[cfg(not(unix))]
let _ = (path, mode);
}