From b0e080d1eea7a731c5912efe8f7c0d9b30c0b997 Mon Sep 17 00:00:00 2001 From: Junko Date: Wed, 22 Nov 2023 13:11:49 +0100 Subject: [PATCH] set command --- Cargo.toml | 2 +- README.org | 122 +++++++++++++++++++++++++++++++--------------------- TODO.org | 54 +++++++++++++---------- src/main.rs | 113 ++++++++++++++++++++++++++++++++++-------------- 4 files changed, 185 insertions(+), 106 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 71ca25e..0129c7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,6 @@ edition = "2021" dirs = "5.0.1" serde = { version = "1.0.192", features = ["derive"] } serde_json = "1.0" -reqwest = "0.11.22" +reqwest = { version = "0.11.22", features = ["json"] } rofi = "0.3.0" tokio = { version = "1", features = ["full"] } diff --git a/README.org b/README.org index 947f61f..f5f5933 100644 --- a/README.org +++ b/README.org @@ -24,7 +24,7 @@ edition = "2021" dirs = "5.0.1" serde = { version = "1.0.192", features = ["derive"] } serde_json = "1.0" -reqwest = "0.11.22" +reqwest = { version = "0.11.22", features = ["json"] } rofi = "0.3.0" tokio = { version = "1", features = ["full"] } #+end_src @@ -79,7 +79,8 @@ struct Member { #+begin_src rust :tangle src/main.rs :comments link fn main() { if std::env::args().len() > 1 { - let command = std::env::args().nth(1).expect("No command given"); + let augs: Vec = std::env::args().collect(); + let command = &augs[1]; let config_path: String; match dirs::config_dir() { Some(x) => { @@ -90,7 +91,7 @@ fn main() { }, "set" => { if std::env::args().len() > 2 { - match set_member(config_path, std::env::args().nth(2).expect("No member given")) { + match set_member(config_path, &augs[2..]) { Ok(_) => (), Err(e) => println!("{e}"), } @@ -179,7 +180,7 @@ TODO let system = get_system(&config_path); TODO } */ -fn set_member(config_path: String, member: String) -> Result<(), &'static str> { +fn set_member(config_path: String, tf_members: &[String]) -> Result<(), &'static str> { let config = get_config(&config_path); if config == Value::Null { return Err("Config not found. Stopping"); @@ -187,30 +188,34 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> { 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; + let mut to_front: Vec = Vec::new(); + for member in tf_members { + for mem in &system.members { + if mem.name.to_lowercase() == member.to_lowercase() || mem.alias.to_lowercase() == member.to_lowercase() { + println!("Member {member} found"); + to_front.push(mem.clone()); + + break; + } } } + if to_front.len() != 0 { + let fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system); - if flag { - println!("Member {member} found"); - get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system); - - - Ok(()) - } else { - Err("Member {member} not found") + pk_set_fronters(&config["pk_key"].as_str().unwrap(), &system, to_front, &fronters); } - + Ok(()) } fn memberlist(config_path: String) { let sys = get_system(&config_path); for mem in sys.members { - println!("{}", mem.name); + if mem.name != mem.alias { + println!("{} / {}", mem.name, mem.alias); + } else { + println!("{}", mem.name); + } } } @@ -238,22 +243,22 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec { ***** Get fronters #+begin_src rust :tangle src/main.rs :comments link -fn pk_get_fronters(key: &str, sysid: &str) -> Vec { - let url = format!("{}/systems/{}/fronters", PK_URL, sysid); +fn sp_get_fronters(key: &str, sys: &System) -> Vec { + let url = format!("{}/fronters", SP_URL); let res = http_get_request(url,key); - let data: Value = serde_json::from_str(&res.unwrap()).unwrap(); - println!("{:?}", data); - let memberdata = &data["members"].as_array(); + let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); - let mut members: Vec = Vec::new(); - for member in memberdata { - for m in member.into_iter() { - let mname = m["name"].as_str().unwrap(); - members.push(String::from(mname)); + let mut members: Vec = Vec::new(); + for data in datas { + let sp_id = &data["content"]["member"].as_str().unwrap(); + for member in &sys.members { + if &member.sp_id == sp_id { + members.push(member.clone()); + } } - } + } return members; } #+end_src @@ -261,25 +266,12 @@ fn pk_get_fronters(key: &str, sysid: &str) -> Vec { **** Simplyplural ***** Get user ID #+begin_src rust :tangle src/main.rs :comments link -fn sp_get_fronters(key: &str, sys: &System) -> Vec { - let url = format!("{}/fronters", SP_URL); +fn sp_get_userid(key: &str) -> String { + let url = format!("{}/me", SP_URL); let res = http_get_request(url,key); - let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); - - let mut members: Vec = Vec::new(); - for data in datas { - let sp_id = &data["content"]["member"].as_str().unwrap(); - let mut push_name = String::new(); - for member in &sys.members { - if &member.sp_id == sp_id { - push_name = String::from(&member.name); - } - } - members.push(push_name); - - } - return members; + let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap(); + return json_res["id"].as_str().unwrap().to_string(); } #+end_src @@ -407,11 +399,10 @@ fn get_system(config_path: &str) -> System { } -fn get_fronters(pk_key: &str, pk_sysid: &str, sp_key: &str, sys: &System) -> HashMap> { - let mut fronters: HashMap> = HashMap::new(); - fronters.insert(String::from("pk"), pk_get_fronters(pk_key, pk_sysid)); +fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> HashMap> { + let mut fronters: HashMap> = HashMap::new(); + fronters.insert(String::from("pk"), pk_get_fronters(pk_key, sys)); fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys)); - println!("{:?}", fronters); return fronters; } @@ -432,4 +423,35 @@ async fn http_get_request(url: String, key: &str) -> Result>) -> Result<(), Box> { + let client = reqwest::Client::new(); + let _ = client + .post(url) + .json(body) + .header(USER_AGENT, "Pluralsync") + .header(AUTHORIZATION, key) + .send() + .await?; + Ok(()) +} + +/* +#[tokio::main] +async fn http_patch_request(url: String, key: &str) -> Result> { + let client = reqwest::Client::new(); + let res = client + .patch(url) + .body("wiwiwiw") + .header(USER_AGENT, "Pluralsync") + .header(AUTHORIZATION, key) + .send() + .await? + .text() + .await?; + Ok(res) +} +*/ #+end_src diff --git a/TODO.org b/TODO.org index 505a2f7..6ce4622 100644 --- a/TODO.org +++ b/TODO.org @@ -8,44 +8,52 @@ :PROPERTIES: :COOKIE_DATA: recursive :END: -*** Milestone 1.0 [5/20][25%] -**** Main functions [2/10][20%] +*** Milestone 1.0 [10/22][45%] +**** Main functions [4/11][36%] ***** Finished Add `sync` command -***** InProgress Add `set` command [0/2] -****** LowPriority [SET] Add empty -****** InProgress [SET] Add set -***** LowPriority Add `get` command +***** HighPriority Add `set` command [2/3] +****** HighPriority [SET] Add empty +****** Finished [SET] Add set +****** Finished Adapt to set multiple arguments +***** HighPriority Add `get` command ***** LowPriority Add `add` command [0/2] ****** LowPriority [ADD] Add empty ****** LowPriority [ADD] Add set ***** LowPriority Add `setgroup` command ***** Finished Add `memberlist` command -**** Utils [3/9][33%] +**** Utils [6/10][60%] ***** Json loading [2/2] ****** Finished Add new function to get the config create empty in path if not exists ****** Finished `Get system` if Value::Null sync and load json ***** Finished Get current front -***** InProgress http PATCH request -***** InProgress http POST request +***** Finished http PATCH request +***** Finished http POST request +***** Finished Set members know the alias ***** HighPriority Compare if sent members are currently fronting and keep them otherwise add to fronting array ***** HighPriority Check for mismatch in fronting between pluralkit and simplyplural ***** LowPriority Get fronters handles the front file -***** LowPriority Rofi stuff +***** HighPriority Rofi stuff **** Doc [0/1][0%] ***** Functions [0/1] -****** Backlog separate memberlist into its own code block in the readme +****** Backlog Separate memberlist into its own code block in the readme ** Kanban -| Backlog | LowPriority | HighPriority | InProgress | Finished | -|--------------------------------+--------------------------------+--------------------------------+-------------------------+--------------------------------| -| [[/home/alicia/git/pluralshit/TODO.org::separate memberlist into its own code block in the readme][separate memberlist into its o]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add empty][{SET} Add empty]] | [[/home/alicia/git/pluralshit/TODO.org::Compare if sent members are currently fronting and keep them otherwise add to fronting array][Compare if sent members are cu]] | [[/home/alicia/git/pluralshit/TODO.org::Add `set` command \[0/2\]][Add `set` command {0/2}]] | [[/home/alicia/git/pluralshit/TODO.org::Add `sync` command][Add `sync` command]] | -| | [[/home/alicia/git/pluralshit/TODO.org::Add `get` command][Add `get` command]] | [[/home/alicia/git/pluralshit/TODO.org::Check for mismatch in fronting between pluralkit and simplyplural][Check for mismatch in fronting]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add set][{SET} Add set]] | [[/home/alicia/git/pluralshit/TODO.org::Add `memberlist` command][Add `memberlist` command]] | -| | [[/home/alicia/git/pluralshit/TODO.org::Add `add` command \[0/2\]][Add `add` command {0/2}]] | | [[/home/alicia/git/pluralshit/TODO.org::http PATCH request][http PATCH request]] | [[/home/alicia/git/pluralshit/TODO.org::Add new function to get the config create empty in path if not exists][Add new function to get the co]] | -| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add empty][{ADD} Add empty]] | | [[/home/alicia/git/pluralshit/TODO.org::http POST request][http POST request]] | [[/home/alicia/git/pluralshit/TODO.org::`Get system` if Value::Null sync and load json][`Get system` if Value::Null sy]] | -| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add set][{ADD} Add set]] | | | [[/home/alicia/git/pluralshit/TODO.org::Get current front][Get current front]] | -| | [[/home/alicia/git/pluralshit/TODO.org::Add `setgroup` command][Add `setgroup` command]] | | | | -| | [[/home/alicia/git/pluralshit/TODO.org::Get fronters handles the front file][Get fronters handles the front]] | | | | -| | [[/home/alicia/git/pluralshit/TODO.org::Rofi stuff][Rofi stuff]] | | | | -| | | | | | -| | | | | | +| Backlog | LowPriority | HighPriority | InProgress | Finished | +|--------------------------------+--------------------------------+--------------------------------+------------+--------------------------------| +| [[/home/alicia/git/pluralshit/TODO.org::separate memberlist into its own code block in the readme][separate memberlist into its o]] | [[/home/alicia/git/pluralshit/TODO.org::Add `add` command \[0/2\]][Add `add` command {0/2}]] | [[/home/alicia/git/pluralshit/TODO.org::Add `set` command \[2/3\]][Add `set` command {2/3}]] | | [[/home/alicia/git/pluralshit/TODO.org::Add `sync` command][Add `sync` command]] | +| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add empty][{ADD} Add empty]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add empty][{SET} Add empty]] | | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add set][{SET} Add set]] | +| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add set][{ADD} Add set]] | [[/home/alicia/git/pluralshit/TODO.org::Add `get` command][Add `get` command]] | | [[/home/alicia/git/pluralshit/TODO.org::Adapt to set multiple arguments][Adapt to set multiple argument]] | +| | [[/home/alicia/git/pluralshit/TODO.org::Add `setgroup` command][Add `setgroup` command]] | [[/home/alicia/git/pluralshit/TODO.org::Compare if sent members are currently fronting and keep them otherwise add to fronting array][Compare if sent members are cu]] | | [[/home/alicia/git/pluralshit/TODO.org::Add `memberlist` command][Add `memberlist` command]] | +| | [[/home/alicia/git/pluralshit/TODO.org::Get fronters handles the front file][Get fronters handles the front]] | [[/home/alicia/git/pluralshit/TODO.org::Check for mismatch in fronting between pluralkit and simplyplural][Check for mismatch in fronting]] | | [[/home/alicia/git/pluralshit/TODO.org::Add new function to get the config create empty in path if not exists][Add new function to get the co]] | +| | | [[/home/alicia/git/pluralshit/TODO.org::Rofi stuff][Rofi stuff]] | | [[/home/alicia/git/pluralshit/TODO.org::`Get system` if Value::Null sync and load json][`Get system` if Value::Null sy]] | +| | | | | [[/home/alicia/git/pluralshit/TODO.org::Get current front][Get current front]] | +| | | | | [[/home/alicia/git/pluralshit/TODO.org::http PATCH request][http PATCH request]] | +| | | | | [[/home/alicia/git/pluralshit/TODO.org::http POST request][http POST request]] | +| | | | | [[/home/alicia/git/pluralshit/TODO.org::Set members know the alias][Set members know the alias]] | +| | | | | | +| | | | | | +| | | | | | +| | | | | | +| | | | | | +| | | | | | #+TBLFM: @1='(kanban-headers $#)::@2$1..@>$>='(kanban-zero @# $# nil (list (buffer-file-name))) diff --git a/src/main.rs b/src/main.rs index fa07b56..b92585c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,8 @@ struct Member { // [[file:../README.org::*Main][Main:1]] fn main() { if std::env::args().len() > 1 { - let command = std::env::args().nth(1).expect("No command given"); + let augs: Vec = std::env::args().collect(); + let command = &augs[1]; let config_path: String; match dirs::config_dir() { Some(x) => { @@ -54,7 +55,7 @@ fn main() { }, "set" => { if std::env::args().len() > 2 { - match set_member(config_path, std::env::args().nth(2).expect("No member given")) { + match set_member(config_path, &augs[2..]) { Ok(_) => (), Err(e) => println!("{e}"), } @@ -139,7 +140,7 @@ TODO let system = get_system(&config_path); TODO } */ -fn set_member(config_path: String, member: String) -> Result<(), &'static str> { +fn set_member(config_path: String, tf_members: &[String]) -> Result<(), &'static str> { let config = get_config(&config_path); if config == Value::Null { return Err("Config not found. Stopping"); @@ -147,30 +148,34 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> { 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; + let mut to_front: Vec = Vec::new(); + for member in tf_members { + for mem in &system.members { + if mem.name.to_lowercase() == member.to_lowercase() || mem.alias.to_lowercase() == member.to_lowercase() { + println!("Member {member} found"); + to_front.push(mem.clone()); + + break; + } } } + if to_front.len() != 0 { + let fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system); - if flag { - println!("Member {member} found"); - get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system); - - - Ok(()) - } else { - Err("Member {member} not found") + pk_set_fronters(&config["pk_key"].as_str().unwrap(), &system, to_front, &fronters); } - + Ok(()) } fn memberlist(config_path: String) { let sys = get_system(&config_path); for mem in sys.members { - println!("{}", mem.name); + if mem.name != mem.alias { + println!("{} / {}", mem.name, mem.alias); + } else { + println!("{}", mem.name); + } } } @@ -195,24 +200,40 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec { // Get system:1 ends here // [[file:../README.org::*Get fronters][Get fronters:1]] -fn pk_get_fronters(key: &str, sysid: &str) -> Vec { - let url = format!("{}/systems/{}/fronters", PK_URL, sysid); +fn pk_get_fronters(key: &str, sys: &System) -> Vec { + let url = format!("{}/systems/{}/fronters", PK_URL, sys.pk_userid); let res = http_get_request(url,key); let data: Value = serde_json::from_str(&res.unwrap()).unwrap(); - println!("{:?}", data); let memberdata = &data["members"].as_array(); - let mut members: Vec = Vec::new(); + let mut members: Vec = Vec::new(); for member in memberdata { for m in member.into_iter() { - let mname = m["name"].as_str().unwrap(); - members.push(String::from(mname)); + for dbmem in &sys.members { + if m["name"].as_str().unwrap() == dbmem.name { + members.push(dbmem.clone()); + } + } } } return members; } + +fn pk_set_fronters(key: &str, sys: &System, to_front: Vec, _fronters: &HashMap>) { + let url = format!("{}/systems/{}/switches", PK_URL, sys.pk_userid); + + // if not fronting + + let mut frontcodes = Vec::new(); + for tf in to_front { + frontcodes.push(tf.pk_id); + } + let mut body: HashMap<&str, Vec> = HashMap::new(); + body.insert("members", frontcodes); + let _ = http_post_request(url, key, &body); +} // Get fronters:1 ends here // [[file:../README.org::*Get user ID][Get user ID:1]] @@ -257,22 +278,20 @@ fn get_sp_id(mem: &Member, ids: &HashMap) -> String { // Get ID from member:1 ends here // [[file:../README.org::*Get fronters][Get fronters:1]] -fn sp_get_fronters(key: &str, sys: &System) -> Vec { +fn sp_get_fronters(key: &str, sys: &System) -> Vec { let url = format!("{}/fronters", SP_URL); let res = http_get_request(url,key); let datas: Vec = serde_json::from_str(&res.unwrap()).unwrap(); - let mut members: Vec = Vec::new(); + let mut members: Vec = Vec::new(); for data in datas { let sp_id = &data["content"]["member"].as_str().unwrap(); - let mut push_name = String::new(); for member in &sys.members { if &member.sp_id == sp_id { - push_name = String::from(&member.name); + members.push(member.clone()); } } - members.push(push_name); } return members; @@ -342,11 +361,10 @@ fn get_system(config_path: &str) -> System { } -fn get_fronters(pk_key: &str, pk_sysid: &str, sp_key: &str, sys: &System) -> HashMap> { - let mut fronters: HashMap> = HashMap::new(); - fronters.insert(String::from("pk"), pk_get_fronters(pk_key, pk_sysid)); +fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> HashMap> { + let mut fronters: HashMap> = HashMap::new(); + fronters.insert(String::from("pk"), pk_get_fronters(pk_key, sys)); fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys)); - println!("{:?}", fronters); return fronters; } @@ -366,4 +384,35 @@ async fn http_get_request(url: String, key: &str) -> Result>) -> Result<(), Box> { + let client = reqwest::Client::new(); + let _ = client + .post(url) + .json(body) + .header(USER_AGENT, "Pluralsync") + .header(AUTHORIZATION, key) + .send() + .await?; + Ok(()) +} + +/* +#[tokio::main] +async fn http_patch_request(url: String, key: &str) -> Result> { + let client = reqwest::Client::new(); + let res = client + .patch(url) + .body("wiwiwiw") + .header(USER_AGENT, "Pluralsync") + .header(AUTHORIZATION, key) + .send() + .await? + .text() + .await?; + Ok(res) +} +*/ // Http Request handler:1 ends here