Implemented appendProjectIDToTask flag

This commit is contained in:
マリウス 2021-07-05 19:43:31 -05:00
parent 2465fec28b
commit 0247fe0859
No known key found for this signature in database
GPG key ID: 272ED814BF63261F

View file

@ -1,107 +1,114 @@
package z package z
import ( import (
"os" "fmt"
"fmt" "os"
"time" "time"
"github.com/spf13/cobra"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
"github.com/spf13/cobra"
) )
var listTotalTime bool var listTotalTime bool
var listOnlyProjectsAndTasks bool var listOnlyProjectsAndTasks bool
var appendProjectIDToTask bool
var listCmd = &cobra.Command{ var listCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List activities", Short: "List activities",
Long: "List all tracked activities.", Long: "List all tracked activities.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
user := GetCurrentUser() user := GetCurrentUser()
entries, err := database.ListEntries(user) entries, err := database.ListEntries(user)
if err != nil { if err != nil {
fmt.Printf("%s %+v\n", CharError, err) fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1) os.Exit(1)
} }
var sinceTime time.Time var sinceTime time.Time
var untilTime time.Time var untilTime time.Time
if since != "" { if since != "" {
sinceTime, err = time.Parse(time.RFC3339, since) sinceTime, err = time.Parse(time.RFC3339, since)
if err != nil { if err != nil {
fmt.Printf("%s %+v\n", CharError, err) fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1) os.Exit(1)
} }
} }
if until != "" { if until != "" {
untilTime, err = time.Parse(time.RFC3339, until) untilTime, err = time.Parse(time.RFC3339, until)
if err != nil { if err != nil {
fmt.Printf("%s %+v\n", CharError, err) fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1) os.Exit(1)
} }
} }
var filteredEntries []Entry var filteredEntries []Entry
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime) filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
if err != nil { if err != nil {
fmt.Printf("%s %+v\n", CharError, err) fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1) os.Exit(1)
} }
if listOnlyProjectsAndTasks == true { if listOnlyProjectsAndTasks == true {
var projectsAndTasks = make(map[string]map[string]bool) var projectsAndTasks = make(map[string]map[string]bool)
for _, filteredEntry := range filteredEntries { for _, filteredEntry := range filteredEntries {
taskMap, ok := projectsAndTasks[filteredEntry.Project] taskMap, ok := projectsAndTasks[filteredEntry.Project]
if !ok { if !ok {
taskMap = make(map[string]bool) taskMap = make(map[string]bool)
projectsAndTasks[filteredEntry.Project] = taskMap projectsAndTasks[filteredEntry.Project] = taskMap
} }
taskMap[filteredEntry.Task] = true taskMap[filteredEntry.Task] = true
projectsAndTasks[filteredEntry.Project] = taskMap projectsAndTasks[filteredEntry.Project] = taskMap
} }
for project, _ := range projectsAndTasks { for project, _ := range projectsAndTasks {
fmt.Printf("%s %s\n", CharMore, project) fmt.Printf("%s %s\n", CharMore, project)
for task, _ := range projectsAndTasks[project] { for task, _ := range projectsAndTasks[project] {
fmt.Printf("%*s└── %s\n", 1, " ", task) if appendProjectIDToTask == true {
} fmt.Printf("%*s└── %s [%s]\n", 1, " ", task, project)
} } else {
fmt.Printf("%*s└── %s\n", 1, " ", task)
}
}
}
return return
} }
totalHours := decimal.NewFromInt(0); totalHours := decimal.NewFromInt(0)
for _, entry := range filteredEntries { for _, entry := range filteredEntries {
totalHours = totalHours.Add(entry.GetDuration()) totalHours = totalHours.Add(entry.GetDuration())
fmt.Printf("%s\n", entry.GetOutput(false)) fmt.Printf("%s\n", entry.GetOutput(false))
} }
if listTotalTime == true { if listTotalTime == true {
fmt.Printf("\nTOTAL: %s H\n\n", totalHours.StringFixed(2)) fmt.Printf("\nTOTAL: %s H\n\n", totalHours.StringFixed(2))
} }
return return
}, },
} }
func init() { 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().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(&listTotalTime, "total", false, "Show total time of hours for listed activities") listCmd.Flags().BoolVar(&listTotalTime, "total", false, "Show total time of hours for listed activities")
listCmd.Flags().BoolVar(&listOnlyProjectsAndTasks, "only-projects-and-tasks", false, "Only list projects and their tasks, no entries") listCmd.Flags().BoolVar(&listOnlyProjectsAndTasks, "only-projects-and-tasks", false, "Only list projects and their tasks, no entries")
listCmd.Flags().BoolVar(&appendProjectIDToTask, "append-project-id-to-task", false, "Append project ID to tasks in the list")
var err error var err error
database, err = InitDatabase() database, err = InitDatabase()
if err != nil { if err != nil {
fmt.Printf("%s %+v\n", CharError, err) fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1) os.Exit(1)
} }
} }