Init commit
This commit is contained in:
commit
6c5536733f
19 changed files with 3312 additions and 0 deletions
60
bakery/src/state.rs
Normal file
60
bakery/src/state.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
use anyhow::{Context, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct InstalledPackage {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub binaries: Vec<String>,
|
||||
pub services: Vec<String>,
|
||||
pub installed_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize)]
|
||||
pub struct State {
|
||||
pub packages: HashMap<String, InstalledPackage>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn load() -> Result<Self> {
|
||||
let path = state_path();
|
||||
if !path.exists() {
|
||||
return Ok(Self::default());
|
||||
}
|
||||
let text = std::fs::read_to_string(&path).context("reading installed.json")?;
|
||||
serde_json::from_str(&text).context("parsing installed.json")
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
let path = state_path();
|
||||
if let Some(dir) = path.parent() {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
}
|
||||
let text = serde_json::to_string_pretty(self)?;
|
||||
std::fs::write(&path, text).context("writing installed.json")
|
||||
}
|
||||
|
||||
pub fn is_installed(&self, name: &str) -> bool {
|
||||
self.packages.contains_key(name)
|
||||
}
|
||||
|
||||
pub fn record(&mut self, pkg: InstalledPackage) {
|
||||
self.packages.insert(pkg.name.clone(), pkg);
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, name: &str) -> Option<InstalledPackage> {
|
||||
self.packages.remove(name)
|
||||
}
|
||||
}
|
||||
|
||||
fn state_path() -> PathBuf {
|
||||
dirs::state_dir()
|
||||
.unwrap_or_else(|| {
|
||||
dirs::home_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("~"))
|
||||
.join(".local/state")
|
||||
})
|
||||
.join("bakery/installed.json")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue