zeit/z/listCmd.go

82 lines
2.1 KiB
Go
Raw Normal View History

2020-10-15 21:45:14 +00:00
package z
import (
"os"
"fmt"
"time"
2020-10-15 21:45:14 +00:00
"github.com/spf13/cobra"
2020-10-17 19:19:05 +00:00
"github.com/shopspring/decimal"
2020-10-15 21:45:14 +00:00
)
2020-10-17 19:19:05 +00:00
var listTotalTime bool
2020-10-15 21:45:14 +00:00
var listCmd = &cobra.Command{
Use: "list",
2020-10-16 00:08:49 +00:00
Short: "List activities",
Long: "List all tracked activities.",
2020-10-15 21:45:14 +00:00
Run: func(cmd *cobra.Command, args []string) {
user := GetCurrentUser()
entries, err := database.ListEntries(user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
var sinceTime time.Time
var untilTime time.Time
if since != "" {
sinceTime, err = time.Parse(time.RFC3339, since)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
if until != "" {
untilTime, err = time.Parse(time.RFC3339, until)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
var filteredEntries []Entry
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
2020-10-17 19:19:05 +00:00
totalHours := decimal.NewFromInt(0);
for _, entry := range filteredEntries {
2020-10-17 19:19:05 +00:00
duration := entry.Finish.Sub(entry.Begin)
durationDec := decimal.NewFromFloat(duration.Hours())
totalHours = totalHours.Add(durationDec)
2020-10-17 20:14:00 +00:00
fmt.Printf("%s\n", entry.GetOutput(false))
2020-10-15 21:45:14 +00:00
}
2020-10-17 19:19:05 +00:00
if listTotalTime == true {
fmt.Printf("\nTOTAL: %s H\n\n", totalHours.StringFixed(2))
}
2020-10-15 21:45:14 +00:00
return
},
}
func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().StringVar(&since, "since", "", "Date/time to start the list from")
listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until")
listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed")
listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed")
2020-10-17 19:19:05 +00:00
listCmd.Flags().BoolVar(&listTotalTime, "total", false, "Show total time of hours for listed activities")
2020-10-15 21:45:14 +00:00
var err error
database, err = InitDatabase()
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}