zeit/z/listCmd.go

125 lines
3.6 KiB
Go
Raw Normal View History

2020-10-15 21:45:14 +00:00
package z
import (
2022-02-28 16:14:27 +00:00
"fmt"
"os"
"time"
2021-07-06 00:43:31 +00:00
2022-02-28 16:14:27 +00:00
"github.com/shopspring/decimal"
"github.com/spf13/cobra"
2020-10-15 21:45:14 +00:00
)
2020-10-17 19:19:05 +00:00
var listTotalTime bool
var listOnlyProjectsAndTasks bool
2022-04-15 00:11:44 +00:00
var listOnlyTasks bool
2021-07-06 00:43:31 +00:00
var appendProjectIDToTask bool
2020-10-17 19:19:05 +00:00
2020-10-15 21:45:14 +00:00
var listCmd = &cobra.Command{
2022-02-28 16:14:27 +00:00
Use: "list",
Short: "List activities",
Long: "List all tracked activities.",
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)
}
2022-04-15 00:11:44 +00:00
if listOnlyProjectsAndTasks == true || listOnlyTasks == true {
2022-02-28 16:14:27 +00:00
var projectsAndTasks = make(map[string]map[string]bool)
for _, filteredEntry := range filteredEntries {
taskMap, ok := projectsAndTasks[filteredEntry.Project]
if !ok {
taskMap = make(map[string]bool)
projectsAndTasks[filteredEntry.Project] = taskMap
}
taskMap[filteredEntry.Task] = true
projectsAndTasks[filteredEntry.Project] = taskMap
}
for project, _ := range projectsAndTasks {
2022-04-15 00:11:44 +00:00
if listOnlyProjectsAndTasks == true && listOnlyTasks == false {
fmt.Printf("%s %s\n", CharMore, project)
}
2022-02-28 16:14:27 +00:00
for task, _ := range projectsAndTasks[project] {
2022-04-15 00:11:44 +00:00
if listOnlyProjectsAndTasks == true && listOnlyTasks == false {
fmt.Printf("%*s└── ", 1, " ")
}
2022-02-28 16:14:27 +00:00
if appendProjectIDToTask == true {
2022-04-15 00:11:44 +00:00
fmt.Printf("%s [%s]\n", task, project)
2022-02-28 16:14:27 +00:00
} else {
2022-04-15 00:11:44 +00:00
fmt.Printf("%s\n", task)
2022-02-28 16:14:27 +00:00
}
}
}
return
}
totalHours := decimal.NewFromInt(0)
for _, entry := range filteredEntries {
totalHours = totalHours.Add(entry.GetDuration())
fmt.Printf("%s\n", entry.GetOutput(false))
}
if listTotalTime == true {
fmt.Printf("\nTOTAL: %s H\n\n", fmtHours(totalHours));
}
return
},
2020-10-15 21:45:14 +00:00
}
func init() {
2022-02-28 16:14:27 +00:00
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")
listCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
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")
2022-04-15 00:11:44 +00:00
listCmd.Flags().BoolVar(&listOnlyTasks, "only-tasks", false, "Only list tasks, no projects nor entries")
2022-02-28 16:14:27 +00:00
listCmd.Flags().BoolVar(&appendProjectIDToTask, "append-project-id-to-task", false, "Append project ID to tasks in the list")
var err error
database, err = InitDatabase()
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
2020-10-15 21:45:14 +00:00
}