read config, ensure cache exists, fuck lifetimes
This commit is contained in:
parent
eea467fc9a
commit
d9b34538c1
|
@ -11,3 +11,5 @@ iced = "0.9.0"
|
|||
regex = "1.8.4"
|
||||
reqwest = "0.11.18"
|
||||
scraper = "0.16.0"
|
||||
serde = { version = "1.0.164", features = ["derive"] }
|
||||
serde_json = "1.0.97"
|
||||
|
|
57
src/lib.rs
57
src/lib.rs
|
@ -1,4 +1,6 @@
|
|||
use std::fs;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub struct ConfigFile {
|
||||
pub latest_date: String,
|
||||
|
@ -8,32 +10,51 @@ pub struct ConfigFile {
|
|||
pub fn get_home() -> String {
|
||||
let home = match std::env::consts::OS {
|
||||
"linux" => std::env::var("HOME").expect("how is there not a $HOME"),
|
||||
"windows" => std::env::var("userprofile").expect("yell at me if this doesn't work please i'm not going to touch windows"),
|
||||
_ => std::env::var("HOME").expect("if this doesn't work, you're probably on macos, which i literally cannot test on"),
|
||||
"windows" => std::env::var("userprofile")
|
||||
.expect("yell at me if this doesn't work please i'm not going to touch windows"),
|
||||
_ => std::env::var("HOME").expect(
|
||||
"if this doesn't work, you're probably on macos, which i literally cannot test on",
|
||||
),
|
||||
};
|
||||
// println!("{home}");
|
||||
home
|
||||
}
|
||||
|
||||
pub fn create_config(file: String) -> bool {
|
||||
let replaced = file.replace('~', &get_home()[..]);
|
||||
let mut paths = replaced.split('/').collect::<Vec<&str>>();
|
||||
let filename = paths.pop();
|
||||
pub fn create_config(file: &String) -> bool {
|
||||
let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME
|
||||
let mut paths = replaced.split('/').collect::<Vec<&str>>(); // split path by /
|
||||
paths.pop();
|
||||
let path = paths.join("/");
|
||||
let file_path = std::path::Path::new(&path);
|
||||
let file_contents = "{\"latest_date\": \"20021104\",\"cache_folder\": \"~/.cache/ggg\"}";
|
||||
if let Some(p) = file_path.parent() {
|
||||
fs::create_dir_all(p).expect("couldn't create needed folders")
|
||||
let file_path = std::path::Path::new(&path); // Path without the filename
|
||||
|
||||
if let Some(_) = file_path.parent() {
|
||||
// if the file_path has a parent folder
|
||||
fs::create_dir_all(&path).expect("couldn't create needed folders") // create folders
|
||||
};
|
||||
|
||||
let cfg_path = std::path::Path::new(format!("{}/{}", file_path.display(), filename.expect("filename somehow died, if you know how please tell me")));
|
||||
|
||||
if cfg_path.exists() {
|
||||
false
|
||||
} else {
|
||||
|
||||
fs::write(cfg_path, file_contents).expect("couldn't write to config file");
|
||||
if file_path.exists() && !Path::new(&replaced).exists() {
|
||||
let file_contents = "{\"latest_date\": \"20021104\",\"cache_folder\": \"~/.cache/ggg\"}";
|
||||
fs::write(replaced, file_contents).expect("couldn't write to config file");
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_config(file: &String) -> Config {
|
||||
let read = fs::read_to_string(file).expect("couldn't read config file");
|
||||
serde_json::from_str(&read[..]).expect("couldn't parse json")
|
||||
}
|
||||
|
||||
pub fn ensure_exists(path: String) {
|
||||
let p = Path::new(&path);
|
||||
if !p.exists() {
|
||||
fs::create_dir_all(p).expect("couldn't create folder")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub latest_date: String,
|
||||
pub cache_folder: String,
|
||||
}
|
||||
|
|
|
@ -12,5 +12,7 @@ fn main() {
|
|||
|
||||
let config_file = args.config_file;
|
||||
|
||||
ggg::create_config(config_file);
|
||||
ggg::create_config(&config_file);
|
||||
let config = ggg::read_config(&config_file);
|
||||
ggg::ensure_exists(config.cache_folder)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue