1use crate::pkg::types;
2use anyhow::{Result, anyhow};
3use std::fs;
4
5fn get_lockfile_path() -> Result<std::path::PathBuf> {
6 Ok(std::env::current_dir()?.join("zoi.lock"))
7}
8
9pub fn read_zoi_lock() -> Result<types::ZoiLock> {
10 let path = get_lockfile_path()?;
11 if !path.exists() {
12 return Ok(types::ZoiLock {
13 version: "1".to_string(),
14 ..Default::default()
15 });
16 }
17 let content = fs::read_to_string(path)?;
18 if content.trim().is_empty() {
19 return Ok(types::ZoiLock {
20 version: "1".to_string(),
21 ..Default::default()
22 });
23 }
24
25 serde_json::from_str(&content).map_err(|e| {
26 anyhow!(
27 "Failed to parse zoi.lock. It might be corrupted or in an old format. Error: {}",
28 e
29 )
30 })
31}
32
33pub fn write_zoi_lock(lockfile: &types::ZoiLock) -> Result<()> {
34 let path = get_lockfile_path()?;
35 let content = serde_json::to_string_pretty(lockfile)?;
36 fs::write(path, content)?;
37 Ok(())
38}