Skip to content

Commit 04a9e61

Browse files
committed
Add vacation countdown command
1 parent ada0dbf commit 04a9e61

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

cmd/vacation.go

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ const (
1717
)
1818

1919
var (
20-
vacationDetailsverbose bool
21-
vacationNote string
22-
vacationHalfDay bool
23-
vacationFrom string
24-
vacationAmount int
20+
vacationDetailsverbose bool
21+
vacationNote string
22+
vacationHalfDay bool
23+
vacationFrom string
24+
vacationAmount int
25+
vacationCountdownVerbose bool
2526
)
2627

2728
func init() {
@@ -34,6 +35,9 @@ func init() {
3435
vacationCreateCommand.Flags().IntVarP(&vacationAmount, "amount", "a", 1, "Create amount of consecutive vacation days beginning at from date [Default: 1]")
3536
vacationCommand.AddCommand(vacationCreateCommand)
3637

38+
vacationCountdownCommand.Flags().BoolVarP(&vacationCountdownVerbose, "verbose", "v", false, "verbose output")
39+
vacationCommand.AddCommand(vacationCountdownCommand)
40+
3741
rootCmd.AddCommand(vacationCommand)
3842
}
3943

@@ -144,7 +148,7 @@ var vacationCreateCommand = &cobra.Command{
144148
projectIdForVacation := domain.NewProjectId(projectId)
145149
serviceIdForVacation := domain.NewServiceId(serviceId)
146150

147-
fromDay := domain.Today()
151+
fromDay := domain.Today()
148152
if vacationFrom != "" {
149153
fromDay, err = domain.ParseLocalDate(vacationFrom)
150154
if err != nil {
@@ -177,3 +181,67 @@ var vacationCreateCommand = &cobra.Command{
177181
return nil
178182
},
179183
}
184+
185+
var vacationCountdownCommand = &cobra.Command{
186+
Use: "countdown",
187+
Short: "shows how much time is left to your next vacation",
188+
RunE: func(cmd *cobra.Command, args []string) error {
189+
vacation := application.Conf.GetVacation()
190+
191+
if vacation.ServiceId == "" {
192+
return errors.New(textProjectOrServiceNotConfigured)
193+
}
194+
195+
serviceId, err := strconv.Atoi(vacation.ServiceId)
196+
if err != nil {
197+
return errors.New(textProjectOrServiceNotConfigured)
198+
}
199+
200+
today := domain.Today()
201+
202+
entries, err := application.MiteApi.TimeEntries(&domain.TimeEntryQuery{
203+
From: &today,
204+
Direction: "asc",
205+
ServiceId: domain.NewServiceId(serviceId),
206+
})
207+
if err != nil {
208+
return err
209+
}
210+
211+
firstEntry := entries[0]
212+
213+
unixSeconds := firstEntry.Date.Unix()
214+
secondsLeft := unixSeconds - today.Unix()
215+
minutesLeft := secondsLeft / 60
216+
hoursLeft := minutesLeft / 60
217+
daysLeft := hoursLeft / 24
218+
219+
withHours := hoursLeft - (daysLeft * 24)
220+
withMinutes := minutesLeft - (hoursLeft * 60)
221+
withSeconds := secondsLeft - (minutesLeft * 60)
222+
223+
fmt.Printf("Your next vacation starts in:\n"+
224+
"\n"+
225+
"\t %dd %dh %dm %ds\n\n",
226+
daysLeft,
227+
withHours,
228+
withMinutes,
229+
withSeconds)
230+
231+
if vacationCountdownVerbose {
232+
fmt.Printf("\t===================\n"+
233+
"\n"+
234+
"\t Total: \n"+
235+
"\t - %d days\n"+
236+
"\t - %d hours\n"+
237+
"\t - %d minutes\n"+
238+
"\t - %d seconds\n\n",
239+
daysLeft,
240+
hoursLeft,
241+
minutesLeft,
242+
secondsLeft)
243+
}
244+
245+
return nil
246+
},
247+
}

domain/datetime.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func (d LocalDate) Before(b LocalDate) bool {
4141
return d.time.Before(b.time)
4242
}
4343

44+
func (d LocalDate) Unix() int64 {
45+
return d.time.Unix()
46+
}
47+
4448
func (d LocalDate) String() string {
4549
return d.time.Format(ISO8601)
4650
}

0 commit comments

Comments
 (0)