set command

This commit is contained in:
Junko 2023-11-22 13:11:49 +01:00
parent f087dcf3c2
commit b0e080d1ee
4 changed files with 185 additions and 106 deletions

View File

@ -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"] }

View File

@ -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<String> = 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,31 +188,35 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
let system: System = get_system(&config_path);
let mut flag = false;
let mut to_front: Vec<Member> = Vec::new();
for member in tf_members {
for mem in &system.members {
if mem.name.to_lowercase() == member.to_lowercase() {
flag = true;
}
}
if flag {
if mem.name.to_lowercase() == member.to_lowercase() || mem.alias.to_lowercase() == member.to_lowercase() {
println!("Member {member} found");
get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system);
to_front.push(mem.clone());
Ok(())
} else {
Err("Member {member} not found")
break;
}
}
}
if to_front.len() != 0 {
let fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system);
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 {
if mem.name != mem.alias {
println!("{} / {}", mem.name, mem.alias);
} else {
println!("{}", mem.name);
}
}
}
#+end_src
@ -238,22 +243,22 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
***** Get fronters
#+begin_src rust :tangle src/main.rs :comments link
fn pk_get_fronters(key: &str, sysid: &str) -> Vec<String> {
let url = format!("{}/systems/{}/fronters", PK_URL, sysid);
fn sp_get_fronters(key: &str, sys: &System) -> Vec<Member> {
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<Value> = serde_json::from_str(&res.unwrap()).unwrap();
let mut members: Vec<String> = 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<Member> = 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<String> {
**** Simplyplural
***** Get user ID
#+begin_src rust :tangle src/main.rs :comments link
fn sp_get_fronters(key: &str, sys: &System) -> Vec<String> {
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<Value> = serde_json::from_str(&res.unwrap()).unwrap();
let mut members: Vec<String> = 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<String, Vec<String>> {
let mut fronters: HashMap<String, Vec<String>> = 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<String, Vec<Member>> {
let mut fronters: HashMap<String, Vec<Member>> = 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<String, Box<dyn std:
.await?;
Ok(res)
}
#[tokio::main]
async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
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<String, Box<dyn std::error::Error>> {
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

View File

@ -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]] | | | |
|--------------------------------+--------------------------------+--------------------------------+------------+--------------------------------|
| [[/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)))

View File

@ -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<String> = 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,31 +148,35 @@ fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
let system: System = get_system(&config_path);
let mut flag = false;
let mut to_front: Vec<Member> = Vec::new();
for member in tf_members {
for mem in &system.members {
if mem.name.to_lowercase() == member.to_lowercase() {
flag = true;
}
}
if flag {
if mem.name.to_lowercase() == member.to_lowercase() || mem.alias.to_lowercase() == member.to_lowercase() {
println!("Member {member} found");
get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system);
to_front.push(mem.clone());
Ok(())
} else {
Err("Member {member} not found")
break;
}
}
}
if to_front.len() != 0 {
let fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system);
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 {
if mem.name != mem.alias {
println!("{} / {}", mem.name, mem.alias);
} else {
println!("{}", mem.name);
}
}
}
// Set member:1 ends here
@ -195,24 +200,40 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
// Get system:1 ends here
// [[file:../README.org::*Get fronters][Get fronters:1]]
fn pk_get_fronters(key: &str, sysid: &str) -> Vec<String> {
let url = format!("{}/systems/{}/fronters", PK_URL, sysid);
fn pk_get_fronters(key: &str, sys: &System) -> Vec<Member> {
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<String> = Vec::new();
let mut members: Vec<Member> = 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<Member>, _fronters: &HashMap<String, Vec<Member>>) {
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<String>> = 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, String>) -> 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<String> {
fn sp_get_fronters(key: &str, sys: &System) -> Vec<Member> {
let url = format!("{}/fronters", SP_URL);
let res = http_get_request(url,key);
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
let mut members: Vec<String> = Vec::new();
let mut members: Vec<Member> = 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<String, Vec<String>> {
let mut fronters: HashMap<String, Vec<String>> = 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<String, Vec<Member>> {
let mut fronters: HashMap<String, Vec<Member>> = 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<String, Box<dyn std:
.await?;
Ok(res)
}
#[tokio::main]
async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
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<String, Box<dyn std::error::Error>> {
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