Skip to content

Commit

Permalink
Implement --user flag for list timeentries command
Browse files Browse the repository at this point in the history
Defaults to `me`.
  • Loading branch information
myabc committed Feb 11, 2025
1 parent f92405a commit b5ab3d9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The list can get filtered further.`,

func init() {
initWorkPackagesFlags()
initTimeEntriesFlags()

notificationsCmd.Flags().StringVarP(
&notificationReason,
Expand Down
16 changes: 14 additions & 2 deletions cmd/list/time_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,29 @@ import (
"github.com/spf13/cobra"
)

var user string

var timeEntriesCmd = &cobra.Command{
Use: "timeentries",
Short: "Lists time entries",
Long: "Get a list of all personal time entries.",
Long: "Get a list of all time entries.",
Run: listTimeEntries,
}

func listTimeEntries(_ *cobra.Command, _ []string) {
if all, err := time_entries.All(); err == nil {
if all, err := time_entries.All(timeEntriesFilterOptions()); err == nil {
printer.TimeEntryList(all)
} else {
printer.Error(err)
}
}

func timeEntriesFilterOptions() *map[time_entries.FilterOption]string {
options := make(map[time_entries.FilterOption]string)

if len(user) > 0 {
options[time_entries.User] = user
}

return &options
}
11 changes: 11 additions & 0 deletions cmd/list/time_entries_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package list

func initTimeEntriesFlags() {
timeEntriesCmd.Flags().StringVarP(
&user,
"user",
"u",
"me",
"User the time entry tracks expenditures for (can be name, ID or 'me')",
)
}
9 changes: 7 additions & 2 deletions components/resources/time_entries/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (
"github.com/opf/openproject-cli/models"
)

func All() ([]*models.TimeEntry, error) {
func All(filterOptions *map[FilterOption]string) ([]*models.TimeEntry, error) {
var filters []requests.Filter
filters = append(filters, UserFilter("me"))

for updateOpt, value := range *filterOptions {
if updateOpt == User {
filters = append(filters, UserFilter(value))
}
}

query := requests.NewPaginatedQuery(-1, filters)

Expand Down

0 comments on commit b5ab3d9

Please sign in to comment.