Skip to content

Commit

Permalink
👾 Allow tags to be set in start command (#88)
Browse files Browse the repository at this point in the history
- Tags can be configured in the start command using `--tags` or `-t` flag
   Multiple tags must be separated by spaces.

* 🩹 Simplify `interactively_create_time_entry`

- Avoid configuring entity fields in two spots
- Now it only concerns itself with the field we modify interactively
  - `billable`
  - `project`
  - `task`

Closes #83
  • Loading branch information
shantanuraj authored Dec 11, 2024
1 parent e8dde57 commit aaaddbb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
6 changes: 6 additions & 0 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ pub enum Command {
help = "Exact name of the project you want the time entry to be associated with"
)]
project: Option<String>,
#[structopt(
short,
long,
help = "Space separated list of tags to associate with the time entry, e.g. 'tag1 tag2 tag3'"
)]
tags: Option<Vec<String>>,
#[structopt(short, long)]
billable: bool,
},
Expand Down
47 changes: 19 additions & 28 deletions src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ use crate::commands;
use crate::config;
use crate::models;
use crate::models::Entities;
use crate::models::Project;
use crate::picker::ItemPicker;
use crate::picker::PickableItem;
use crate::picker::PickableItemKind;
use crate::utilities;
use api::client::ApiClient;
use colored::Colorize;
use commands::stop::{StopCommand, StopCommandOrigin};
use models::ResultWithDefaultError;
use models::TimeEntry;
use models::{ResultWithDefaultError, TimeEntry};

pub struct StartCommand;

fn interactively_create_time_entry(
default_time_entry: TimeEntry,
workspace_id: i64,
time_entry: TimeEntry,
entities: Entities,
picker: Box<dyn ItemPicker>,
description: String,
project: Option<Project>,
billable: bool,
) -> TimeEntry {
let yes_or_default_no = [
"y".to_string(),
Expand All @@ -32,8 +26,8 @@ fn interactively_create_time_entry(
"".to_string(),
];

let (project, task) = match project {
Some(_) => (project, None),
let (project, task) = match time_entry.project {
Some(_) => (time_entry.project, None),
None => {
if entities.projects.is_empty() {
(None, None)
Expand Down Expand Up @@ -72,23 +66,21 @@ fn interactively_create_time_entry(
};

// Only ask for billable if the user didn't provide a value AND if the selected project doesn't have a default billable setting.
let billable = billable
let billable = time_entry.billable
|| project.clone().and_then(|p| p.billable).unwrap_or(
utilities::read_from_stdin_with_constraints(
"Is your time entry billable? (y/N): ",
&yes_or_default_no,
) == "y",
);

let task = task.or(default_time_entry.task.clone());
let task = task.or(time_entry.task.clone());

TimeEntry {
billable,
description,
workspace_id,
project,
task,
..default_time_entry
..time_entry
}
}

Expand All @@ -98,6 +90,7 @@ impl StartCommand {
picker: Box<dyn ItemPicker>,
description: Option<String>,
project_name: Option<String>,
tags: Option<Vec<String>>,
billable: bool,
interactive: bool,
) -> ResultWithDefaultError<()> {
Expand Down Expand Up @@ -127,29 +120,27 @@ impl StartCommand {
})
.or(default_time_entry.project.clone());

let tags = tags.unwrap_or(default_time_entry.tags.clone());

let billable = billable
|| default_time_entry.billable
|| project.clone().and_then(|p| p.billable).unwrap_or(false);

let description = description.unwrap_or(default_time_entry.description.clone());

let time_entry_to_create = if interactive {
interactively_create_time_entry(
default_time_entry,
workspace_id,
entities.clone(),
picker,
let time_entry_to_create = {
let initial_entry = TimeEntry {
description,
project,
tags,
billable,
)
} else {
TimeEntry {
billable,
description,
project,
workspace_id,
..default_time_entry
..TimeEntry::default()
};
if interactive {
interactively_create_time_entry(initial_entry, entities.clone(), picker)
} else {
initial_entry
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ async fn execute_subcommand(args: CommandLineArguments) -> ResultWithDefaultErro
billable,
description,
project,
tags,
} => {
StartCommand::execute(
get_default_api_client()?,
picker,
description,
project,
tags,
billable,
interactive,
)
Expand Down

0 comments on commit aaaddbb

Please sign in to comment.