-
Notifications
You must be signed in to change notification settings - Fork 1
/
flags.go
92 lines (78 loc) · 2.89 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type FlagData struct {
Token string
Repos []string
ProjectOwner string
ProjectNumber int
IncludeClosed bool
Filters Filters
}
type Filters struct {
Authors []string
LabelsOr []string
LabelsAnd []string
}
func configureFlags(root *cobra.Command) error {
flags := FlagData{}
pflags := root.PersistentFlags()
pflags.StringVarP(&flags.Token, "token", "t", "", "github oauth token (GITHUB_TOKEN)")
pflags.StringSliceVarP(&flags.Repos, "repo", "r", []string{}, "github repo name (GITHUB_REPO) or a set of repos `owner1/repo1,owner2/repo2`")
pflags.StringVarP(&flags.ProjectOwner, "project-owner", "o", "", "github project owner (GITHUB_PROJECT_OWNER)")
pflags.IntVarP(&flags.ProjectNumber, "project-number", "p", 0, "github project number (GITHUB_PROJECT_NUMBER)")
pflags.BoolVarP(&flags.IncludeClosed, "include-closed", "c", false, "include closed prs/issues")
pflags.StringSliceVarP(&flags.Filters.Authors, "authors", "a", []string{}, "only sync prs by these authors. ie 'katbyte,author2,author3'")
pflags.StringSliceVarP(&flags.Filters.LabelsOr, "labels-or", "l", []string{}, "filter that match any label conditions. ie 'label1,label2,-not-this-label'")
pflags.StringSliceVarP(&flags.Filters.LabelsAnd, "labels-and", "", []string{}, "filter that match all label conditions. ie 'label1,label2,-not-this-label'")
// binding map for viper/pflag -> env
m := map[string]string{
"token": "GITHUB_TOKEN",
"repo": "GITHUB_REPO", // todo rename this to repos
"project-owner": "GITHUB_PROJECT_OWNER",
"project-number": "GITHUB_PROJECT_NUMBER",
"include-closed": "GITHUB_INCLUDE_CLOSED",
"authors": "GITHUB_AUTHORS",
"labels-or": "GITHUB_LABELS_OR",
"labels-and": "GITHUB_LABELS_AND",
}
for name, env := range m {
if err := viper.BindPFlag(name, pflags.Lookup(name)); err != nil {
return fmt.Errorf("error binding '%s' flag: %w", name, err)
}
if env != "" {
if err := viper.BindEnv(name, env); err != nil {
return fmt.Errorf("error binding '%s' to env '%s' : %w", name, env, err)
}
}
}
return nil
}
func GetFlags() FlagData {
// TODO BUG for some reason it is not correctly splitting on ,? so hack this in
authors := viper.GetStringSlice("authors")
if len(authors) > 0 {
authors = strings.Split(authors[0], ",")
}
repos := viper.GetStringSlice("repo")
if len(repos) > 0 {
repos = strings.Split(repos[0], ",")
}
// there has to be an easier way....
return FlagData{
Token: viper.GetString("token"),
Repos: repos,
ProjectNumber: viper.GetInt("project-number"),
ProjectOwner: viper.GetString("project-owner"),
IncludeClosed: viper.GetBool("include-closed"),
Filters: Filters{
Authors: authors,
LabelsOr: viper.GetStringSlice("labels-or"),
LabelsAnd: viper.GetStringSlice("labels-and"),
},
}
}