Refactor: Getting rid of cmd and moving all the stuff to models
This commit is contained in:
parent
e905688a91
commit
4e8aad79c6
134
src/cmd.rs
134
src/cmd.rs
@ -1,134 +0,0 @@
|
|||||||
use std::path::PathBuf;
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
|
|
||||||
#[path = "./config.rs"]
|
|
||||||
mod config;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct Project {
|
|
||||||
id: u32,
|
|
||||||
name: String,
|
|
||||||
slug: String,
|
|
||||||
description: Option<String>,
|
|
||||||
status: String,
|
|
||||||
user_id: u32,
|
|
||||||
paused: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct ProjectsResponse {
|
|
||||||
data: Vec<Project>,
|
|
||||||
error: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct Task {
|
|
||||||
id: u32,
|
|
||||||
name: String,
|
|
||||||
description: Option<String>,
|
|
||||||
project_id: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct TasksResponse {
|
|
||||||
data: Vec<Task>,
|
|
||||||
error: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct Stamp {
|
|
||||||
id: u32,
|
|
||||||
user_id: u32,
|
|
||||||
project_id: u32,
|
|
||||||
start: Option<String>,
|
|
||||||
end: Option<String>,
|
|
||||||
description: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct StampsResponse {
|
|
||||||
data: Vec<Stamp>,
|
|
||||||
error: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn api_call(config_file: &PathBuf, endpoint: String) -> Result<reqwest::blocking::Response, reqwest::Error> {
|
|
||||||
// Config
|
|
||||||
let mut default_config = config::Config::default();
|
|
||||||
let config = default_config.parse(&config_file);
|
|
||||||
|
|
||||||
// Call api
|
|
||||||
let endpoint = format!("{}{}", &config.url, endpoint);
|
|
||||||
let client = reqwest::blocking::Client::new();
|
|
||||||
let res = client.get(endpoint)
|
|
||||||
.header("Content-type", "application/json")
|
|
||||||
.header("Accept", "application/json")
|
|
||||||
.header("Authorization", &config.key)
|
|
||||||
.send();
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_projects(config_file: &PathBuf) {
|
|
||||||
let response = api_call(&config_file, String::from("projects"));
|
|
||||||
// eprintln!("{:#?}", response);
|
|
||||||
match response {
|
|
||||||
Ok(parsed) => {
|
|
||||||
let projects = parsed.json::<ProjectsResponse>().unwrap();
|
|
||||||
for project in projects.data {
|
|
||||||
println!("➡️ ({id}) {name}",
|
|
||||||
id=project.id,
|
|
||||||
name=project.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!("Error happened: {}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_tasks(config_file: &PathBuf, project: u32) {
|
|
||||||
let response = api_call(&config_file, String::from("tasks"));
|
|
||||||
// eprintln!("{:#?}", response);
|
|
||||||
match response {
|
|
||||||
Ok(parsed) => {
|
|
||||||
// println!("{:?}", parsed);
|
|
||||||
let tasks = parsed.json::<TasksResponse>().unwrap();
|
|
||||||
for task in tasks.data {
|
|
||||||
match project {
|
|
||||||
// 0 means no project, print them all
|
|
||||||
0 => {
|
|
||||||
println!("✳️ (pr: {projectid}) ({id}) {name}",
|
|
||||||
projectid=task.project_id,
|
|
||||||
id=task.id,
|
|
||||||
name=task.name);
|
|
||||||
},
|
|
||||||
// Otherwise but 0, print only task with that project.id
|
|
||||||
_ => {
|
|
||||||
if project == task.project_id {
|
|
||||||
println!("✳️ (pr: {projectid}) ({id}) {name}",
|
|
||||||
projectid=task.project_id,
|
|
||||||
id=task.id,
|
|
||||||
name=task.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!("Error happened: {}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_stamps(config_file: &PathBuf, last: u64) {
|
|
||||||
let response = api_call(&config_file, String::from("stamps"));
|
|
||||||
// eprintln!("{:#?}", response);
|
|
||||||
match response {
|
|
||||||
Ok(parsed) => {
|
|
||||||
let stamps = parsed.json::<StampsResponse>().unwrap();
|
|
||||||
// eprintln!("{:#?}", stamps);
|
|
||||||
for stamp in stamps.data {
|
|
||||||
println!("⏳ ({id}) {description}",
|
|
||||||
id=stamp.id,
|
|
||||||
description=stamp.description.unwrap());
|
|
||||||
if last != 0 { break; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => println!("Error happened: {}", e),
|
|
||||||
}
|
|
||||||
}
|
|
30
src/main.rs
30
src/main.rs
@ -5,6 +5,18 @@ mod cli;
|
|||||||
mod cmd;
|
mod cmd;
|
||||||
pub mod console;
|
pub mod console;
|
||||||
|
|
||||||
|
#[path = "./models/tasks.rs"]
|
||||||
|
mod tasks;
|
||||||
|
use tasks::{Task, Tasks};
|
||||||
|
|
||||||
|
#[path = "./models/projects.rs"]
|
||||||
|
mod projects;
|
||||||
|
use projects::Projects;
|
||||||
|
|
||||||
|
#[path = "./models/stamps.rs"]
|
||||||
|
mod stamps;
|
||||||
|
use stamps::Stamps;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Get cli matches
|
// Get cli matches
|
||||||
let matches = cli::build_cli().get_matches();
|
let matches = cli::build_cli().get_matches();
|
||||||
@ -23,7 +35,11 @@ fn main() {
|
|||||||
// gst projects
|
// gst projects
|
||||||
("projects", Some(_matches)) => {
|
("projects", Some(_matches)) => {
|
||||||
console::info("List of projects");
|
console::info("List of projects");
|
||||||
cmd::get_projects(&config_file);
|
let projects = Projects {
|
||||||
|
data: Vec::new(),
|
||||||
|
error: "".to_string(),
|
||||||
|
};
|
||||||
|
projects.get(&config_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// gst tasks [--project]
|
// gst tasks [--project]
|
||||||
@ -32,14 +48,22 @@ fn main() {
|
|||||||
let project = _matches.value_of("project")
|
let project = _matches.value_of("project")
|
||||||
.unwrap_or("0").trim().parse()
|
.unwrap_or("0").trim().parse()
|
||||||
.expect("Type a number!");
|
.expect("Type a number!");
|
||||||
cmd::get_tasks(&config_file, project);
|
let tasks = Tasks {
|
||||||
|
data: Vec::new(),
|
||||||
|
error: "".to_string(),
|
||||||
|
};
|
||||||
|
tasks.get(&config_file, project);
|
||||||
}
|
}
|
||||||
|
|
||||||
// gst stamps [--last]
|
// gst stamps [--last]
|
||||||
("stamps", Some(_matches)) => {
|
("stamps", Some(_matches)) => {
|
||||||
console::info("List of stamps");
|
console::info("List of stamps");
|
||||||
let last = _matches.occurrences_of("last");
|
let last = _matches.occurrences_of("last");
|
||||||
cmd::get_stamps(&config_file, last);
|
let stamps = Stamps {
|
||||||
|
data: Vec::new(),
|
||||||
|
error: "".to_string(),
|
||||||
|
};
|
||||||
|
stamps.get(&config_file, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => console::error("Whut!!!"),
|
_ => console::error("Whut!!!"),
|
||||||
|
50
src/models/projects.rs
Normal file
50
src/models/projects.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Project {
|
||||||
|
pub id: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub slug: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub status: String,
|
||||||
|
pub user_id: u32,
|
||||||
|
pub paused: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Projects {
|
||||||
|
pub data: Vec<Project>,
|
||||||
|
pub error: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Projects {
|
||||||
|
pub fn get(&self, config_file: &PathBuf) {
|
||||||
|
// Config
|
||||||
|
let mut default_config = config::Config::default();
|
||||||
|
let config = default_config.parse(&config_file);
|
||||||
|
|
||||||
|
// Call api
|
||||||
|
let endpoint = format!("{}{}", &config.url, "projects");
|
||||||
|
let client = reqwest::blocking::Client::new();
|
||||||
|
let res = client.get(endpoint)
|
||||||
|
.header("Content-type", "application/json")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.header("Authorization", &config.key)
|
||||||
|
.send();
|
||||||
|
|
||||||
|
match res {
|
||||||
|
Ok(parsed) => {
|
||||||
|
let projects = parsed.json::<Projects>().unwrap();
|
||||||
|
for project in projects.data {
|
||||||
|
println!("➡️ ({id}) {name}",
|
||||||
|
id=project.id,
|
||||||
|
name=project.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => println!("Error happened: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
src/models/stamps.rs
Normal file
51
src/models/stamps.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Stamp {
|
||||||
|
pub id: u32,
|
||||||
|
pub user_id: u32,
|
||||||
|
pub project_id: u32,
|
||||||
|
pub start: Option<String>,
|
||||||
|
pub end: Option<String>,
|
||||||
|
pub description: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Stamps {
|
||||||
|
pub data: Vec<Stamp>,
|
||||||
|
pub error: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Stamps {
|
||||||
|
pub fn get(&self, config_file: &PathBuf, last: u64) {
|
||||||
|
// Config
|
||||||
|
let mut default_config = config::Config::default();
|
||||||
|
let config = default_config.parse(&config_file);
|
||||||
|
|
||||||
|
// Call api
|
||||||
|
let endpoint = format!("{}{}", &config.url, "stamps");
|
||||||
|
let client = reqwest::blocking::Client::new();
|
||||||
|
let res = client.get(endpoint)
|
||||||
|
.header("Content-type", "application/json")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.header("Authorization", &config.key)
|
||||||
|
.send();
|
||||||
|
|
||||||
|
match res {
|
||||||
|
Ok(parsed) => {
|
||||||
|
let stamps = parsed.json::<Stamps>().unwrap();
|
||||||
|
// eprintln!("{:#?}", stamps);
|
||||||
|
for stamp in stamps.data {
|
||||||
|
println!("⏳ ({id}) {description}",
|
||||||
|
id=stamp.id,
|
||||||
|
description=stamp.description.unwrap());
|
||||||
|
if last != 0 { break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => println!("Error happened: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
85
src/models/tasks.rs
Normal file
85
src/models/tasks.rs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Task {
|
||||||
|
pub id: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub project_id: u32,
|
||||||
|
pub estimated: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Task {
|
||||||
|
#[tokio::main]
|
||||||
|
pub async fn add(&self, config_file: &PathBuf) -> reqwest::Response {
|
||||||
|
// Config
|
||||||
|
let mut default_config = config::Config::default();
|
||||||
|
let config = default_config.parse(&config_file);
|
||||||
|
|
||||||
|
// Call api
|
||||||
|
let endpoint = format!("{}{}", &config.url, "task/add");
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let res = client.post(endpoint)
|
||||||
|
.header("Authorization", &config.key)
|
||||||
|
.json(&self)
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
res.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Tasks {
|
||||||
|
pub data: Vec<Task>,
|
||||||
|
pub error: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tasks {
|
||||||
|
pub fn get(&self, config_file: &PathBuf, project: u32) {
|
||||||
|
// Config
|
||||||
|
let mut default_config = config::Config::default();
|
||||||
|
let config = default_config.parse(&config_file);
|
||||||
|
|
||||||
|
// Call api
|
||||||
|
let endpoint = format!("{}{}", &config.url, "tasks");
|
||||||
|
let client = reqwest::blocking::Client::new();
|
||||||
|
let res = client.get(endpoint)
|
||||||
|
.header("Content-type", "application/json")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.header("Authorization", &config.key)
|
||||||
|
.send();
|
||||||
|
match res {
|
||||||
|
Ok(parsed) => {
|
||||||
|
// println!("{:?}", parsed);
|
||||||
|
let tasks = parsed.json::<Tasks>().unwrap();
|
||||||
|
for task in tasks.data {
|
||||||
|
match project {
|
||||||
|
// 0 means no project, print them all
|
||||||
|
0 => {
|
||||||
|
println!("✳️ (pr: {projectid}) ({id}) {name}",
|
||||||
|
projectid=task.project_id,
|
||||||
|
id=task.id,
|
||||||
|
name=task.name);
|
||||||
|
},
|
||||||
|
// Otherwise but 0, print only task with that project.id
|
||||||
|
_ => {
|
||||||
|
if project == task.project_id {
|
||||||
|
println!("✳️ (pr: {projectid}) ({id}) {name}",
|
||||||
|
projectid=task.project_id,
|
||||||
|
id=task.id,
|
||||||
|
name=task.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => println!("Error happened: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user