X Tutup
Skip to content

Commit 3ad41e3

Browse files
committed
Change JSON Exporter to an interface
1 parent e63904b commit 3ad41e3

File tree

7 files changed

+63
-52
lines changed

7 files changed

+63
-52
lines changed

pkg/cmd/issue/list/list.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type ListOptions struct {
3030
BaseRepo func() (ghrepo.Interface, error)
3131
Browser browser
3232

33-
WebMode bool
34-
Export *cmdutil.ExportFormat
33+
WebMode bool
34+
Exporter cmdutil.Exporter
3535

3636
Assignee string
3737
Labels []string
@@ -87,7 +87,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
8787
cmd.Flags().StringVar(&opts.Mention, "mention", "", "Filter by mention")
8888
cmd.Flags().StringVarP(&opts.Milestone, "milestone", "m", "", "Filter by milestone `number` or `title`")
8989
cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search issues with `query`")
90-
cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields)
90+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields)
9191

9292
return cmd
9393
}
@@ -139,8 +139,8 @@ func listRun(opts *ListOptions) error {
139139
return opts.Browser.Browse(openURL)
140140
}
141141

142-
if opts.Export != nil {
143-
filterOptions.Fields = opts.Export.Fields
142+
if opts.Exporter != nil {
143+
filterOptions.Fields = opts.Exporter.Fields()
144144
}
145145

146146
listResult, err := issueList(httpClient, baseRepo, filterOptions, opts.LimitResults)
@@ -154,8 +154,9 @@ func listRun(opts *ListOptions) error {
154154
}
155155
defer opts.IO.StopPager()
156156

157-
if opts.Export != nil {
158-
return opts.Export.Write(opts.IO.Out, api.ExportIssues(listResult.Issues, opts.Export.Fields), opts.IO.ColorEnabled())
157+
if opts.Exporter != nil {
158+
data := api.ExportIssues(listResult.Issues, opts.Exporter.Fields())
159+
return opts.Exporter.Write(opts.IO.Out, data, opts.IO.ColorEnabled())
159160
}
160161

161162
if isTerminal {

pkg/cmd/issue/status/status.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type StatusOptions struct {
2020
IO *iostreams.IOStreams
2121
BaseRepo func() (ghrepo.Interface, error)
2222

23-
Export *cmdutil.ExportFormat
23+
Exporter cmdutil.Exporter
2424
}
2525

2626
func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command {
@@ -45,7 +45,7 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co
4545
},
4646
}
4747

48-
cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields)
48+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields)
4949

5050
return cmd
5151
}
@@ -80,8 +80,8 @@ func statusRun(opts *StatusOptions) error {
8080
Username: currentUser,
8181
Fields: defaultFields,
8282
}
83-
if opts.Export != nil {
84-
options.Fields = opts.Export.Fields
83+
if opts.Exporter != nil {
84+
options.Fields = opts.Exporter.Fields()
8585
}
8686
issuePayload, err := api.IssueStatus(apiClient, baseRepo, options)
8787
if err != nil {
@@ -94,13 +94,13 @@ func statusRun(opts *StatusOptions) error {
9494
}
9595
defer opts.IO.StopPager()
9696

97-
if opts.Export != nil {
97+
if opts.Exporter != nil {
9898
data := map[string]interface{}{
99-
"createdBy": api.ExportIssues(issuePayload.Authored.Issues, opts.Export.Fields),
100-
"assigned": api.ExportIssues(issuePayload.Assigned.Issues, opts.Export.Fields),
101-
"mentioned": api.ExportIssues(issuePayload.Mentioned.Issues, opts.Export.Fields),
99+
"createdBy": api.ExportIssues(issuePayload.Authored.Issues, opts.Exporter.Fields()),
100+
"assigned": api.ExportIssues(issuePayload.Assigned.Issues, opts.Exporter.Fields()),
101+
"mentioned": api.ExportIssues(issuePayload.Mentioned.Issues, opts.Exporter.Fields()),
102102
}
103-
return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
103+
return opts.Exporter.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
104104
}
105105

106106
out := opts.IO.Out

pkg/cmd/issue/view/view.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ViewOptions struct {
3333
SelectorArg string
3434
WebMode bool
3535
Comments bool
36-
Export *cmdutil.ExportFormat
36+
Exporter cmdutil.Exporter
3737

3838
Now func() time.Time
3939
}
@@ -72,7 +72,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
7272

7373
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open an issue in the browser")
7474
cmd.Flags().BoolVarP(&opts.Comments, "comments", "c", false, "View issue comments")
75-
cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields)
75+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields)
7676

7777
return cmd
7878
}
@@ -115,9 +115,9 @@ func viewRun(opts *ViewOptions) error {
115115
}
116116
defer opts.IO.StopPager()
117117

118-
if opts.Export != nil {
119-
exportIssue := issue.ExportData(opts.Export.Fields)
120-
return opts.Export.Write(opts.IO.Out, exportIssue, opts.IO.ColorEnabled())
118+
if opts.Exporter != nil {
119+
exportIssue := issue.ExportData(opts.Exporter.Fields())
120+
return opts.Exporter.Write(opts.IO.Out, exportIssue, opts.IO.ColorEnabled())
121121
}
122122

123123
if opts.IO.IsStdoutTTY() {

pkg/cmd/pr/list/list.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type ListOptions struct {
2929

3030
WebMode bool
3131
LimitResults int
32-
Export *cmdutil.ExportFormat
32+
Exporter cmdutil.Exporter
3333

3434
State string
3535
BaseBranch string
@@ -78,7 +78,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
7878
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
7979
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
8080
cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search pull requests with `query`")
81-
cmdutil.AddJSONFlags(cmd, &opts.Export, api.PullRequestFields)
81+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields)
8282

8383
return cmd
8484
}
@@ -115,8 +115,8 @@ func listRun(opts *ListOptions) error {
115115
Search: opts.Search,
116116
Fields: defaultFields,
117117
}
118-
if opts.Export != nil {
119-
filters.Fields = opts.Export.Fields
118+
if opts.Exporter != nil {
119+
filters.Fields = opts.Exporter.Fields()
120120
}
121121

122122
if opts.WebMode {
@@ -143,8 +143,9 @@ func listRun(opts *ListOptions) error {
143143
}
144144
defer opts.IO.StopPager()
145145

146-
if opts.Export != nil {
147-
return opts.Export.Write(opts.IO.Out, api.ExportPRs(listResult.PullRequests, opts.Export.Fields), opts.IO.ColorEnabled())
146+
if opts.Exporter != nil {
147+
data := api.ExportPRs(listResult.PullRequests, opts.Exporter.Fields())
148+
return opts.Exporter.Write(opts.IO.Out, data, opts.IO.ColorEnabled())
148149
}
149150

150151
if opts.IO.IsStdoutTTY() {

pkg/cmd/pr/status/status.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type StatusOptions struct {
2929
Branch func() (string, error)
3030

3131
HasRepoOverride bool
32-
Export *cmdutil.ExportFormat
32+
Exporter cmdutil.Exporter
3333
}
3434

3535
func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command {
@@ -57,7 +57,7 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co
5757
},
5858
}
5959

60-
cmdutil.AddJSONFlags(cmd, &opts.Export, api.PullRequestFields)
60+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields)
6161

6262
return cmd
6363
}
@@ -96,8 +96,8 @@ func statusRun(opts *StatusOptions) error {
9696
CurrentPR: currentPRNumber,
9797
HeadRef: currentPRHeadRef,
9898
}
99-
if opts.Export != nil {
100-
options.Fields = opts.Export.Fields
99+
if opts.Exporter != nil {
100+
options.Fields = opts.Exporter.Fields()
101101
}
102102
prPayload, err := api.PullRequestStatus(apiClient, baseRepo, options)
103103
if err != nil {
@@ -110,16 +110,16 @@ func statusRun(opts *StatusOptions) error {
110110
}
111111
defer opts.IO.StopPager()
112112

113-
if opts.Export != nil {
113+
if opts.Exporter != nil {
114114
data := map[string]interface{}{
115115
"currentBranch": nil,
116-
"createdBy": api.ExportPRs(prPayload.ViewerCreated.PullRequests, opts.Export.Fields),
117-
"needsReview": api.ExportPRs(prPayload.ReviewRequested.PullRequests, opts.Export.Fields),
116+
"createdBy": api.ExportPRs(prPayload.ViewerCreated.PullRequests, opts.Exporter.Fields()),
117+
"needsReview": api.ExportPRs(prPayload.ReviewRequested.PullRequests, opts.Exporter.Fields()),
118118
}
119119
if prPayload.CurrentPR != nil {
120-
data["currentBranch"] = prPayload.CurrentPR.ExportData(opts.Export.Fields)
120+
data["currentBranch"] = prPayload.CurrentPR.ExportData(opts.Exporter.Fields())
121121
}
122-
return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
122+
return opts.Exporter.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
123123
}
124124

125125
out := opts.IO.Out

pkg/cmd/pr/view/view.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type ViewOptions struct {
3535
Remotes func() (context.Remotes, error)
3636
Branch func() (string, error)
3737

38-
Export *cmdutil.ExportFormat
38+
Exporter cmdutil.Exporter
3939

4040
SelectorArg string
4141
BrowserMode bool
@@ -85,7 +85,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
8585

8686
cmd.Flags().BoolVarP(&opts.BrowserMode, "web", "w", false, "Open a pull request in the browser")
8787
cmd.Flags().BoolVarP(&opts.Comments, "comments", "c", false, "View pull request comments")
88-
cmdutil.AddJSONFlags(cmd, &opts.Export, api.PullRequestFields)
88+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields)
8989

9090
return cmd
9191
}
@@ -116,9 +116,9 @@ func viewRun(opts *ViewOptions) error {
116116
}
117117
defer opts.IO.StopPager()
118118

119-
if opts.Export != nil {
120-
exportPR := pr.ExportData(opts.Export.Fields)
121-
return opts.Export.Write(opts.IO.Out, exportPR, opts.IO.ColorEnabled())
119+
if opts.Exporter != nil {
120+
exportPR := pr.ExportData(opts.Exporter.Fields())
121+
return opts.Exporter.Write(opts.IO.Out, exportPR, opts.IO.ColorEnabled())
122122
}
123123

124124
if connectedToTerminal {

pkg/cmdutil/json_flags.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type JSONFlagError struct {
1919
error
2020
}
2121

22-
func AddJSONFlags(cmd *cobra.Command, exportTarget **ExportFormat, fields []string) {
22+
func AddJSONFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) {
2323
f := cmd.Flags()
2424
f.StringSlice("json", nil, "Output JSON with the specified `fields`")
2525
f.StringP("jq", "q", "", "Filter JSON output using a jq `expression`")
@@ -62,9 +62,9 @@ func checkJSONFlags(cmd *cobra.Command) (*ExportFormat, error) {
6262
}
6363
jv := jsonFlag.Value.(pflag.SliceValue)
6464
return &ExportFormat{
65-
Fields: jv.GetSlice(),
66-
Filter: jqFlag.Value.String(),
67-
Template: tplFlag.Value.String(),
65+
fields: jv.GetSlice(),
66+
filter: jqFlag.Value.String(),
67+
template: tplFlag.Value.String(),
6868
}, nil
6969
} else if jqFlag.Changed {
7070
return nil, errors.New("cannot use `--jq` without specifying `--json`")
@@ -74,10 +74,19 @@ func checkJSONFlags(cmd *cobra.Command) (*ExportFormat, error) {
7474
return nil, nil
7575
}
7676

77+
type Exporter interface {
78+
Fields() []string
79+
Write(w io.Writer, data interface{}, colorEnabled bool) error
80+
}
81+
7782
type ExportFormat struct {
78-
Fields []string
79-
Filter string
80-
Template string
83+
fields []string
84+
filter string
85+
template string
86+
}
87+
88+
func (e *ExportFormat) Fields() []string {
89+
return e.fields
8190
}
8291

8392
func (e *ExportFormat) Write(w io.Writer, data interface{}, colorEnabled bool) error {
@@ -88,10 +97,10 @@ func (e *ExportFormat) Write(w io.Writer, data interface{}, colorEnabled bool) e
8897
return err
8998
}
9099

91-
if e.Filter != "" {
92-
return export.FilterJSON(w, &buf, e.Filter)
93-
} else if e.Template != "" {
94-
return export.ExecuteTemplate(w, &buf, e.Template, colorEnabled)
100+
if e.filter != "" {
101+
return export.FilterJSON(w, &buf, e.filter)
102+
} else if e.template != "" {
103+
return export.ExecuteTemplate(w, &buf, e.template, colorEnabled)
95104
} else if colorEnabled {
96105
return jsoncolor.Write(w, &buf, " ")
97106
}

0 commit comments

Comments
 (0)
X Tutup