Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Le Baron de Charlus committed Apr 24, 2023
1 parent 01d8333 commit 457a09a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
14 changes: 11 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ pub struct Cli {

#[derive(Subcommand, Debug)]
pub enum Commands {
//Task(Task)
/// Actions on tasks, default list your tasks
Tasks(Tasks),
/// Actions on folders
Folders(Folders),
}

#[derive(Args, Debug)]
#[derive(Debug, Parser)]
pub struct Tasks {
pub tasks_arg: Option<String>,
/// Search tasks, matching words in title
#[arg(short, long)]
pub search: Option<String>,
/// Filter searched tasks by status
#[arg(long)]
pub status: Option<String>,
#[arg(short, long)]
pub me: bool,
}

#[derive(Args, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Folders {
child_ids: Option<Vec<String>>,
}

pub fn get_folders<'a>(url: &'a str, path: &'a str, token: &'a str) -> Result<()> {
pub fn _get_folders<'a>(url: &'a str, path: &'a str, token: &'a str) -> Result<()> {
let client = reqwest::blocking::Client::new();
let url: String = format!("{}{}", &url, &path);
let res = client
Expand Down
40 changes: 20 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ fn main() -> Result<()> {
let user: String = env::var("WRIKE_USER")?;
let url: String = env::var("URL")?;
let token: String = env::var("TOKEN")?;

let cli = Cli::parse();

match &cli.command {
Some(Commands::Tasks(name)) => match name.tasks_arg {
Some(ref _tasks_arg) => {
println!("No extra argument expected for <tasks>, please see --help");
}
None => {
let path: String = format!(r##"/tasks?responsibles=[{}]"##, user);
tasks::get_tasks(&url, &path, &token)?;
}
},
Some(Commands::Folders(name)) => match name.folders_arg {
Some(ref _folders_arg) => {
println!("No extra argument expected for <tasks>, please see --help");
}
None => {
let path: &str = "/folders";
folders::get_folders(&url, &path, &token)?;
}
},
Some(Commands::Tasks(args)) => {
let null = String::from("");
let status = match &args.status {
Some(status) => status,
None => "[Active, Completed, Deferred, Cancelled]",
};
let search = match &args.search {
Some(search) => search,
None => "",
};

let user = if args.me { &user } else { &null };
let path = format!(
r##"/tasks?responsibles=[{}]&title={}&status={}"##,
user, search, status
);
tasks::get_tasks(&url, &path, &token)?;
}

Some(Commands::Folders(_folders_arg)) => {}
None => {}
}

Ok(())
}
7 changes: 4 additions & 3 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@ struct Tasks {
id: String,
status: String,
importance: String,
permalink: String,
}

pub fn get_tasks<'a>(url: &'a str, path: &'a str, token: &'a str) -> Result<()> {
let client = reqwest::blocking::Client::new();
let url: String = format!("{}{}", &url, &path);
let res = client
.get(url)
.get(&url)
.header(AUTHORIZATION, token)
.header(CONTENT_TYPE, "application/json")
.header(ACCEPT, "application/json")
.send()
.context("Failed to make get tasks")?;

let tasks: KindTask = res.json::<KindTask>().context("Could not decode json")?;
let mut table = Table::new();
table.add_row(row!["id", "name", "priority", "status"]);
table.add_row(row!["id", "name", "priority", "url", "status"]);
for i in tasks.data.iter() {
table.add_row(Row::new(vec![
Cell::new(&i.id),
Cell::new(&i.title),
Cell::new(&i.importance),
Cell::new(&i.permalink),
Cell::new(&i.status),
]));
}
Expand Down

0 comments on commit 457a09a

Please sign in to comment.