diff --git a/check.sh b/check.sh index e641dd9..5e6efec 100755 --- a/check.sh +++ b/check.sh @@ -3,4 +3,4 @@ set -euo pipefail cargo fmt cargo clippy --all-targets --all-features -cargo test run +cargo test --all-targets diff --git a/src/cli.rs b/src/cli.rs index 728bc31..450567a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -76,10 +76,17 @@ pub enum Workspaces { List, } +#[derive(Parser, Debug)] +pub struct ListProjects { + /// Include archived projects + #[arg(long, default_value_t = false)] + pub include_archived: bool, +} + #[derive(Subcommand, Debug)] pub enum Projects { /// List all projects (default workspace) - List, + List(ListProjects), } #[derive(Parser, Debug)] @@ -215,10 +222,17 @@ pub struct TimeEntryDetails { pub id: u64, } +#[derive(Parser, Debug)] +pub struct ListClients { + /// Include archived + #[arg(long, default_value_t = false)] + pub include_archived: bool, +} + #[derive(Parser, Debug)] pub enum Clients { /// List all clients (default workspace) - List, + List(ListClients), /// Create client (in default workspace) Create(CreateClient), diff --git a/src/client.rs b/src/client.rs index a93ebed..d7bf5a3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -14,6 +14,7 @@ use chrono::DateTime; use chrono::Duration; use chrono::Local; use colored::Colorize; +use itertools::Itertools; use minreq::Method; use minreq::Request; use minreq::Response; @@ -179,13 +180,25 @@ impl TogglClient { pub fn get_workspace_clients( &self, debug: bool, + include_archived: bool, workspace_id: u64, ) -> anyhow::Result>> { - self.request( + let clients = self.request( debug, Method::Get, &format!("workspaces/{workspace_id}/clients"), - ) + )?; + + match clients { + Some(clients) if include_archived => Ok(Some(clients)), + Some(clients) => Ok(Some( + clients + .into_iter() + .filter(|client| !client.archived) + .collect_vec(), + )), + None => Ok(None), + } } pub fn get_time_entries( @@ -219,13 +232,25 @@ impl TogglClient { pub fn get_workspace_projects( &self, debug: bool, + include_archived: bool, workspace_id: u64, ) -> anyhow::Result> { - self.request::>( + let projects = self.request::>( debug, Method::Get, &format!("workspaces/{workspace_id}/projects"), - ) + )?; + + if include_archived { + Ok(projects) + } else { + Ok( + projects + .into_iter() + .filter(|project| project.status != "archived") + .collect_vec(), + ) + } } #[allow(clippy::too_many_arguments)] diff --git a/src/client_tests.rs b/src/client_tests.rs index 6f54f7e..2775e3f 100644 --- a/src/client_tests.rs +++ b/src/client_tests.rs @@ -125,13 +125,15 @@ fn get_workspace_clients() -> anyhow::Result<()> { "id": 1234, "wid": 12345678, "name": "fkbr.org", - "at": "2021-11-16T09:30:21+00:00" + "at": "2021-11-16T09:30:21+00:00", + "archived": false }, { "id": 2345, "wid": 12345678, "name": "beta male gmbh", - "at": "2021-11-16T08:42:34+00:00" + "at": "2021-11-16T08:42:34+00:00", + "archived": false } ] ); @@ -156,7 +158,7 @@ fn get_workspace_clients() -> anyhow::Result<()> { )?; let clients = client - .get_workspace_clients(false, 12345678)? + .get_workspace_clients(false, false, 12345678)? .unwrap_or_default(); let first_client = clients.first().unwrap(); let second_client = clients.get(1).unwrap(); @@ -191,7 +193,8 @@ fn get_workspace_projects() -> anyhow::Result<()> { "color": "5", "auto_estimates": false, "actual_hours": 4, - "hex_color": "#2da608" + "hex_color": "#2da608", + "status": "active" }, { "id": 987654321, @@ -209,7 +212,8 @@ fn get_workspace_projects() -> anyhow::Result<()> { "actual_hours": 23, "rate": 100, "currency": "EUR", - "hex_color": "#525266" + "hex_color": "#525266", + "status": "active" } ] ); @@ -233,7 +237,7 @@ fn get_workspace_projects() -> anyhow::Result<()> { server.url().parse()?, )?; - let projects = client.get_workspace_projects(false, 12345678)?; + let projects = client.get_workspace_projects(false, false, 12345678)?; let first_project = projects.first().unwrap(); let second_project = projects.get(1).unwrap(); @@ -424,7 +428,8 @@ fn create_client() -> anyhow::Result<()> { { "id": 1234567890, "wid": 123456789, - "name": "fkbr.org" + "name": "fkbr.org", + "archived": false } ); diff --git a/src/commands/clients.rs b/src/commands/clients.rs index 6fde2d3..8f35f01 100644 --- a/src/commands/clients.rs +++ b/src/commands/clients.rs @@ -32,14 +32,17 @@ pub fn create( pub fn list( debug: bool, + include_archived: bool, format: &Format, client: &TogglClient, ) -> anyhow::Result<()> { let me = client.get_me(debug)?; - if let Ok(Some(clients)) = - client.get_workspace_clients(debug, me.default_workspace_id) - { + if let Ok(Some(clients)) = client.get_workspace_clients( + debug, + include_archived, + me.default_workspace_id, + ) { match format { Format::Json => output_values_json(&clients), Format::Raw => output_values_raw(&clients), diff --git a/src/commands/projects.rs b/src/commands/projects.rs index 59017ea..ae357b9 100644 --- a/src/commands/projects.rs +++ b/src/commands/projects.rs @@ -9,12 +9,16 @@ use crate::{ pub fn list( debug: bool, + include_archived: bool, format: &Format, client: &TogglClient, ) -> anyhow::Result<()> { let me = client.get_me(debug)?; - let workspace_projects = - client.get_workspace_projects(debug, me.default_workspace_id)?; + let workspace_projects = client.get_workspace_projects( + debug, + include_archived, + me.default_workspace_id, + )?; if workspace_projects.is_empty() { println!("No entries found!"); diff --git a/src/commands/time_entries.rs b/src/commands/time_entries.rs index 0d7c0e1..b019925 100644 --- a/src/commands/time_entries.rs +++ b/src/commands/time_entries.rs @@ -76,9 +76,9 @@ pub fn list( let workspace_id = me.default_workspace_id; - let projects = client.get_workspace_projects(debug, workspace_id)?; + let projects = client.get_workspace_projects(debug, false, workspace_id)?; let clients = client - .get_workspace_clients(debug, workspace_id)? + .get_workspace_clients(debug, false, workspace_id)? .unwrap_or_default(); let output_entries = collect_output_entries( @@ -165,7 +165,7 @@ pub fn create( ) -> anyhow::Result<()> { let me = client.get_me(debug)?; let workspace_id = me.default_workspace_id; - let projects = client.get_workspace_projects(debug, workspace_id)?; + let projects = client.get_workspace_projects(debug, false, workspace_id)?; let project = projects .iter() @@ -273,7 +273,7 @@ pub fn start( ) -> anyhow::Result<()> { let me = client.get_me(debug)?; let workspace_id = me.default_workspace_id; - let projects = client.get_workspace_projects(debug, workspace_id)?; + let projects = client.get_workspace_projects(debug, false, workspace_id)?; let project = projects .iter() diff --git a/src/commands/time_entries_tests.rs b/src/commands/time_entries_tests.rs index cf7b257..c08cd8b 100644 --- a/src/commands/time_entries_tests.rs +++ b/src/commands/time_entries_tests.rs @@ -457,7 +457,8 @@ fn projects() -> Value { "color": "5", "auto_estimates": false, "actual_hours": 4, - "hex_color": "#2da608" + "hex_color": "#2da608", + "status": "active" }, { "id": 987654321, @@ -475,7 +476,8 @@ fn projects() -> Value { "actual_hours": 23, "rate": 100, "currency": "EUR", - "hex_color": "#525266" + "hex_color": "#525266", + "status": "active" } ] ) diff --git a/src/main.rs b/src/main.rs index 5dacf71..155a334 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use crate::cli::{Clients, Options, SubCommand, TimeEntries}; use crate::config::init_settings_file; use clap::Parser; -use cli::{Reports, Settings}; +use cli::{Projects, Reports, Settings}; use client::init_client; use report_client::init_report_client; @@ -25,11 +25,18 @@ fn main() -> anyhow::Result<()> { SubCommand::Settings(action) => match action { Settings::Init => init_settings_file()?, }, - SubCommand::Projects(_action) => { - let client = init_client()?; + SubCommand::Projects(action) => match action { + Projects::List(list_projects) => { + let client = init_client()?; - commands::projects::list(debug, &format, &client)?; - } + commands::projects::list( + debug, + list_projects.include_archived, + &format, + &client, + )?; + } + }, SubCommand::Workspaces(_action) => { let client = init_client()?; @@ -70,9 +77,14 @@ fn main() -> anyhow::Result<()> { let client = init_client()?; commands::clients::create(debug, &format, &create_client, &client)? } - Clients::List => { + Clients::List(list_clients) => { let client = init_client()?; - commands::clients::list(debug, &format, &client)?; + commands::clients::list( + debug, + list_clients.include_archived, + &format, + &client, + )?; } }, diff --git a/src/model.rs b/src/model.rs index 3cbee08..d9890a9 100644 --- a/src/model.rs +++ b/src/model.rs @@ -26,6 +26,7 @@ pub struct Project { pub id: u64, pub name: String, pub wid: u64, + pub status: String, pub cid: Option, } @@ -56,6 +57,7 @@ pub struct TimeEntry { pub struct Client { pub id: u64, pub name: String, + pub archived: bool, } #[derive(Debug, Clone, Copy)]