Skip to content

Commit

Permalink
Add --week flag to 'submit'
Browse files Browse the repository at this point in the history
  • Loading branch information
mawkler committed Nov 19, 2024
1 parent 46a5028 commit 51ecb3d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
10 changes: 9 additions & 1 deletion src/cli/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,15 @@ pub enum Command {
},

/// Submit time sheet for week
Submit,
Submit {
/// Week number (defaults to current week if omitted)
#[arg(long, short)]
week: Option<u8>,

/// Year (defaults to current year if omitted)
#[arg(long, short, requires = "week")]
year: Option<i32>,
},

/// Log out
Logout,
Expand Down
19 changes: 9 additions & 10 deletions src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<'a> CommandClient<'a> {
}

pub(crate) async fn get(&self, week: Option<u8>, year: Option<i32>, format: Format) {
let week = create_week_with_fallback(week, year);
let week = to_week_with_fallback(week, year);

match format {
Format::Json => self.get_json(week.as_ref()).await.context("JSON"),
Expand All @@ -84,7 +84,7 @@ impl<'a> CommandClient<'a> {
}

let day = get_days(days);
let week = create_week_with_fallback(week, year);
let week = to_week_with_fallback(week, year);

self.time_sheet_service
.lock()
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<'a> CommandClient<'a> {
exit_with_error!("`--day` is set but no day was provided");
}

let week = create_week_with_fallback(week, year);
let week = to_week_with_fallback(week, year);
self.time_sheet_service
.lock()
.await
Expand All @@ -139,7 +139,7 @@ impl<'a> CommandClient<'a> {
week: Option<u8>,
year: Option<i32>,
) {
let week = create_week_with_fallback(week, year);
let week = to_week_with_fallback(week, year);

self.repository
.lock()
Expand All @@ -152,11 +152,12 @@ impl<'a> CommandClient<'a> {
});
}

pub(crate) async fn submit(&mut self) {
pub(crate) async fn submit(&mut self, week: Option<u8>, year: Option<i32>) {
let week = to_week_with_fallback(week, year);
self.repository
.lock()
.await
.submit()
.submit(week.as_ref())
.await
.unwrap_or_else(|err| {
exit_with_error!("Failed to submit: {}", error_stack_fmt(&err));
Expand All @@ -172,10 +173,8 @@ fn get_days(days: Option<Days>) -> Days {
})
}

fn create_week_with_fallback(week: Option<u8>, year: Option<i32>) -> Option<WeekNumber> {
fn to_week_with_fallback(week: Option<u8>, year: Option<i32>) -> Option<WeekNumber> {
week.map(|week| {
// Fall back to today's year
let year = year.unwrap_or_else(|| year.unwrap_or_else(|| chrono::Utc::now().year()));
WeekNumber::new(week, year).unwrap_or_else(|err| exit_with_error!("{err}"))
WeekNumber::new_with_fallback(week, year).unwrap_or_else(|err| exit_with_error!("{err}"))
})
}
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub(crate) mod arguments;
pub(crate) mod commands;
pub(crate) mod day_parser;
pub(crate) mod rendering;
pub(crate) mod week;
8 changes: 7 additions & 1 deletion src/domain/models/week.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::anyhow;
use chrono::{NaiveDate, Weekday};
use chrono::{Datelike, NaiveDate, Weekday};
use std::fmt::Display;

#[derive(Debug, Clone)]
Expand All @@ -15,6 +15,12 @@ impl WeekNumber {
Ok(Self { number: week, year })
}

pub(crate) fn new_with_fallback(week: u8, year: Option<i32>) -> anyhow::Result<Self> {
// Fall back to today's year
let year = year.unwrap_or_else(|| year.unwrap_or_else(|| chrono::Utc::now().year()));
WeekNumber::new(week, year)
}

pub(crate) fn first_day(&self) -> Option<NaiveDate> {
first_day_of_week(self.number, self.year)
}
Expand Down
17 changes: 15 additions & 2 deletions src/infrastructure/repositories/time_sheet_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ impl TimeSheetRepository<'_> {
}

// Fall back to today's week
//
// This is a small optimization since we don't need to make an extra request if we want the
// current week (i.e. `week` is `None`)
let time_registration = self.get_time_registration().await?;
Ok(time_registration.into())
}
Expand Down Expand Up @@ -306,8 +309,18 @@ impl TimeSheetRepository<'_> {
.with_context(|| format!("Failed to get tasks for job '{job}'"))
}

pub(crate) async fn submit(&mut self) -> Result<()> {
let container_instance = self.get_container_instance().await?;
pub(crate) async fn submit(&mut self, week: Option<&WeekNumber>) -> Result<()> {
// Set the week
let _ = self
.get_time_sheet(week)
.await
.context("Failed to get time sheet")?;

let container_instance = self
.get_container_instance()
.await
.context("Failed to get container instance")?;

let concurrency_control = self
.client
.submit(&container_instance)
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn main() -> anyhow::Result<()> {
year,
} => command_client.clear(&job, &task, day, week, year).await,
// TODO: haven't actually tested this yet (can only be tested once a week)
Command::Submit => command_client.submit().await,
Command::Submit { week, year } => command_client.submit(week, year).await,
Command::Logout => command_client.logout().await,
Command::Line(line) => match line {
Line::Delete {
Expand Down

0 comments on commit 51ecb3d

Please sign in to comment.