Skip to main content

fur_cli/security/
state.rs

1use std::fs;
2use std::path::Path;
3use serde_json::json;
4use serde_json::Value;
5
6pub fn lock_file() -> &'static str {
7    ".fur/lock.json"
8}
9
10pub fn is_locked() -> bool {
11
12    let path = Path::new(lock_file());
13
14    if !path.exists() {
15        return false;
16    }
17
18    let content = fs::read_to_string(path).unwrap_or_default();
19
20    if let Ok(v) = serde_json::from_str::<Value>(&content) {
21        return v["locked"].as_bool().unwrap_or(false);
22    }
23
24    false
25}
26
27pub fn write_lock() {
28
29    let data = json!({
30        "locked": true,
31        "algorithm": "AES-256-GCM",
32        "version": 1
33    });
34
35    fs::write(lock_file(), serde_json::to_string_pretty(&data).unwrap())
36        .unwrap();
37}
38
39pub fn remove_lock() {
40
41    let path = Path::new(lock_file());
42
43    if path.exists() {
44        let _ = fs::remove_file(path);
45    }
46}