From 341e22ef6b8290bba8ba43ecddb9f1175b3cafb9 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Jun 2017 16:56:42 +0200 Subject: main.go: Handle the `--date` argument If no date is passed in, default to the current date. Otherwise, parse the date into a time that can be used to create the `TimeEntry`. * Rename `date` to `date_str` to allow us to use `date` for the `time.Time` that gets sent to `NewTimeEntry()`. * If parsing fails, print an error message and exit. * In order to use the `err` variable without redefining `date` on line 66, define it at the top of the function. --- main.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index e655967..8b3db02 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,8 @@ type Config struct { var config Config func main() { + var err error + loadConfig() // Parse command line arguments @@ -39,7 +41,7 @@ func main() { Short('t'). Default("7"). Int() - date := kingpin.Flag("date", "Date when work was done (e.g. 2017-01-31)"). + date_str := kingpin.Flag("date", "Date when work was done (e.g. 2017-01-31)"). Short('d'). String() description := kingpin.Flag("description", "Description of work."). @@ -55,10 +57,23 @@ func main() { os.Exit(1) } + var date time.Time + + // If the date argument isn't sent, default to today + if *date_str == "" { + date = time.Now() + } else { + date, err = time.Parse("2006-01-02", *date_str) + if err != nil { + fmt.Printf("Date '%s' could not be parsed. Example: -d 2017-01-31\n", *date_str) + os.Exit(1) + } + } + time_entry := timetask.NewTimeEntry( config.Profile, project, - time.Now(), + date, *time_spent, *description, ) -- cgit v1.2.3