Compare commits

...

5 Commits

3 changed files with 54 additions and 50 deletions

View File

@ -95,9 +95,7 @@ fn main() {
}
}
// gst stamp --start --task NUM --description "desc" --dstart "20120101" --dend "20120101"
// gst stamp --stop
// gst stamp --update --description "desc" --dstart "20120101" --dend "20120101"
// gst stamp [--start | --stop | --update]
("stamp", Some(_matches)) => {
let start = _matches.occurrences_of("start");
let stop = _matches.occurrences_of("stop");
@ -108,21 +106,14 @@ fn main() {
let description: &str = _matches.value_of("description").unwrap_or("").trim();
let dstart: &str = _matches.value_of("dstart").unwrap_or("").trim();
let dend: &str = _matches.value_of("dend").unwrap_or("").trim();
// gst stamp --start --task NUM --description "desc" --dstart "20120101" --dend "20120101"
if start == 1 {
console::info("Add a new stamp");
match task {
0 => panic!("Need some task number to add the stamp"),
_ => {
let stamp = Stamp {
id: 0,
user_id: 0,
project_id: 0,
start: Some(dstart.to_string()),
end: Some(dend.to_string()),
description: Some(description.to_string()),
task_id: Some(task),
};
let added = stamp.add(&config_file);
let stamp = Stamp::new(&config_file, dstart, dend, description, task, "");
let added = stamp.api_post(&config_file, "stamp/add");
match added.status() {
reqwest::StatusCode::OK => println!("OK"),
err => println!("KO: {:?}, something happened", err),
@ -130,31 +121,25 @@ fn main() {
}
}
}
// gst stamp --stop
else if stop == 1 {
console::info("Stop last stamp");
let stamp = Stamp {
id: 0,
user_id: 0,
project_id: 0,
start: Some("".to_string()),
end: Some("".to_string()),
description: Some("".to_string()),
task_id: Some(0),
};
let stopped = stamp.stop(&config_file);
let stamp = Stamp::new(&config_file, "", "", "", 0, "");
let stopped = stamp.api_post(&config_file, "stamp/stop");
match stopped.status() {
reqwest::StatusCode::OK => println!("OK"),
err => println!("KO: {:?}, something happened", err),
}
}
// gst stamp --update --description "desc" --dstart "20120101" --dend "20120101"
else if update == 1 {
console::info("Update last stamp");
// let stamp = {
// // description
// // dstart
// // dend
// };
// stamp.update();
let stamp = Stamp::new(&config_file, dstart, dend, description, task, "");
let updated = stamp.api_post(&config_file, "stamp/update");
match updated.status() {
reqwest::StatusCode::OK => println!("OK"),
err => println!("KO: {:?}, something happened", err),
}
}
}

View File

@ -9,6 +9,7 @@ use toml;
pub struct Config {
pub url: String,
pub key: String,
pub user_id: u32,
}
impl Config {
@ -16,6 +17,7 @@ impl Config {
Config {
url: "https://uri".to_string(),
key: "s3cr3tk3y".to_string(),
user_id: 0,
}
}
pub fn parse(&mut self, config_file: &PathBuf) -> Config {

View File

@ -12,34 +12,40 @@ pub struct Stamp {
pub end: Option<String>,
pub description: Option<String>,
pub task_id: Option<u32>,
pub duration: Option<String>,
}
impl Stamp {
#[tokio::main]
pub async fn add(&self, config_file: &PathBuf) -> reqwest::Response {
pub fn new(config_file: &PathBuf,
dstart: &str,
dend: &str,
description: &str,
task: u32,
duration: &str,
) -> Stamp {
// Config
let mut default_config = config::Config::default();
let config = default_config.parse(&config_file);
// Call api
let endpoint = format!("{}{}", &config.url, "stamp/add");
let client = reqwest::Client::new();
let res = client.post(endpoint)
.header("Authorization", &config.key)
.json(&self)
.send()
.await;
res.unwrap()
Stamp {
id: 0,
user_id: config.user_id,
project_id: 0,
start: Some(dstart.to_string()),
end: Some(dend.to_string()),
description: Some(description.to_string()),
task_id: Some(task),
duration: Some(duration.to_string()),
}
}
#[tokio::main]
pub async fn stop(&self, config_file: &PathBuf) -> reqwest::Response {
pub async fn api_post(&self, config_file: &PathBuf, endpoint: &str) -> reqwest::Response {
// Config
let mut default_config = config::Config::default();
let config = default_config.parse(&config_file);
// Call api
let endpoint = format!("{}{}", &config.url, "stamp/stop");
let endpoint = format!("{}{}", &config.url, String::from(endpoint));
let client = reqwest::Client::new();
let res = client.post(endpoint)
.header("Authorization", &config.key)
@ -77,19 +83,30 @@ impl Stamps {
let stamps = parsed.json::<Stamps>().unwrap();
// eprintln!("{:#?}", stamps);
for stamp in stamps.data {
let mut open: String = String::from("");
match stamp.end {
None => open = "⏸️ ".to_string(),
_ => {
let op: String = format_args!("{duration}",
duration=stamp.duration.unwrap().to_string()
).to_string();
open = op;
}
}
let output: String = format_args!("⏳ ({id}) {open} - {description}",
open=open,
id=stamp.id,
description=stamp.description.unwrap()
).to_string();
match project {
0 => {
println!("⏳ ({id}) {description}",
id=stamp.id,
description=stamp.description.unwrap());
println!("{}", output);
if last != 0 { break; }
},
_ => {
if project == stamp.project_id {
println!("⏳ ({id}) {description}",
id=stamp.id,
description=stamp.description.unwrap());
println!("{}", output);
if last != 0 { break; }
}
}