tracevault_cli/
credentials.rs1use serde::{Deserialize, Serialize};
2use std::fs;
3use std::path::PathBuf;
4
5#[derive(Debug, Serialize, Deserialize)]
6pub struct Credentials {
7 pub server_url: String,
8 pub token: String,
9 pub email: String,
10 pub org_name: String,
11}
12
13impl Credentials {
14 pub fn path() -> PathBuf {
15 dirs::config_dir()
16 .unwrap_or_else(|| PathBuf::from("~/.config"))
17 .join("tracevault")
18 .join("credentials.json")
19 }
20
21 pub fn load() -> Option<Self> {
22 let path = Self::path();
23 let content = fs::read_to_string(&path).ok()?;
24 serde_json::from_str(&content).ok()
25 }
26
27 pub fn save(&self) -> Result<(), std::io::Error> {
28 let path = Self::path();
29 if let Some(parent) = path.parent() {
30 fs::create_dir_all(parent)?;
31 }
32 let json = serde_json::to_string_pretty(self)
33 .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
34 fs::write(&path, json)
35 }
36
37 pub fn delete() -> Result<(), std::io::Error> {
38 let path = Self::path();
39 if path.exists() {
40 fs::remove_file(&path)?;
41 }
42 Ok(())
43 }
44}