-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework user flag to use new-style filters
Also - validates user input (according to core app `User#login` Regexp). - supports lists of users (separated by a comma).
- Loading branch information
Showing
5 changed files
with
97 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package list | ||
|
||
func initTimeEntriesFlags() { | ||
timeEntriesCmd.Flags().StringVarP( | ||
&user, | ||
"user", | ||
"u", | ||
"me", | ||
"User the time entry tracks expenditures for (can be name, ID or 'me')", | ||
) | ||
for _, filter := range activeTimeEntryFilters { | ||
timeEntriesCmd.Flags().StringVarP( | ||
filter.ValuePointer(), | ||
filter.Name(), | ||
filter.ShortHand(), | ||
filter.DefaultValue(), | ||
filter.Usage(), | ||
) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package filters | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/opf/openproject-cli/components/errors" | ||
"github.com/opf/openproject-cli/components/printer" | ||
"github.com/opf/openproject-cli/components/requests" | ||
) | ||
|
||
const validValueRegexp = `^([\pL0-9_\-@.+ ]+|[0-9]+|me)(,([\pL0-9_\-@.+ ]+|[0-9]+|me))*$` | ||
|
||
type UserFilter struct { | ||
value string | ||
} | ||
|
||
func (f *UserFilter) ValuePointer() *string { | ||
return &f.value | ||
} | ||
|
||
func (f *UserFilter) Value() string { | ||
return f.value | ||
} | ||
|
||
func (f *UserFilter) Name() string { | ||
return "user" | ||
} | ||
|
||
func (f *UserFilter) ShortHand() string { | ||
return "u" | ||
} | ||
|
||
func (f *UserFilter) Usage() string { | ||
return `User the time entry tracks expenditures for (can be name, ID or 'me')` | ||
} | ||
|
||
func (f *UserFilter) ValidateInput() error { | ||
matched, _ := regexp.Match(validValueRegexp, []byte(f.value)) | ||
if !matched { | ||
return errors.Custom(fmt.Sprintf("Invalid user value %s.", printer.Yellow(f.value))) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *UserFilter) DefaultValue() string { | ||
return "me" | ||
} | ||
|
||
func (f *UserFilter) Query() requests.Query { | ||
return requests.NewQuery(nil, []requests.Filter{ | ||
{ | ||
Operator: "=", | ||
Name: "user", | ||
Values: strings.Split(f.value, ","), | ||
}, | ||
}) | ||
} | ||
|
||
func NewUserFilter() *UserFilter { | ||
return &UserFilter{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters