fixed logic for system get
This commit is contained in:
parent
bd977e5879
commit
e338f92361
206
README.org
206
README.org
|
@ -1,32 +1,6 @@
|
||||||
#+title: Pluralsync (Codename pluralshit)
|
#+title: Pluralsync (Codename pluralshit)
|
||||||
#+author: Jvnko
|
#+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
|
* About
|
||||||
* Configuration
|
* Configuration
|
||||||
#+begin_src json :tangle config.example.json
|
#+begin_src json :tangle config.example.json
|
||||||
|
@ -58,11 +32,16 @@ tokio = { version = "1", features = ["full"] }
|
||||||
** Main.rs
|
** Main.rs
|
||||||
*** Imports
|
*** Imports
|
||||||
#+begin_src rust :tangle src/main.rs :comments link
|
#+begin_src rust :tangle src/main.rs :comments link
|
||||||
|
use std::fs;
|
||||||
|
use std::fs::create_dir;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::{fs, collections::HashMap};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use reqwest::header::{USER_AGENT, AUTHORIZATION};
|
use reqwest::header::{USER_AGENT, AUTHORIZATION};
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use dirs;
|
use dirs;
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
@ -76,6 +55,7 @@ const SP_URL: &str = "https://api.apparyllis.com/v1";
|
||||||
#+begin_src rust :tangle src/main.rs :comments link
|
#+begin_src rust :tangle src/main.rs :comments link
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
#[derive(Deserialize)]
|
||||||
struct System {
|
struct System {
|
||||||
pk_userid: String,
|
pk_userid: String,
|
||||||
sp_userid: String,
|
sp_userid: String,
|
||||||
|
@ -84,6 +64,7 @@ struct System {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
#[derive(Deserialize)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Member {
|
struct Member {
|
||||||
pk_id: String,
|
pk_id: String,
|
||||||
|
@ -91,7 +72,6 @@ struct Member {
|
||||||
name: String,
|
name: String,
|
||||||
alias: String
|
alias: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Main
|
*** Main
|
||||||
|
@ -104,12 +84,17 @@ fn main() {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
config_path = format!("{}/pluralshit", x.display());
|
config_path = format!("{}/pluralshit", x.display());
|
||||||
match command.as_str() {
|
match command.as_str() {
|
||||||
"sync" => sync(config_path),
|
"sync" => {
|
||||||
|
let _ = sync(config_path);
|
||||||
|
},
|
||||||
"set" => {
|
"set" => {
|
||||||
if std::env::args().len() > 2 {
|
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 {
|
} else {
|
||||||
set_empty(config_path);
|
//set_empty(config_path);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
&_ => println!("Invalid command"),
|
&_ => println!("Invalid command"),
|
||||||
|
@ -126,25 +111,48 @@ fn main() {
|
||||||
*** Functions
|
*** Functions
|
||||||
**** Main commands
|
**** Main commands
|
||||||
#+begin_src rust :tangle src/main.rs :comments link
|
#+begin_src rust :tangle src/main.rs :comments link
|
||||||
fn set_member(config_path: String, member: String) {
|
fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
|
||||||
let config = load_json(format!("{}/config.json", config_path));
|
let config = get_config(&config_path);
|
||||||
let system = get_system(config_path);
|
if config == Value::Null {
|
||||||
|
return Err("Config not found. Stopping");
|
||||||
//for mem in system["members"].as_str().unwrap() {
|
|
||||||
// TODO
|
|
||||||
// TODO Hacer que devuelva el json en forma de Vec<Member>
|
|
||||||
// TODO
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
fn set_empty(config_path: String) {
|
fn set_empty(config_path: String) {
|
||||||
let config = load_json(format!("{}/config.json", config_path));
|
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) -> Result<(), String>{
|
||||||
|
// Get config
|
||||||
|
let config = get_config(&config_path);
|
||||||
|
if config == Value::Null {
|
||||||
|
println!("Stopping.");
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sync(config_path: String) {
|
|
||||||
// Get config
|
|
||||||
let config = load_json(format!("{}/config.json", config_path));
|
|
||||||
let pk_key = &config["pk_key"].as_str().unwrap();
|
let pk_key = &config["pk_key"].as_str().unwrap();
|
||||||
let sp_key = &config["sp_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 json = serde_json::to_string(&sys);
|
||||||
let _ = fs::write(format!("{}/system.json", config_path), &json.unwrap());
|
let _ = fs::write(format!("{}/system.json", config_path), &json.unwrap());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
@ -197,14 +206,14 @@ fn sync(config_path: String) {
|
||||||
fn pk_get_system(key: &str) -> Value {
|
fn pk_get_system(key: &str) -> Value {
|
||||||
let url = format!("{}/systems/@me", PK_URL);
|
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();
|
return serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
|
fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
|
||||||
let url = format!("{}/systems/{}/members", PK_URL, sysid);
|
let url = format!("{}/systems/{}/members", PK_URL, sysid);
|
||||||
|
|
||||||
let res = http_request(url,key);
|
let res = http_get_request(url,key);
|
||||||
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
|
|
||||||
return datas;
|
return datas;
|
||||||
|
@ -216,7 +225,7 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
|
||||||
fn sp_get_userid(key: &str) -> String {
|
fn sp_get_userid(key: &str) -> String {
|
||||||
let url = format!("{}/me", SP_URL);
|
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();
|
let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
return json_res["id"].as_str().unwrap().to_string();
|
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<String, String> {
|
fn sp_get_memberids(key: &str, system_id: &str) -> HashMap<String, String> {
|
||||||
let url = format!("{}/members/{}", SP_URL, system_id);
|
let url = format!("{}/members/{}", SP_URL, system_id);
|
||||||
|
|
||||||
let res = http_request(url,key);
|
let res = http_get_request(url,key);
|
||||||
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
|
|
||||||
let mut sp_memberdata: HashMap<String, String> = HashMap::new();
|
let mut sp_memberdata: HashMap<String, String> = HashMap::new();
|
||||||
|
@ -253,7 +262,7 @@ fn get_sp_id(mem: &Member, ids: &HashMap<String, String>) -> String {
|
||||||
#+begin_src rust :tangle src/main.rs :comments link
|
#+begin_src rust :tangle src/main.rs :comments link
|
||||||
fn load_json(path: String) -> Value {
|
fn load_json(path: String) -> Value {
|
||||||
if Path::new(&path).exists() {
|
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();
|
return serde_json::from_str(&config_data).unwrap();
|
||||||
} else {
|
} else {
|
||||||
println!("Config file in {path} not found");
|
println!("Config file in {path} not found");
|
||||||
|
@ -261,38 +270,59 @@ fn load_json(path: String) -> Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
fn get_config(config_path: &str) -> Value {
|
||||||
// TODO fn get_config() -> Value {
|
let path = format!("{}/config.json", config_path);
|
||||||
// TODO load_json
|
if Path::new(config_path).exists() {
|
||||||
// TODO if Value:null - > mkconfig dir - > make empty config.json
|
|
||||||
// TODO else -> return
|
|
||||||
// TODO }
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
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);
|
let path = format!("{}/system.json", config_path);
|
||||||
|
|
||||||
// TODO
|
let mut result = load_json(String::from(&path));
|
||||||
// TODO load_json
|
|
||||||
// TODO if Value:null - > sync - > load_json
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
if Path::new(&path).exists() {
|
if result == Value::Null {
|
||||||
let config_data = fs::read_to_string(path).expect("File not found");
|
println!("Syncing system config");
|
||||||
return serde_json::from_str(&config_data).unwrap();
|
let _ = sync(String::from(config_path));
|
||||||
} else {
|
result = load_json(String::from(&path));
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let vec = serde_json::to_vec(&result).unwrap();
|
||||||
|
let sys = serde_json::from_slice::<System>(&vec).unwrap();
|
||||||
|
|
||||||
|
return sys;
|
||||||
|
|
||||||
}
|
}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
**** Http Request handler
|
**** Http Request handler
|
||||||
#+begin_src rust :tangle src/main.rs :comments link
|
#+begin_src rust :tangle src/main.rs :comments link
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn http_request(url: String, key: &str) -> Result<String, Box<dyn std::error::Error>> {
|
async fn http_get_request(url: String, key: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let res = client
|
let res = client
|
||||||
.get(url)
|
.get(url)
|
||||||
|
@ -305,39 +335,3 @@ async fn http_request(url: String, key: &str) -> Result<String, Box<dyn std::err
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
* Organization
|
|
||||||
** Tasks
|
|
||||||
:PROPERTIES:
|
|
||||||
:COOKIE_DATA: recursive
|
|
||||||
:END:
|
|
||||||
*** Milestone 1.0 [1/11][9%]
|
|
||||||
**** Main functions [1/9][11%]
|
|
||||||
***** Finished Add `sync` command
|
|
||||||
***** InProgress Add `set` command [0/2]
|
|
||||||
****** HighPriority [SET] Add empty
|
|
||||||
****** InProgress [SET] Add set
|
|
||||||
***** HighPriority Add `get` command
|
|
||||||
***** LowPriority Add `Add` command [0/2]
|
|
||||||
****** LowPriority [ADD] Add empty
|
|
||||||
****** LowPriority [ADD] Add set
|
|
||||||
***** LowPriority Add `setgroup` command
|
|
||||||
**** Commands [0/0][100%]
|
|
||||||
**** Utils [0/2][0%]
|
|
||||||
***** Json loading [0/2]
|
|
||||||
****** HighPriority Add new function to get the config create empty in path if not exists
|
|
||||||
****** HighPriority `Get system` if Value::Null sync and load json
|
|
||||||
** Kanban
|
|
||||||
| Backlog | LowPriority | HighPriority | InProgress | Finished |
|
|
||||||
|---------+-------------------------+--------------------------------+-------------------------+--------------------|
|
|
||||||
| | [[/home/alicia/git/pluralshit/README.org::Add `Add` command \[0/2\]][Add `Add` command {0/2}]] | [[/home/alicia/git/pluralshit/README.org::\[SET\] Add empty][{SET} Add empty]] | [[/home/alicia/git/pluralshit/README.org::Add `set` command \[0/2\]][Add `set` command {0/2}]] | [[/home/alicia/git/pluralshit/README.org::Add `sync` command][Add `sync` command]] |
|
|
||||||
| | [[/home/alicia/git/pluralshit/README.org::\[ADD\] Add empty][{ADD} Add empty]] | [[/home/alicia/git/pluralshit/README.org::Add `get` command][Add `get` command]] | [[/home/alicia/git/pluralshit/README.org::\[SET\] Add set][{SET} Add set]] | |
|
|
||||||
| | [[/home/alicia/git/pluralshit/README.org::\[ADD\] Add set][{ADD} Add set]] | [[/home/alicia/git/pluralshit/README.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/README.org::Add `setgroup` command][Add `setgroup` command]] | [[/home/alicia/git/pluralshit/README.org::`Get system` if Value::Null sync and load json][`Get system` if Value::Null sy]] | | |
|
|
||||||
| | | | | |
|
|
||||||
| | | | | |
|
|
||||||
| | | | | |
|
|
||||||
| | | | | |
|
|
||||||
| | | | | |
|
|
||||||
| | | | | |
|
|
||||||
#+TBLFM: @1='(kanban-headers $#)::@2$1..@>$>='(kanban-zero @# $# nil (list (buffer-file-name)))
|
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
#+title: Todo
|
||||||
|
#+author: Jvnko
|
||||||
|
|
||||||
|
#+seq_todo: Backlog LowPriority HighPriority InProgress | Finished
|
||||||
|
|
||||||
|
* Organization
|
||||||
|
** Tasks
|
||||||
|
:PROPERTIES:
|
||||||
|
:COOKIE_DATA: recursive
|
||||||
|
:END:
|
||||||
|
*** Milestone 1.0 [3/11][27%]
|
||||||
|
**** Main functions [1/9][11%]
|
||||||
|
***** Finished Add `sync` command
|
||||||
|
***** InProgress Add `set` command [0/2]
|
||||||
|
****** HighPriority [SET] Add empty
|
||||||
|
****** InProgress [SET] Add set
|
||||||
|
***** HighPriority Add `get` command
|
||||||
|
***** LowPriority Add `Add` command [0/2]
|
||||||
|
****** LowPriority [ADD] Add empty
|
||||||
|
****** LowPriority [ADD] Add set
|
||||||
|
***** LowPriority Add `setgroup` command
|
||||||
|
**** Utils [2/2][100%]
|
||||||
|
***** 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
|
||||||
|
|
||||||
|
** Kanban
|
||||||
|
| Backlog | LowPriority | HighPriority | InProgress | Finished |
|
||||||
|
|---------+-------------------------+-------------------+-------------------------+--------------------------------|
|
||||||
|
| | [[/home/alicia/git/pluralshit/TODO.org::Add `Add` command \[0/2\]][Add `Add` command {0/2}]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add empty][{SET} Add empty]] | [[/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\] Add empty][{ADD} Add empty]] | [[/home/alicia/git/pluralshit/TODO.org::Add `get` command][Add `get` command]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add set][{SET} Add set]] | [[/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 set][{ADD} Add set]] | | | [[/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 `setgroup` command][Add `setgroup` command]] | | | |
|
||||||
|
| | | | | |
|
||||||
|
| | | | | |
|
||||||
|
| | | | | |
|
||||||
|
| | | | | |
|
||||||
|
| | | | | |
|
||||||
|
| | | | | |
|
||||||
|
#+TBLFM: @1='(kanban-headers $#)::@2$1..@>$>='(kanban-zero @# $# nil (list (buffer-file-name)))
|
143
src/main.rs
143
src/main.rs
|
@ -1,9 +1,14 @@
|
||||||
// [[file:../README.org::*Imports][Imports:1]]
|
// [[file:../README.org::*Imports][Imports:1]]
|
||||||
|
use std::fs;
|
||||||
|
use std::fs::create_dir;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::{fs, collections::HashMap};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use reqwest::header::{USER_AGENT, AUTHORIZATION};
|
use reqwest::header::{USER_AGENT, AUTHORIZATION};
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use dirs;
|
use dirs;
|
||||||
// Imports:1 ends here
|
// Imports:1 ends here
|
||||||
|
|
||||||
|
@ -15,6 +20,7 @@ const SP_URL: &str = "https://api.apparyllis.com/v1";
|
||||||
// [[file:../README.org::*Structs][Structs:1]]
|
// [[file:../README.org::*Structs][Structs:1]]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
#[derive(Deserialize)]
|
||||||
struct System {
|
struct System {
|
||||||
pk_userid: String,
|
pk_userid: String,
|
||||||
sp_userid: String,
|
sp_userid: String,
|
||||||
|
@ -23,6 +29,7 @@ struct System {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
#[derive(Deserialize)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Member {
|
struct Member {
|
||||||
pk_id: String,
|
pk_id: String,
|
||||||
|
@ -41,12 +48,17 @@ fn main() {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
config_path = format!("{}/pluralshit", x.display());
|
config_path = format!("{}/pluralshit", x.display());
|
||||||
match command.as_str() {
|
match command.as_str() {
|
||||||
"sync" => sync(config_path),
|
"sync" => {
|
||||||
|
let _ = sync(config_path);
|
||||||
|
},
|
||||||
"set" => {
|
"set" => {
|
||||||
if std::env::args().len() > 2 {
|
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 {
|
} else {
|
||||||
set_empty(config_path);
|
//set_empty(config_path);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
&_ => println!("Invalid command"),
|
&_ => println!("Invalid command"),
|
||||||
|
@ -61,25 +73,48 @@ fn main() {
|
||||||
// Main:1 ends here
|
// Main:1 ends here
|
||||||
|
|
||||||
// [[file:../README.org::*Main commands][Main commands:1]]
|
// [[file:../README.org::*Main commands][Main commands:1]]
|
||||||
fn set_member(config_path: String, member: String) {
|
fn set_member(config_path: String, member: String) -> Result<(), &'static str> {
|
||||||
let config = load_json(format!("{}/config.json", config_path));
|
let config = get_config(&config_path);
|
||||||
let system = get_system(config_path);
|
if config == Value::Null {
|
||||||
|
return Err("Config not found. Stopping");
|
||||||
//for mem in system["members"].as_str().unwrap() {
|
|
||||||
// TODO
|
|
||||||
// TODO Hacer que devuelva el json en forma de Vec<Member>
|
|
||||||
// TODO
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
fn set_empty(config_path: String) {
|
fn set_empty(config_path: String) {
|
||||||
let config = load_json(format!("{}/config.json", config_path));
|
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) -> Result<(), String>{
|
||||||
|
// Get config
|
||||||
|
let config = get_config(&config_path);
|
||||||
|
if config == Value::Null {
|
||||||
|
println!("Stopping.");
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sync(config_path: String) {
|
|
||||||
// Get config
|
|
||||||
let config = load_json(format!("{}/config.json", config_path));
|
|
||||||
let pk_key = &config["pk_key"].as_str().unwrap();
|
let pk_key = &config["pk_key"].as_str().unwrap();
|
||||||
let sp_key = &config["sp_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 json = serde_json::to_string(&sys);
|
||||||
let _ = fs::write(format!("{}/system.json", config_path), &json.unwrap());
|
let _ = fs::write(format!("{}/system.json", config_path), &json.unwrap());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
// Main commands:1 ends here
|
// Main commands:1 ends here
|
||||||
|
|
||||||
|
@ -131,14 +167,14 @@ fn sync(config_path: String) {
|
||||||
fn pk_get_system(key: &str) -> Value {
|
fn pk_get_system(key: &str) -> Value {
|
||||||
let url = format!("{}/systems/@me", PK_URL);
|
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();
|
return serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
|
fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
|
||||||
let url = format!("{}/systems/{}/members", PK_URL, sysid);
|
let url = format!("{}/systems/{}/members", PK_URL, sysid);
|
||||||
|
|
||||||
let res = http_request(url,key);
|
let res = http_get_request(url,key);
|
||||||
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
|
|
||||||
return datas;
|
return datas;
|
||||||
|
@ -149,7 +185,7 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
|
||||||
fn sp_get_userid(key: &str) -> String {
|
fn sp_get_userid(key: &str) -> String {
|
||||||
let url = format!("{}/me", SP_URL);
|
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();
|
let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
return json_res["id"].as_str().unwrap().to_string();
|
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<String, String> {
|
fn sp_get_memberids(key: &str, system_id: &str) -> HashMap<String, String> {
|
||||||
let url = format!("{}/members/{}", SP_URL, system_id);
|
let url = format!("{}/members/{}", SP_URL, system_id);
|
||||||
|
|
||||||
let res = http_request(url,key);
|
let res = http_get_request(url,key);
|
||||||
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
||||||
|
|
||||||
let mut sp_memberdata: HashMap<String, String> = HashMap::new();
|
let mut sp_memberdata: HashMap<String, String> = HashMap::new();
|
||||||
|
@ -185,7 +221,7 @@ fn get_sp_id(mem: &Member, ids: &HashMap<String, String>) -> String {
|
||||||
// [[file:../README.org::*Utilities][Utilities:1]]
|
// [[file:../README.org::*Utilities][Utilities:1]]
|
||||||
fn load_json(path: String) -> Value {
|
fn load_json(path: String) -> Value {
|
||||||
if Path::new(&path).exists() {
|
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();
|
return serde_json::from_str(&config_data).unwrap();
|
||||||
} else {
|
} else {
|
||||||
println!("Config file in {path} not found");
|
println!("Config file in {path} not found");
|
||||||
|
@ -193,37 +229,58 @@ fn load_json(path: String) -> Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
fn get_config(config_path: &str) -> Value {
|
||||||
// TODO fn get_config() -> Value {
|
let path = format!("{}/config.json", config_path);
|
||||||
// TODO load_json
|
if Path::new(config_path).exists() {
|
||||||
// TODO if Value:null - > mkconfig dir - > make empty config.json
|
|
||||||
// TODO else -> return
|
|
||||||
// TODO }
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
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);
|
let path = format!("{}/system.json", config_path);
|
||||||
|
|
||||||
// TODO
|
let mut result = load_json(String::from(&path));
|
||||||
// TODO load_json
|
|
||||||
// TODO if Value:null - > sync - > load_json
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
if Path::new(&path).exists() {
|
if result == Value::Null {
|
||||||
let config_data = fs::read_to_string(path).expect("File not found");
|
println!("Syncing system config");
|
||||||
return serde_json::from_str(&config_data).unwrap();
|
let _ = sync(String::from(config_path));
|
||||||
} else {
|
result = load_json(String::from(&path));
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let vec = serde_json::to_vec(&result).unwrap();
|
||||||
|
let sys = serde_json::from_slice::<System>(&vec).unwrap();
|
||||||
|
|
||||||
|
return sys;
|
||||||
|
|
||||||
}
|
}
|
||||||
// Utilities:1 ends here
|
// Utilities:1 ends here
|
||||||
|
|
||||||
// [[file:../README.org::*Http Request handler][Http Request handler:1]]
|
// [[file:../README.org::*Http Request handler][Http Request handler:1]]
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn http_request(url: String, key: &str) -> Result<String, Box<dyn std::error::Error>> {
|
async fn http_get_request(url: String, key: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let res = client
|
let res = client
|
||||||
.get(url)
|
.get(url)
|
||||||
|
|
Loading…
Reference in New Issue