1use std::env;
2
3pub fn home_dir() -> Result<String, Box<dyn std::error::Error>> {
4 let home_dir = if cfg!(target_os = "windows") {
5 format!("{}/AppData/Local/wei/", std::env::var("USERPROFILE")?)
6 } else {
7 format!("{}/.wei/", std::env::var("HOME")?)
8 };
9 fs::create_dir_all(home_dir.clone())?;
10 Ok(home_dir)
11}
12
13pub fn dir_daemon() -> String {
14 let dir = format!("{}daemon.dat", home_dir().unwrap());
15 let path = Path::new(&dir);
16 if path.exists() {
17 return path.display().to_string();
18 }
19
20 "./daemon.dat".to_string()
21}
22
23pub fn status() -> String {
25 let path_status = dir_status();
27 let path = Path::new(&path_status);
28 if !path.exists() {
29 write(&path_status, "status", "1").unwrap();
30 }
31 read(&path_status, "status").unwrap()
32}
33
34pub fn task_start() {
35 write(&task_path(), "status", "1").unwrap();
36}
37
38pub fn task_stop() {
39 write(&task_path(), "status", "0").unwrap();
40}
41
42pub fn task_status() -> String {
43 let path_status = task_path();
45 let path = Path::new(&path_status);
46 if !path.exists() {
47 write(&path_status, "status", "1").unwrap();
48 }
49 read(&path_status, "status").unwrap()
50}
51
52pub fn task_path() -> String {
53 format!("{}task.dat", home_dir().unwrap())
54}
55
56pub fn start() {
58 write(&dir_status(), "status", "1").unwrap();
59}
60
61pub fn stop() {
63 write(&dir_status(), "status", "0").unwrap();
64}
65
66pub fn dir_status() -> String {
67 format!("{}status.dat", home_dir().unwrap())
68}
69
70pub fn dir_uuid() -> String {
71 format!("{}uuid.dat", home_dir().unwrap())
72}
73
74pub fn dir_user() -> String {
75 format!("{}user.dat", home_dir().unwrap())
76}
77
78pub fn dir_bin() -> String {
79 format!("{}bin.dat", home_dir().unwrap())
80}
81
82use std::fs::{self, File};
83use std::io::{self, Write};
84use std::path::Path;
85use serde_yaml::Value;
86
87pub fn read(dir: &str, key: &str) -> Result<String, io::Error> {
89 let expanded_path = Path::new(dir);
90
91 if let Some(parent) = expanded_path.parent() {
93 if !parent.exists() {
94 fs::create_dir_all(&parent)?;
95 }
96 }
97
98 if !expanded_path.exists() {
99 File::create(&expanded_path)?.write_all(b"---\n")?;
100 }
101
102 let content = fs::read_to_string(&expanded_path)?;
103 let yaml: Value = serde_yaml::from_str(&content).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
104
105 Ok(yaml.get(key).and_then(Value::as_str).unwrap_or("").to_string())
106}
107
108pub fn read_value(dir: &str, key: &str) -> Result<Option<Value>, io::Error> {
110 let expanded_path = Path::new(dir);
111
112 if let Some(parent) = expanded_path.parent() {
114 if !parent.exists() {
115 fs::create_dir_all(&parent)?;
116 }
117 }
118
119 if !expanded_path.exists() {
120 File::create(&expanded_path)?.write_all(b"---\n")?;
121 }
122
123 let content = fs::read_to_string(&expanded_path)?;
124 let yaml: Value = serde_yaml::from_str(&content).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
125
126 Ok(yaml.get(key).cloned())
127}
128
129pub fn write(file_path: &str, key: &str, value: &str) -> Result<(), io::Error> {
131 let expanded_path = Path::new(file_path);
132
133 if let Some(parent) = expanded_path.parent() {
135 if !parent.exists() {
136 fs::create_dir_all(&parent)?;
137 }
138 }
139
140 if !expanded_path.exists() {
141 File::create(&expanded_path)?.write_all(b"---\n")?;
142 }
143
144 let content = fs::read_to_string(&expanded_path)?;
145 let mut yaml: Value = serde_yaml::from_str(&content).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
146
147 yaml[key] = Value::String(value.to_string());
149
150 fs::write(&expanded_path, serde_yaml::to_string(&yaml).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?)?;
151
152 Ok(())
153}
154
155pub fn bin_init(name: &str) {
157 let mut path = std::env::current_dir().unwrap();
158 path.push("./src/main.rs");
159 if path.exists() {
160 write(&dir_bin(), name, &env::current_exe().unwrap().display().to_string()).unwrap();
161 }
162}