Adapting --range: lowercase switch and better messages

This commit is contained in:
Martin (Schretzi) Fuchsluger
2025-02-16 08:18:14 +01:00
parent 6b6e4e5ce6
commit beef2d767e
3 changed files with 20 additions and 8 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ package z
import ( import (
"os" "os"
"fmt" "fmt"
"strings"
"encoding/json" "encoding/json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -80,7 +81,7 @@ func init() {
exportCmd.Flags().StringVar(&format, "format", "zeit", "Format to export, possible values: zeit, tyme") exportCmd.Flags().StringVar(&format, "format", "zeit", "Format to export, possible values: zeit, tyme")
exportCmd.Flags().StringVar(&since, "since", "", "Date/time to start the export from") exportCmd.Flags().StringVar(&since, "since", "", "Date/time to start the export from")
exportCmd.Flags().StringVar(&until, "until", "", "Date/time to export until") exportCmd.Flags().StringVar(&until, "until", "", "Date/time to export until")
exportCmd.Flags().StringVar(&listRange, "range", "", "shortcut to set since/until for a given range (today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth)") exportCmd.Flags().StringVar(&listRange, "range", "", "Shortcut for --since and --until that accepts: " + strings.Join(Ranges(), ", "))
exportCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be exported") exportCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be exported")
exportCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be exported") exportCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be exported")
+16 -6
View File
@@ -188,6 +188,16 @@ func GetGitLog(repo string, since time.Time, until time.Time) (string, string, e
return stdoutStr, stderrStr, nil return stdoutStr, stderrStr, nil
} }
func Ranges() []string {
return []string{
"today",
"yesterday",
"thisWeek",
"lastWeek",
"thisMonth",
"lastMonth",
}
}
func ParseSinceUntil(since string, until string, listRange string) (time.Time, time.Time) { func ParseSinceUntil(since string, until string, listRange string) (time.Time, time.Time) {
@@ -223,29 +233,29 @@ func ParseSinceUntil(since string, until string, listRange string) (time.Time, t
loc, _ := time.LoadLocation("Local") loc, _ := time.LoadLocation("Local")
time.Local = loc time.Local = loc
switch listRange { switch strings.ToLower(listRange) {
case "today": case "today":
sinceTime = now.BeginningOfDay() sinceTime = now.BeginningOfDay()
untilTime = now.EndOfDay() untilTime = now.EndOfDay()
case "yesterday": case "yesterday":
sinceTime = now.BeginningOfDay().AddDate(0, 0, -1) sinceTime = now.BeginningOfDay().AddDate(0, 0, -1)
untilTime = now.EndOfDay().AddDate(0, 0, -1) untilTime = now.EndOfDay().AddDate(0, 0, -1)
case "thisWeek": case "thisweek":
sinceTime = now.BeginningOfWeek() sinceTime = now.BeginningOfWeek()
untilTime = now.EndOfWeek() untilTime = now.EndOfWeek()
case "lastWeek": case "lastweek":
lastWeekDay := time.Now().AddDate(0, 0, -7) lastWeekDay := time.Now().AddDate(0, 0, -7)
sinceTime = now.With(lastWeekDay).BeginningOfWeek() sinceTime = now.With(lastWeekDay).BeginningOfWeek()
untilTime = now.With(lastWeekDay).EndOfWeek() untilTime = now.With(lastWeekDay).EndOfWeek()
case "thisMonth": case "thismonth":
sinceTime = now.BeginningOfMonth() sinceTime = now.BeginningOfMonth()
untilTime = now.EndOfMonth() untilTime = now.EndOfMonth()
case "lastMonth": case "lastmonth":
lastMonthDay := time.Now().AddDate(0, -1, 0) lastMonthDay := time.Now().AddDate(0, -1, 0)
sinceTime = now.With(lastMonthDay).BeginningOfMonth() sinceTime = now.With(lastMonthDay).BeginningOfMonth()
untilTime = now.With(lastMonthDay).EndOfMonth() untilTime = now.With(lastMonthDay).EndOfMonth()
default: default:
fmt.Println("unkown range selection: (today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth)") fmt.Println("Unknown range selection, possible options: ", strings.Join(Ranges(), " "))
os.Exit(1) os.Exit(1)
} }
} }
+2 -1
View File
@@ -2,6 +2,7 @@ package z
import ( import (
"fmt" "fmt"
"strings"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -38,7 +39,7 @@ func init() {
rootCmd.AddCommand(listCmd) rootCmd.AddCommand(listCmd)
listCmd.Flags().StringVar(&since, "since", "", "Date/time to start the list from") listCmd.Flags().StringVar(&since, "since", "", "Date/time to start the list from")
listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until") listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until")
listCmd.Flags().StringVar(&listRange, "range", "", "shortcut to set since/until for a given range (today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth)") listCmd.Flags().StringVar(&listRange, "range", "", "Shortcut for --since and --until that accepts: " + strings.Join(Ranges(), ", "))
listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed") listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed")
listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed") listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed")
listCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes") listCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")