diff options
| author | Teddy Wing | 2017-06-03 16:56:42 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-06-03 16:56:42 +0200 | 
| commit | 341e22ef6b8290bba8ba43ecddb9f1175b3cafb9 (patch) | |
| tree | 12d843e380b5edb4988b860c909d68ccef1bca7b | |
| parent | 4a4b8e00ae62ec3898b7e4f590bd4a3660a0f535 (diff) | |
| download | timetasker-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.go | 19 | 
1 files changed, 17 insertions, 2 deletions
| @@ -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,  	) | 
