| 1 | use std::{fs, path::PathBuf}; |
| 2 | |
| 3 | fn path() -> Option<PathBuf> { |
| 4 | dirs::config_dir().map(|p| p.join("onde").join("token")) |
| 5 | } |
| 6 | |
| 7 | pub fn load() -> Option<String> { |
| 8 | let content = fs::read_to_string(path()?).ok()?; |
| 9 | let trimmed = content.trim().to_string(); |
| 10 | if trimmed.is_empty() { |
| 11 | None |
| 12 | } else { |
| 13 | Some(trimmed) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | pub fn save(token: &str) -> std::io::Result<()> { |
| 18 | let path = path() |
| 19 | .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "no config directory"))?; |
| 20 | if let Some(parent) = path.parent() { |
| 21 | fs::create_dir_all(parent)?; |
| 22 | } |
| 23 | fs::write(path, token) |
| 24 | } |
| 25 | |
| 26 | pub fn clear() { |
| 27 | if let Some(path) = path() { |
| 28 | let _ = fs::remove_file(path); |
| 29 | } |
| 30 | } |