From e338f92361c868b70534409fd3dbaddbe0b772c6 Mon Sep 17 00:00:00 2001 From: Junko Date: Tue, 21 Nov 2023 14:20:21 +0100 Subject: [PATCH] fixed logic for system get --- README.org | 202 +++++++++++++++++++++++++--------------------------- TODO.org | 40 +++++++++++ src/main.rs | 139 +++++++++++++++++++++++++----------- 3 files changed, 236 insertions(+), 145 deletions(-) create mode 100644 TODO.org diff --git a/README.org b/README.org index 9320f6b..b0bad8c 100644 --- a/README.org +++ b/README.org @@ -1,32 +1,6 @@ #+title: Pluralsync (Codename pluralshit) #+author: Jvnko -#+seq_todo: Backlog LowPriority HighPriority InProgress | Finished - -* Table of contents :toc_4: -- [[#about][About]] -- [[#configuration][Configuration]] -- [[#code][Code]] - - [[#cargotoml][Cargo.toml]] - - [[#mainrs][Main.rs]] - - [[#imports][Imports]] - - [[#constants][Constants]] - - [[#structs][Structs]] - - [[#main][Main]] - - [[#functions][Functions]] - - [[#main-commands][Main commands]] - - [[#pluralkit][Pluralkit]] - - [[#simplyplural][Simplyplural]] - - [[#utilities][Utilities]] - - [[#http-request-handler][Http Request handler]] -- [[#organization][Organization]] - - [[#tasks][Tasks]] - - [[#milestone-10-1119][Milestone 1.0]] - - [[#main-functions-1911][Main functions]] - - [[#commands-00100][Commands]] - - [[#utils-020][Utils]] - - [[#kanban][Kanban]] - * About * Configuration #+begin_src json :tangle config.example.json @@ -58,11 +32,16 @@ tokio = { version = "1", features = ["full"] } ** Main.rs *** Imports #+begin_src rust :tangle src/main.rs :comments link +use std::fs; +use std::fs::create_dir; use std::path::Path; -use std::{fs, collections::HashMap}; +use std::collections::HashMap; + use reqwest::header::{USER_AGENT, AUTHORIZATION}; + use serde::{Serialize, Deserialize}; use serde_json::Value; + use dirs; #+end_src @@ -76,6 +55,7 @@ const SP_URL: &str = "https://api.apparyllis.com/v1"; #+begin_src rust :tangle src/main.rs :comments link #[derive(Debug)] #[derive(Serialize)] +#[derive(Deserialize)] struct System { pk_userid: String, sp_userid: String, @@ -84,6 +64,7 @@ struct System { #[derive(Debug)] #[derive(Serialize)] +#[derive(Deserialize)] #[derive(Clone)] struct Member { pk_id: String, @@ -91,7 +72,6 @@ struct Member { name: String, alias: String } - #+end_src *** Main @@ -104,12 +84,17 @@ fn main() { Some(x) => { config_path = format!("{}/pluralshit", x.display()); match command.as_str() { - "sync" => sync(config_path), + "sync" => { + let _ = sync(config_path); + }, "set" => { if std::env::args().len() > 2 { - set_member(config_path, std::env::args().nth(2).expect("No member given")); + match set_member(config_path, std::env::args().nth(2).expect("No member given")) { + Ok(_) => (), + Err(e) => println!("{e}"), + } } else { - set_empty(config_path); + //set_empty(config_path); } }, &_ => println!("Invalid command"), @@ -126,25 +111,48 @@ fn main() { *** Functions **** Main commands #+begin_src rust :tangle src/main.rs :comments link -fn set_member(config_path: String, member: String) { - let config = load_json(format!("{}/config.json", config_path)); - let system = get_system(config_path); +fn set_member(config_path: String, member: String) -> Result<(), &'static str> { + let config = get_config(&config_path); + if config == Value::Null { + return Err("Config not found. Stopping"); + } + + let system: System = get_system(&config_path); + + let mut flag = false; + for mem in system.members { + if mem.name.to_lowercase() == member.to_lowercase() { + flag = true; + } + } + + if flag { + println!("Member {member} found"); + + + + Ok(()) + } else { + Err("Member {member} not found") + } - //for mem in system["members"].as_str().unwrap() { - // TODO - // TODO Hacer que devuelva el json en forma de Vec - // TODO - //} } +/* fn set_empty(config_path: String) { let config = load_json(format!("{}/config.json", config_path)); - let system = get_system(config_path); + let system = get_system(&config_path); } +*/ -fn sync(config_path: String) { +fn sync(config_path: String) -> Result<(), String>{ // Get config - let config = load_json(format!("{}/config.json", config_path)); + let config = get_config(&config_path); + if config == Value::Null { + println!("Stopping."); + return Ok(()); + } + let pk_key = &config["pk_key"].as_str().unwrap(); let sp_key = &config["sp_key"].as_str().unwrap(); @@ -189,6 +197,7 @@ fn sync(config_path: String) { let json = serde_json::to_string(&sys); let _ = fs::write(format!("{}/system.json", config_path), &json.unwrap()); + Ok(()) } #+end_src @@ -197,14 +206,14 @@ fn sync(config_path: String) { fn pk_get_system(key: &str) -> Value { let url = format!("{}/systems/@me", PK_URL); - let res = http_request(url,key); + let res = http_get_request(url,key); return serde_json::from_str(&res.unwrap()).unwrap(); } fn pk_get_members(key: &str, sysid: &str) -> Vec { let url = format!("{}/systems/{}/members", PK_URL, sysid); - let res = http_request(url,key); + let res = http_get_request(url,key); let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); return datas; @@ -216,7 +225,7 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec { fn sp_get_userid(key: &str) -> String { let url = format!("{}/me", SP_URL); - let res = http_request(url,key); + let res = http_get_request(url,key); let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap(); return json_res["id"].as_str().unwrap().to_string(); } @@ -224,7 +233,7 @@ fn sp_get_userid(key: &str) -> String { fn sp_get_memberids(key: &str, system_id: &str) -> HashMap { let url = format!("{}/members/{}", SP_URL, system_id); - let res = http_request(url,key); + let res = http_get_request(url,key); let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); let mut sp_memberdata: HashMap = HashMap::new(); @@ -253,7 +262,7 @@ fn get_sp_id(mem: &Member, ids: &HashMap) -> String { #+begin_src rust :tangle src/main.rs :comments link fn load_json(path: String) -> Value { if Path::new(&path).exists() { - let config_data = fs::read_to_string(path).expect("File not found"); + let config_data = fs::read_to_string(&path).expect("File not found"); return serde_json::from_str(&config_data).unwrap(); } else { println!("Config file in {path} not found"); @@ -261,38 +270,59 @@ fn load_json(path: String) -> Value { } } -// TODO -// TODO fn get_config() -> Value { -// TODO load_json -// TODO if Value:null - > mkconfig dir - > make empty config.json -// TODO else -> return -// TODO } -// TODO +fn get_config(config_path: &str) -> Value { + let path = format!("{}/config.json", config_path); + if Path::new(config_path).exists() { -fn get_system(config_path: String) -> Value{ + let result = load_json(String::from(&path)); + + if result == Value::Null { + println!("Config file missing, creating template in {path}"); + let _ = fs::write(path, r#" +{ + "pk_key": "// Pluralkit token", + "sp_key": "// Simplplural token" +} + "#); + } + + return result; + } else { + println!("Directory {config_path} does not exist. Creating with template config"); + let _ = create_dir(config_path); + let _ = fs::write(path, r#" +{ + "pk_key": "// Pluralkit token", + "sp_key": "// Simplplural token" +} + "#); + return Value::Null; + } +} + +fn get_system(config_path: &str) -> System { let path = format!("{}/system.json", config_path); - // TODO - // TODO load_json - // TODO if Value:null - > sync - > load_json - // TODO + let mut result = load_json(String::from(&path)); - if Path::new(&path).exists() { - let config_data = fs::read_to_string(path).expect("File not found"); - return serde_json::from_str(&config_data).unwrap(); - } else { - println!("System file in {path} not found. Syncing"); - sync(config_path); - let config_data = fs::read_to_string(path).expect("File not found"); - return serde_json::from_str(&config_data).unwrap(); + if result == Value::Null { + println!("Syncing system config"); + let _ = sync(String::from(config_path)); + result = load_json(String::from(&path)); } + + let vec = serde_json::to_vec(&result).unwrap(); + let sys = serde_json::from_slice::(&vec).unwrap(); + + return sys; + } #+end_src **** Http Request handler #+begin_src rust :tangle src/main.rs :comments link #[tokio::main] -async fn http_request(url: String, key: &str) -> Result> { +async fn http_get_request(url: String, key: &str) -> Result> { let client = reqwest::Client::new(); let res = client .get(url) @@ -305,39 +335,3 @@ async fn http_request(url: String, key: &str) -> Result { config_path = format!("{}/pluralshit", x.display()); match command.as_str() { - "sync" => sync(config_path), + "sync" => { + let _ = sync(config_path); + }, "set" => { if std::env::args().len() > 2 { - set_member(config_path, std::env::args().nth(2).expect("No member given")); + match set_member(config_path, std::env::args().nth(2).expect("No member given")) { + Ok(_) => (), + Err(e) => println!("{e}"), + } } else { - set_empty(config_path); + //set_empty(config_path); } }, &_ => println!("Invalid command"), @@ -61,25 +73,48 @@ fn main() { // Main:1 ends here // [[file:../README.org::*Main commands][Main commands:1]] -fn set_member(config_path: String, member: String) { - let config = load_json(format!("{}/config.json", config_path)); - let system = get_system(config_path); +fn set_member(config_path: String, member: String) -> Result<(), &'static str> { + let config = get_config(&config_path); + if config == Value::Null { + return Err("Config not found. Stopping"); + } + + let system: System = get_system(&config_path); + + let mut flag = false; + for mem in system.members { + if mem.name.to_lowercase() == member.to_lowercase() { + flag = true; + } + } + + if flag { + println!("Member {member} found"); + + + + Ok(()) + } else { + Err("Member {member} not found") + } - //for mem in system["members"].as_str().unwrap() { - // TODO - // TODO Hacer que devuelva el json en forma de Vec - // TODO - //} } +/* fn set_empty(config_path: String) { let config = load_json(format!("{}/config.json", config_path)); - let system = get_system(config_path); + let system = get_system(&config_path); } +*/ -fn sync(config_path: String) { +fn sync(config_path: String) -> Result<(), String>{ // Get config - let config = load_json(format!("{}/config.json", config_path)); + let config = get_config(&config_path); + if config == Value::Null { + println!("Stopping."); + return Ok(()); + } + let pk_key = &config["pk_key"].as_str().unwrap(); let sp_key = &config["sp_key"].as_str().unwrap(); @@ -124,6 +159,7 @@ fn sync(config_path: String) { let json = serde_json::to_string(&sys); let _ = fs::write(format!("{}/system.json", config_path), &json.unwrap()); + Ok(()) } // Main commands:1 ends here @@ -131,14 +167,14 @@ fn sync(config_path: String) { fn pk_get_system(key: &str) -> Value { let url = format!("{}/systems/@me", PK_URL); - let res = http_request(url,key); + let res = http_get_request(url,key); return serde_json::from_str(&res.unwrap()).unwrap(); } fn pk_get_members(key: &str, sysid: &str) -> Vec { let url = format!("{}/systems/{}/members", PK_URL, sysid); - let res = http_request(url,key); + let res = http_get_request(url,key); let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); return datas; @@ -149,7 +185,7 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec { fn sp_get_userid(key: &str) -> String { let url = format!("{}/me", SP_URL); - let res = http_request(url,key); + let res = http_get_request(url,key); let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap(); return json_res["id"].as_str().unwrap().to_string(); } @@ -157,7 +193,7 @@ fn sp_get_userid(key: &str) -> String { fn sp_get_memberids(key: &str, system_id: &str) -> HashMap { let url = format!("{}/members/{}", SP_URL, system_id); - let res = http_request(url,key); + let res = http_get_request(url,key); let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); let mut sp_memberdata: HashMap = HashMap::new(); @@ -185,7 +221,7 @@ fn get_sp_id(mem: &Member, ids: &HashMap) -> String { // [[file:../README.org::*Utilities][Utilities:1]] fn load_json(path: String) -> Value { if Path::new(&path).exists() { - let config_data = fs::read_to_string(path).expect("File not found"); + let config_data = fs::read_to_string(&path).expect("File not found"); return serde_json::from_str(&config_data).unwrap(); } else { println!("Config file in {path} not found"); @@ -193,37 +229,58 @@ fn load_json(path: String) -> Value { } } -// TODO -// TODO fn get_config() -> Value { -// TODO load_json -// TODO if Value:null - > mkconfig dir - > make empty config.json -// TODO else -> return -// TODO } -// TODO +fn get_config(config_path: &str) -> Value { + let path = format!("{}/config.json", config_path); + if Path::new(config_path).exists() { -fn get_system(config_path: String) -> Value{ + let result = load_json(String::from(&path)); + + if result == Value::Null { + println!("Config file missing, creating template in {path}"); + let _ = fs::write(path, r#" +{ + "pk_key": "// Pluralkit token", + "sp_key": "// Simplplural token" +} + "#); + } + + return result; + } else { + println!("Directory {config_path} does not exist. Creating with template config"); + let _ = create_dir(config_path); + let _ = fs::write(path, r#" +{ + "pk_key": "// Pluralkit token", + "sp_key": "// Simplplural token" +} + "#); + return Value::Null; + } +} + +fn get_system(config_path: &str) -> System { let path = format!("{}/system.json", config_path); - // TODO - // TODO load_json - // TODO if Value:null - > sync - > load_json - // TODO + let mut result = load_json(String::from(&path)); - if Path::new(&path).exists() { - let config_data = fs::read_to_string(path).expect("File not found"); - return serde_json::from_str(&config_data).unwrap(); - } else { - println!("System file in {path} not found. Syncing"); - sync(config_path); - let config_data = fs::read_to_string(path).expect("File not found"); - return serde_json::from_str(&config_data).unwrap(); + if result == Value::Null { + println!("Syncing system config"); + let _ = sync(String::from(config_path)); + result = load_json(String::from(&path)); } + + let vec = serde_json::to_vec(&result).unwrap(); + let sys = serde_json::from_slice::(&vec).unwrap(); + + return sys; + } // Utilities:1 ends here // [[file:../README.org::*Http Request handler][Http Request handler:1]] #[tokio::main] -async fn http_request(url: String, key: &str) -> Result> { +async fn http_get_request(url: String, key: &str) -> Result> { let client = reqwest::Client::new(); let res = client .get(url)