Skip to content

Commit

Permalink
feat: --include-archived for projects / clients
Browse files Browse the repository at this point in the history
  • Loading branch information
icepuma committed Feb 11, 2024
1 parent fbe2c88 commit cdb081c
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 32 deletions.
2 changes: 1 addition & 1 deletion check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -euo pipefail

cargo fmt
cargo clippy --all-targets --all-features
cargo test run
cargo test --all-targets
18 changes: 16 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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),
Expand Down
33 changes: 29 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,13 +180,25 @@ impl TogglClient {
pub fn get_workspace_clients(
&self,
debug: bool,
include_archived: bool,
workspace_id: u64,
) -> anyhow::Result<Option<Vec<Client>>> {
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(
Expand Down Expand Up @@ -219,13 +232,25 @@ impl TogglClient {
pub fn get_workspace_projects(
&self,
debug: bool,
include_archived: bool,
workspace_id: u64,
) -> anyhow::Result<Vec<Project>> {
self.request::<Vec<Project>>(
let projects = self.request::<Vec<Project>>(
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)]
Expand Down
19 changes: 12 additions & 7 deletions src/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
);
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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"
}
]
);
Expand All @@ -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();

Expand Down Expand Up @@ -424,7 +428,8 @@ fn create_client() -> anyhow::Result<()> {
{
"id": 1234567890,
"wid": 123456789,
"name": "fkbr.org"
"name": "fkbr.org",
"archived": false
}
);

Expand Down
9 changes: 6 additions & 3 deletions src/commands/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 6 additions & 2 deletions src/commands/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
8 changes: 4 additions & 4 deletions src/commands/time_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions src/commands/time_entries_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -475,7 +476,8 @@ fn projects() -> Value {
"actual_hours": 23,
"rate": 100,
"currency": "EUR",
"hex_color": "#525266"
"hex_color": "#525266",
"status": "active"
}
]
)
Expand Down
26 changes: 19 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()?;

Expand Down Expand Up @@ -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,
)?;
}
},

Expand Down
2 changes: 2 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Project {
pub id: u64,
pub name: String,
pub wid: u64,
pub status: String,
pub cid: Option<u64>,
}

Expand Down Expand Up @@ -56,6 +57,7 @@ pub struct TimeEntry {
pub struct Client {
pub id: u64,
pub name: String,
pub archived: bool,
}

#[derive(Debug, Clone, Copy)]
Expand Down

0 comments on commit cdb081c

Please sign in to comment.