2021-09-06 01:11:17 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2023-09-08 09:40:02 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-06 01:11:17 +08:00
|
|
|
|
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CsvFlag is a wrapper around cli.StringFlag, with an added GetValues() method
|
|
|
|
// to retrieve comma separated string values as a slice.
|
|
|
|
type CsvFlag struct {
|
|
|
|
cli.StringFlag
|
|
|
|
AvailableFields []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCsvFlag creates a CsvFlag, while setting its usage string and default values
|
|
|
|
func NewCsvFlag(name, usage string, aliases, availableValues, defaults []string) *CsvFlag {
|
Implement more issue filters (#400)
This adds new filters to `tea issues ls` and `tea pr ls`, made available in SDK 0.15:
```
--state value Filter by state (all|open|closed) (default: open)
--keyword value, -k value Filter by search string
--labels value, -L value Comma-separated list of labels to match issues against.
--milestones value, -m value Comma-separated list of milestones to match issues against.
--author value, -A value
--assignee value, -a value
--mentions value, -M value
--from value, -F value Filter by activity after this date
--until value, -u value Filter by activity before this date
```
Note: I felt free to change parameter names as exposed by SDK & API, as the names exposed by them are partially bollocks (eg `mentioned_by`) and or inconsistent with usage in other commands (eg `tea times --until`)
fixes #376, related #323
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/400
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
2021-12-03 03:26:48 +08:00
|
|
|
var availableDesc string
|
|
|
|
if len(availableValues) != 0 {
|
|
|
|
availableDesc = " Available values:"
|
|
|
|
}
|
2021-09-06 01:11:17 +08:00
|
|
|
return &CsvFlag{
|
|
|
|
AvailableFields: availableValues,
|
|
|
|
StringFlag: cli.StringFlag{
|
|
|
|
Name: name,
|
|
|
|
Aliases: aliases,
|
|
|
|
Value: strings.Join(defaults, ","),
|
Implement more issue filters (#400)
This adds new filters to `tea issues ls` and `tea pr ls`, made available in SDK 0.15:
```
--state value Filter by state (all|open|closed) (default: open)
--keyword value, -k value Filter by search string
--labels value, -L value Comma-separated list of labels to match issues against.
--milestones value, -m value Comma-separated list of milestones to match issues against.
--author value, -A value
--assignee value, -a value
--mentions value, -M value
--from value, -F value Filter by activity after this date
--until value, -u value Filter by activity before this date
```
Note: I felt free to change parameter names as exposed by SDK & API, as the names exposed by them are partially bollocks (eg `mentioned_by`) and or inconsistent with usage in other commands (eg `tea times --until`)
fixes #376, related #323
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/400
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
2021-12-03 03:26:48 +08:00
|
|
|
Usage: fmt.Sprintf(`Comma-separated list of %s.%s
|
2021-09-06 01:11:17 +08:00
|
|
|
%s
|
Implement more issue filters (#400)
This adds new filters to `tea issues ls` and `tea pr ls`, made available in SDK 0.15:
```
--state value Filter by state (all|open|closed) (default: open)
--keyword value, -k value Filter by search string
--labels value, -L value Comma-separated list of labels to match issues against.
--milestones value, -m value Comma-separated list of milestones to match issues against.
--author value, -A value
--assignee value, -a value
--mentions value, -M value
--from value, -F value Filter by activity after this date
--until value, -u value Filter by activity before this date
```
Note: I felt free to change parameter names as exposed by SDK & API, as the names exposed by them are partially bollocks (eg `mentioned_by`) and or inconsistent with usage in other commands (eg `tea times --until`)
fixes #376, related #323
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/400
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
2021-12-03 03:26:48 +08:00
|
|
|
`, usage, availableDesc, strings.Join(availableValues, ",")),
|
2021-09-06 01:11:17 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValues returns the value of the flag, parsed as a commaseparated list
|
|
|
|
func (f CsvFlag) GetValues(ctx *cli.Context) ([]string, error) {
|
|
|
|
val := ctx.String(f.Name)
|
|
|
|
selection := strings.Split(val, ",")
|
|
|
|
if f.AvailableFields != nil && val != "" {
|
|
|
|
for _, field := range selection {
|
|
|
|
if !utils.Contains(f.AvailableFields, field) {
|
|
|
|
return nil, fmt.Errorf("Invalid field '%s'", field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selection, nil
|
|
|
|
}
|