aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-06-03 16:56:42 +0200
committerTeddy Wing2017-06-03 16:56:42 +0200
commit341e22ef6b8290bba8ba43ecddb9f1175b3cafb9 (patch)
tree12d843e380b5edb4988b860c909d68ccef1bca7b
parent4a4b8e00ae62ec3898b7e4f590bd4a3660a0f535 (diff)
downloadtimetasker-341e22ef6b8290bba8ba43ecddb9f1175b3cafb9.tar.bz2
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.
-rw-r--r--main.go19
1 files changed, 17 insertions, 2 deletions
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,
)