Compare commits

..

No commits in common. "5ce6fb8f60de713e582e9fc0c1b89d89c5ef19e1" and "a16fdd3c5f5336eaee999a9d6e165251b5fccff9" have entirely different histories.

3 changed files with 54 additions and 58 deletions

View File

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

View File

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

View File

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