add a function that hopefully gets the home folder, and a function that *should* make the config file
This commit is contained in:
parent
c6eb64eb7e
commit
eea467fc9a
|
@ -6,3 +6,8 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.3.3", features = ["derive"] }
|
||||
iced = "0.9.0"
|
||||
regex = "1.8.4"
|
||||
reqwest = "0.11.18"
|
||||
scraper = "0.16.0"
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
use std::fs;
|
||||
|
||||
pub struct ConfigFile {
|
||||
pub latest_date: String,
|
||||
pub cache_folder: String,
|
||||
}
|
||||
|
||||
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"),
|
||||
};
|
||||
// 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();
|
||||
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 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");
|
||||
true
|
||||
}
|
||||
|
||||
}
|
17
src/main.rs
17
src/main.rs
|
@ -1,3 +1,16 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(short, long, default_value = "~/.config/ggg/config.json")]
|
||||
config_file: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let config_file = args.config_file;
|
||||
|
||||
ggg::create_config(config_file);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue