Skip to content

Commit 1046342

Browse files
committed
kepctl output: repo.Query returns results only
The query command is now responsible for using pkg/output to display results. This will make it possible to unit test repo.Query
1 parent fb82879 commit 1046342

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

pkg/kepctl/commands/query.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package commands
1818

1919
import (
2020
"fmt"
21+
"os"
2122

2223
"github.com/pkg/errors"
2324
"github.com/spf13/cobra"
@@ -111,5 +112,14 @@ func runQuery(opts *repo.QueryOpts) error {
111112
}
112113
rc.TokenPath = rootOpts.TokenPath
113114

114-
return rc.Query(opts)
115+
results, err := rc.Query(opts)
116+
if err != nil {
117+
return err
118+
}
119+
o, err := output.NewOutput(opts.Output, os.Stdout, os.Stderr)
120+
if err != nil {
121+
return err
122+
}
123+
o.PrintProposals(results)
124+
return nil
115125
}

pkg/repo/query.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package repo
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"regexp"
2322

2423
"github.com/pkg/errors"
@@ -29,13 +28,6 @@ import (
2928
)
3029

3130
type QueryOpts struct {
32-
// Proposal options
33-
KEP string // KEP name sig-xxx/xxx-name
34-
Name string
35-
Number string
36-
SIG string
37-
38-
// Queries
3931
Groups []string
4032
Status []string
4133
Stage []string
@@ -84,18 +76,18 @@ func (r *Repo) PrepareQueryOpts(opts *QueryOpts) error {
8476

8577
// Query searches the local repo and possibly GitHub for KEPs
8678
// that match the search criteria.
87-
func (r *Repo) Query(opts *QueryOpts) error {
79+
func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
8880
logrus.Info("Searching for KEPs...")
8981

9082
err := r.PrepareQueryOpts(opts)
9183
if err != nil {
92-
return fmt.Errorf("unable to prepare query opts: %w", err)
84+
return nil, fmt.Errorf("unable to prepare query opts: %w", err)
9385
}
9486

9587
if r.TokenPath != "" {
9688
logrus.Infof("Setting GitHub token: %v", r.TokenPath)
9789
if tokenErr := r.SetGitHubToken(r.TokenPath); tokenErr != nil {
98-
return errors.Wrapf(tokenErr, "setting GitHub token")
90+
return nil, errors.Wrapf(tokenErr, "setting GitHub token")
9991
}
10092
}
10193

@@ -105,7 +97,7 @@ func (r *Repo) Query(opts *QueryOpts) error {
10597
// KEPs in the local filesystem
10698
localKEPs, err := r.LoadLocalKEPs(sig)
10799
if err != nil {
108-
return errors.Wrap(err, "loading local KEPs")
100+
return nil, errors.Wrap(err, "loading local KEPs")
109101
}
110102

111103
allKEPs = append(allKEPs, localKEPs...)
@@ -134,7 +126,7 @@ func (r *Repo) Query(opts *QueryOpts) error {
134126
results := make([]*api.Proposal, 0, 10)
135127
for _, k := range allKEPs {
136128
if k == nil {
137-
return errors.New("one of the KEPs in query was nil")
129+
return nil, errors.New("one of the KEPs in query was nil")
138130
}
139131

140132
logrus.Debugf("current KEP: %v", k)
@@ -158,13 +150,7 @@ func (r *Repo) Query(opts *QueryOpts) error {
158150
results = append(results, k)
159151
}
160152

161-
o, err := output.NewOutput(opts.Output, os.Stdout, os.Stderr)
162-
if err != nil {
163-
return err
164-
}
165-
o.PrintProposals(results)
166-
167-
return nil
153+
return results, nil
168154
}
169155

170156
func sliceToMap(s []string) map[string]bool {

0 commit comments

Comments
 (0)