add config stuff

This commit is contained in:
SadlyNotSappho 2023-10-17 11:37:24 -07:00
parent 8f7c4d4f84
commit 216f52d325
5 changed files with 1658 additions and 1 deletions

1534
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,3 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serde = { version = "1.0.189", features = ["derive"] }
serde_json = "1.0.107"
spotify-rs = "0.3.9"
which = "4.4.2"

98
src/lib.rs Normal file
View File

@ -0,0 +1,98 @@
pub mod structs;
use std::{fs, path::Path, process};
use structs::Config;
pub fn ensure_exists(path: &str) {
let replaced = path.replace('~', &get_home()[..]);
let p = Path::new(&replaced);
if !p.exists() {
match fs::create_dir_all(p) {
Ok(var) => var,
Err(why) => {
eprintln!("lib::ensure_exists: Couldn't create directory(ies) {path}: {why:?}");
process::exit(1)
}
}
}
}
pub fn read_config(file: &str) -> Config {
let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME
let read = match fs::read_to_string(replaced) {
Ok(var) => var,
Err(why) => {
eprintln!("lib::read_config: Couldn't read config file: {why:?}");
process::exit(1)
}
};
match serde_json::from_str(&read[..]) {
Ok(var) => var,
Err(why) => {
eprintln!("lib::read_config: Couldn't parse config file: {why:?}");
process::exit(1)
}
}
}
pub fn create_config(file: &str) -> 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); // Path without the filename
if file_path.parent().is_some() && !file_path.exists() {
// if the file_path has a parent folder
println!("lib::create_config: creating folder {path}");
match fs::create_dir_all(&path) {
Ok(var) => var,
Err(why) => {
eprintln!("lib::create_config: Couldn't create required folder {path}: {why:?}");
process::exit(1)
}
}
};
if file_path.exists() && !Path::new(&replaced).exists() {
let file_contents = "{\"client_id\": \"\",\"client_secret\": \"\"}";
match fs::write(replaced, file_contents) {
Ok(var) => var,
Err(why) => {
eprintln!("lib::create_config: Couldn't write data to config file: {why:?}");
process::exit(1)
}
}
true
} else {
false
}
}
pub fn get_home() -> String {
match std::env::consts::OS {
"linux" => match std::env::var("HOME") {
Ok(var) => var,
Err(why) => {
eprintln!("lib::get_home: Couldn't get $HOME on Linux: {why:?}");
process::exit(1);
}
},
"windows" => match std::env::var("userprofile") {
Ok(var) => var,
Err(why) => {
eprintln!("lib::get_home: Couldn't get $userprofile on Windows: {why:?}");
process::exit(1)
}
},
_ => match std::env::var("HOME") {
Ok(var) => var,
Err(why) => {
eprintln!("lib::get_home: Couldn't get $HOME on other OS: {why:?}");
process::exit(1)
}
},
}
}

View File

@ -1,3 +1,17 @@
use std::process;
fn main() { fn main() {
println!("Hello, world!"); // check for deps
let ytdlp_exists = which::which("yt-dlp").is_ok();
let ffmpeg_exists = which::which("ffmpeg").is_ok();
if !ytdlp_exists {
eprintln!("You need yt-dlp (https://github.com/yt-dlp/yt-dlp) to run this.");
process::exit(1);
}
if !ffmpeg_exists {
eprintln!("You need ffmpeg (https://ffmpeg.org, or check your distro's repos if on linux) to run this.");
process::exit(1);
}
} }

7
src/structs.rs Normal file
View File

@ -0,0 +1,7 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Config {
pub client_id: String,
pub client_secret: String,
}