Skip to content

Commit aed343d

Browse files
committed
kepctl: Finish initial refactor
Signed-off-by: Stephen Augustus <[email protected]>
1 parent b3d3fdc commit aed343d

File tree

4 files changed

+211
-68
lines changed

4 files changed

+211
-68
lines changed

cmd/kepctl/cmd/create.go

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,90 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"github.com/pkg/errors"
2021
"github.com/spf13/cobra"
2122

2223
"k8s.io/enhancements/pkg/kepctl"
2324
)
2425

25-
func buildCreateCommand(k *kepctl.Client) *cobra.Command {
26-
opts := kepctl.CreateOpts{}
27-
cmd := &cobra.Command{
28-
Use: "create [KEP]",
29-
Short: "Create a new KEP",
30-
Long: "Create a new KEP using the current KEP template for the given type",
31-
Example: ` kepctl create sig-architecture/000-mykep`,
32-
PreRunE: func(cmd *cobra.Command, args []string) error {
33-
return opts.Validate(args)
34-
},
35-
RunE: func(cmd *cobra.Command, args []string) error {
36-
return k.Create(&opts)
37-
},
26+
// TODO: Struct literal instead?
27+
var createOpts = kepctl.CreateOpts{}
28+
29+
var createCmd = &cobra.Command{
30+
Use: "create [KEP]",
31+
Short: "Create a new KEP",
32+
Long: "Create a new KEP using the current KEP template for the given type",
33+
Example: ` kepctl create sig-architecture/000-mykep`,
34+
SilenceUsage: true,
35+
SilenceErrors: true,
36+
PreRunE: func(cmd *cobra.Command, args []string) error {
37+
return createOpts.Validate(args)
38+
},
39+
RunE: func(*cobra.Command, []string) error {
40+
return runCreate(createOpts)
41+
},
42+
}
43+
44+
func init() {
45+
// TODO: Should these all be global args?
46+
createCmd.PersistentFlags().StringVar(
47+
&createOpts.Title,
48+
"title",
49+
"",
50+
"KEP Title",
51+
)
52+
53+
createCmd.PersistentFlags().StringArrayVar(
54+
&createOpts.Authors,
55+
"authors",
56+
[]string{},
57+
"Authors",
58+
)
59+
60+
createCmd.PersistentFlags().StringArrayVar(
61+
&createOpts.Reviewers,
62+
"reviewers",
63+
[]string{},
64+
"Reviewers",
65+
)
66+
67+
createCmd.PersistentFlags().StringVar(
68+
&createOpts.Type,
69+
"type",
70+
"feature",
71+
"KEP Type",
72+
)
73+
74+
createCmd.PersistentFlags().StringVarP(
75+
&createOpts.State,
76+
"state",
77+
"s",
78+
"provisional",
79+
"KEP State",
80+
)
81+
82+
createCmd.PersistentFlags().StringArrayVar(
83+
&createOpts.SIGS,
84+
"sigs",
85+
[]string{},
86+
"Participating SIGs",
87+
)
88+
89+
createCmd.PersistentFlags().StringArrayVar(
90+
&createOpts.PRRApprovers,
91+
"prr-approver",
92+
[]string{},
93+
"PRR Approver",
94+
)
95+
96+
rootCmd.AddCommand(createCmd)
97+
}
98+
99+
func runCreate(createOpts kepctl.CreateOpts) error {
100+
k, err := kepctl.New(createOpts.RepoPath)
101+
if err != nil {
102+
return errors.Wrap(err, "creating kepctl client")
38103
}
39104

40-
f := cmd.Flags()
41-
f.StringVar(&opts.Title, "title", "", "KEP Title")
42-
f.StringArrayVar(&opts.Authors, "authors", []string{}, "Authors")
43-
f.StringArrayVar(&opts.Reviewers, "reviewers", []string{}, "Reviewers")
44-
f.StringVar(&opts.Type, "type", "feature", "KEP Type")
45-
f.StringVarP(&opts.State, "state", "s", "provisional", "KEP State")
46-
f.StringArrayVar(&opts.SIGS, "sigs", []string{}, "Participating SIGs")
47-
f.StringArrayVar(&opts.PRRApprovers, "prr-approver", []string{}, "PRR Approver")
48-
49-
addRepoPathFlag(f, &opts.CommonArgs)
50-
return cmd
105+
return k.Create(&createOpts)
51106
}

cmd/kepctl/cmd/promote.go

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,56 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"github.com/pkg/errors"
2021
"github.com/spf13/cobra"
2122

2223
"k8s.io/enhancements/pkg/kepctl"
2324
)
2425

25-
func buildPromoteCommand(k *kepctl.Client) *cobra.Command {
26-
opts := kepctl.PromoteOpts{}
27-
cmd := &cobra.Command{
28-
Use: "promote [KEP]",
29-
Short: "Promote a KEP",
30-
Long: "Promote a KEP to a new stage for a target release",
31-
Example: ` kepctl promote sig-architecture/000-mykep --stage beta --release v1.20`,
32-
PreRunE: func(cmd *cobra.Command, args []string) error {
33-
return opts.Validate(args)
34-
},
35-
RunE: func(cmd *cobra.Command, args []string) error {
36-
return k.Promote(&opts)
37-
},
38-
}
26+
// TODO: Struct literal instead?
27+
var promoteOpts = kepctl.PromoteOpts{}
28+
29+
var promoteCmd = &cobra.Command{
30+
Use: "promote [KEP]",
31+
Short: "Promote a KEP",
32+
Long: "Promote a KEP to a new stage for a target release",
33+
Example: ` kepctl promote sig-architecture/000-mykep --stage beta --release v1.20`,
34+
SilenceUsage: true,
35+
SilenceErrors: true,
36+
PreRunE: func(cmd *cobra.Command, args []string) error {
37+
return promoteOpts.Validate(args)
38+
},
39+
RunE: func(*cobra.Command, []string) error {
40+
return runPromote(promoteOpts)
41+
},
42+
}
43+
44+
func init() {
45+
// TODO: Should these all be global args?
46+
promoteCmd.PersistentFlags().StringVarP(
47+
&promoteOpts.Stage,
48+
"stage",
49+
"s",
50+
"",
51+
"KEP Stage",
52+
)
3953

40-
f := cmd.Flags()
41-
f.StringVarP(&opts.Stage, "stage", "s", "", "KEP Stage")
42-
f.StringVarP(&opts.Release, "release", "r", "", "Target Release")
54+
promoteCmd.PersistentFlags().StringVarP(
55+
&promoteOpts.Release,
56+
"release",
57+
"r",
58+
"",
59+
"Target Release",
60+
)
4361

44-
addRepoPathFlag(f, &opts.CommonArgs)
62+
rootCmd.AddCommand(promoteCmd)
63+
}
64+
65+
func runPromote(opts kepctl.PromoteOpts) error {
66+
k, err := kepctl.New(opts.RepoPath)
67+
if err != nil {
68+
return errors.Wrap(err, "creating kepctl client")
69+
}
4570

46-
return cmd
71+
return k.Promote(&opts)
4772
}

cmd/kepctl/cmd/query.go

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,98 @@ package cmd
1919
import (
2020
"fmt"
2121

22+
"github.com/pkg/errors"
2223
"github.com/spf13/cobra"
2324

2425
"k8s.io/enhancements/pkg/kepctl"
2526
)
2627

27-
func buildQueryCommand(k *kepctl.Client) *cobra.Command {
28-
opts := kepctl.QueryOpts{}
29-
cmd := &cobra.Command{
30-
Use: "query",
31-
Short: "Query KEPs",
32-
Long: "Query the local filesystem, and optionally GitHub PRs for KEPs",
33-
Example: ` kepctl query --sig architecture --status provisional --include-prs`,
34-
PreRunE: func(cmd *cobra.Command, args []string) error {
35-
return opts.Validate()
36-
},
37-
RunE: func(cmd *cobra.Command, args []string) error {
38-
return k.Query(&opts)
39-
},
40-
}
28+
// TODO: Struct literal instead?
29+
var queryOpts = kepctl.QueryOpts{}
30+
31+
var queryCmd = &cobra.Command{
32+
Use: "query",
33+
Short: "Query KEPs",
34+
Long: "Query the local filesystem, and optionally GitHub PRs for KEPs",
35+
Example: ` kepctl query --sig architecture --status provisional --include-prs`,
36+
SilenceUsage: true,
37+
SilenceErrors: true,
38+
PreRunE: func(*cobra.Command, []string) error {
39+
return queryOpts.Validate()
40+
},
41+
RunE: func(*cobra.Command, []string) error {
42+
return runQuery(queryOpts)
43+
},
44+
}
45+
46+
func init() {
47+
// TODO: Should these all be global args?
48+
queryCmd.PersistentFlags().StringSliceVar(
49+
&queryOpts.SIG,
50+
"sig",
51+
nil,
52+
"SIG. If not specified, KEPs from all SIGs are shown.",
53+
)
54+
55+
queryCmd.PersistentFlags().StringSliceVar(
56+
&queryOpts.Status,
57+
"status",
58+
nil,
59+
"Status",
60+
)
61+
62+
queryCmd.PersistentFlags().StringSliceVar(
63+
&queryOpts.Stage,
64+
"stage",
65+
nil,
66+
"Stage",
67+
)
68+
69+
queryCmd.PersistentFlags().StringSliceVar(
70+
&queryOpts.PRRApprover,
71+
"prr",
72+
nil,
73+
"Prod Readiness Approver",
74+
)
4175

42-
f := cmd.Flags()
43-
f.StringSliceVar(&opts.SIG, "sig", nil, "SIG. If not specified, KEPs from all SIGs are shown.")
44-
f.StringSliceVar(&opts.Status, "status", nil, "Status")
45-
f.StringSliceVar(&opts.Stage, "stage", nil, "Stage")
46-
f.StringSliceVar(&opts.PRRApprover, "prr", nil, "Prod Readiness Approver")
47-
f.StringSliceVar(&opts.Approver, "approver", nil, "Approver")
48-
f.StringSliceVar(&opts.Author, "author", nil, "Author")
49-
f.BoolVar(&opts.IncludePRs, "include-prs", false, "Include PRs in the results")
50-
f.StringVar(&opts.Output, "output", kepctl.DefaultOutputOpt, fmt.Sprintf("Output format. Can be %v", kepctl.SupportedOutputOpts))
76+
queryCmd.PersistentFlags().StringSliceVar(
77+
&queryOpts.Approver,
78+
"approver",
79+
nil,
80+
"Approver",
81+
)
5182

52-
addRepoPathFlag(f, &opts.CommonArgs)
83+
queryCmd.PersistentFlags().StringSliceVar(
84+
&queryOpts.Author,
85+
"author",
86+
nil,
87+
"Author",
88+
)
89+
90+
queryCmd.PersistentFlags().BoolVar(
91+
&queryOpts.IncludePRs,
92+
"include-prs",
93+
false,
94+
"Include PRs in the results",
95+
)
96+
97+
queryCmd.PersistentFlags().StringVar(
98+
&queryOpts.Output,
99+
"output",
100+
kepctl.DefaultOutputOpt,
101+
fmt.Sprintf(
102+
"Output format. Can be %v", kepctl.SupportedOutputOpts,
103+
),
104+
)
105+
106+
rootCmd.AddCommand(queryCmd)
107+
}
108+
109+
func runQuery(queryOpts kepctl.QueryOpts) error {
110+
k, err := kepctl.New(queryOpts.RepoPath)
111+
if err != nil {
112+
return errors.Wrap(err, "creating kepctl client")
113+
}
53114

54-
return cmd
115+
return k.Query(&queryOpts)
55116
}

pkg/kepctl/query.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ func (c *QueryOpts) Validate() error {
6363
if err != nil {
6464
return err
6565
}
66+
6667
if len(sigs) == 0 {
6768
return fmt.Errorf("no SIG matches any of the passed regular expressions")
6869
}
70+
6971
c.SIG = sigs
7072
} else {
7173
// if no SIGs are passed, list KEPs from all SIGs

0 commit comments

Comments
 (0)