diff --git a/pkg/cmd/issue/list/list.go b/pkg/cmd/issue/list/list.go index 670380830..ff3aba976 100644 --- a/pkg/cmd/issue/list/list.go +++ b/pkg/cmd/issue/list/list.go @@ -30,8 +30,8 @@ type ListOptions struct { BaseRepo func() (ghrepo.Interface, error) Browser browser - WebMode bool - Export *cmdutil.ExportFormat + WebMode bool + Exporter cmdutil.Exporter Assignee string Labels []string @@ -87,7 +87,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().StringVar(&opts.Mention, "mention", "", "Filter by mention") cmd.Flags().StringVarP(&opts.Milestone, "milestone", "m", "", "Filter by milestone `number` or `title`") cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search issues with `query`") - cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields) return cmd } @@ -139,8 +139,8 @@ func listRun(opts *ListOptions) error { return opts.Browser.Browse(openURL) } - if opts.Export != nil { - filterOptions.Fields = opts.Export.Fields + if opts.Exporter != nil { + filterOptions.Fields = opts.Exporter.Fields() } listResult, err := issueList(httpClient, baseRepo, filterOptions, opts.LimitResults) @@ -154,8 +154,9 @@ func listRun(opts *ListOptions) error { } defer opts.IO.StopPager() - if opts.Export != nil { - return opts.Export.Write(opts.IO.Out, api.ExportIssues(listResult.Issues, opts.Export.Fields), opts.IO.ColorEnabled()) + if opts.Exporter != nil { + data := api.ExportIssues(listResult.Issues, opts.Exporter.Fields()) + return opts.Exporter.Write(opts.IO.Out, data, opts.IO.ColorEnabled()) } if isTerminal { diff --git a/pkg/cmd/issue/status/status.go b/pkg/cmd/issue/status/status.go index 60567c6f3..764ec52fa 100644 --- a/pkg/cmd/issue/status/status.go +++ b/pkg/cmd/issue/status/status.go @@ -20,7 +20,7 @@ type StatusOptions struct { IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) - Export *cmdutil.ExportFormat + Exporter cmdutil.Exporter } 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 }, } - cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields) return cmd } @@ -80,8 +80,8 @@ func statusRun(opts *StatusOptions) error { Username: currentUser, Fields: defaultFields, } - if opts.Export != nil { - options.Fields = opts.Export.Fields + if opts.Exporter != nil { + options.Fields = opts.Exporter.Fields() } issuePayload, err := api.IssueStatus(apiClient, baseRepo, options) if err != nil { @@ -94,13 +94,13 @@ func statusRun(opts *StatusOptions) error { } defer opts.IO.StopPager() - if opts.Export != nil { + if opts.Exporter != nil { data := map[string]interface{}{ - "createdBy": api.ExportIssues(issuePayload.Authored.Issues, opts.Export.Fields), - "assigned": api.ExportIssues(issuePayload.Assigned.Issues, opts.Export.Fields), - "mentioned": api.ExportIssues(issuePayload.Mentioned.Issues, opts.Export.Fields), + "createdBy": api.ExportIssues(issuePayload.Authored.Issues, opts.Exporter.Fields()), + "assigned": api.ExportIssues(issuePayload.Assigned.Issues, opts.Exporter.Fields()), + "mentioned": api.ExportIssues(issuePayload.Mentioned.Issues, opts.Exporter.Fields()), } - return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled()) + return opts.Exporter.Write(opts.IO.Out, &data, opts.IO.ColorEnabled()) } out := opts.IO.Out diff --git a/pkg/cmd/issue/view/view.go b/pkg/cmd/issue/view/view.go index 509787b87..03a9eb848 100644 --- a/pkg/cmd/issue/view/view.go +++ b/pkg/cmd/issue/view/view.go @@ -33,7 +33,7 @@ type ViewOptions struct { SelectorArg string WebMode bool Comments bool - Export *cmdutil.ExportFormat + Exporter cmdutil.Exporter Now func() time.Time } @@ -72,7 +72,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open an issue in the browser") cmd.Flags().BoolVarP(&opts.Comments, "comments", "c", false, "View issue comments") - cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields) return cmd } @@ -115,9 +115,9 @@ func viewRun(opts *ViewOptions) error { } defer opts.IO.StopPager() - if opts.Export != nil { - exportIssue := issue.ExportData(opts.Export.Fields) - return opts.Export.Write(opts.IO.Out, exportIssue, opts.IO.ColorEnabled()) + if opts.Exporter != nil { + exportIssue := issue.ExportData(opts.Exporter.Fields()) + return opts.Exporter.Write(opts.IO.Out, exportIssue, opts.IO.ColorEnabled()) } if opts.IO.IsStdoutTTY() { diff --git a/pkg/cmd/pr/list/list.go b/pkg/cmd/pr/list/list.go index b15acc324..de58d5ad2 100644 --- a/pkg/cmd/pr/list/list.go +++ b/pkg/cmd/pr/list/list.go @@ -29,7 +29,7 @@ type ListOptions struct { WebMode bool LimitResults int - Export *cmdutil.ExportFormat + Exporter cmdutil.Exporter State string BaseBranch string @@ -78,7 +78,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author") cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee") cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search pull requests with `query`") - cmdutil.AddJSONFlags(cmd, &opts.Export, api.PullRequestFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields) return cmd } @@ -115,8 +115,8 @@ func listRun(opts *ListOptions) error { Search: opts.Search, Fields: defaultFields, } - if opts.Export != nil { - filters.Fields = opts.Export.Fields + if opts.Exporter != nil { + filters.Fields = opts.Exporter.Fields() } if opts.WebMode { @@ -143,8 +143,9 @@ func listRun(opts *ListOptions) error { } defer opts.IO.StopPager() - if opts.Export != nil { - return opts.Export.Write(opts.IO.Out, api.ExportPRs(listResult.PullRequests, opts.Export.Fields), opts.IO.ColorEnabled()) + if opts.Exporter != nil { + data := api.ExportPRs(listResult.PullRequests, opts.Exporter.Fields()) + return opts.Exporter.Write(opts.IO.Out, data, opts.IO.ColorEnabled()) } if opts.IO.IsStdoutTTY() { diff --git a/pkg/cmd/pr/status/status.go b/pkg/cmd/pr/status/status.go index 7bdd9c11b..115ee035b 100644 --- a/pkg/cmd/pr/status/status.go +++ b/pkg/cmd/pr/status/status.go @@ -29,7 +29,7 @@ type StatusOptions struct { Branch func() (string, error) HasRepoOverride bool - Export *cmdutil.ExportFormat + Exporter cmdutil.Exporter } 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 }, } - cmdutil.AddJSONFlags(cmd, &opts.Export, api.PullRequestFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields) return cmd } @@ -96,8 +96,8 @@ func statusRun(opts *StatusOptions) error { CurrentPR: currentPRNumber, HeadRef: currentPRHeadRef, } - if opts.Export != nil { - options.Fields = opts.Export.Fields + if opts.Exporter != nil { + options.Fields = opts.Exporter.Fields() } prPayload, err := api.PullRequestStatus(apiClient, baseRepo, options) if err != nil { @@ -110,16 +110,16 @@ func statusRun(opts *StatusOptions) error { } defer opts.IO.StopPager() - if opts.Export != nil { + if opts.Exporter != nil { data := map[string]interface{}{ "currentBranch": nil, - "createdBy": api.ExportPRs(prPayload.ViewerCreated.PullRequests, opts.Export.Fields), - "needsReview": api.ExportPRs(prPayload.ReviewRequested.PullRequests, opts.Export.Fields), + "createdBy": api.ExportPRs(prPayload.ViewerCreated.PullRequests, opts.Exporter.Fields()), + "needsReview": api.ExportPRs(prPayload.ReviewRequested.PullRequests, opts.Exporter.Fields()), } if prPayload.CurrentPR != nil { - data["currentBranch"] = prPayload.CurrentPR.ExportData(opts.Export.Fields) + data["currentBranch"] = prPayload.CurrentPR.ExportData(opts.Exporter.Fields()) } - return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled()) + return opts.Exporter.Write(opts.IO.Out, &data, opts.IO.ColorEnabled()) } out := opts.IO.Out diff --git a/pkg/cmd/pr/view/view.go b/pkg/cmd/pr/view/view.go index d4f897b83..19ebbe756 100644 --- a/pkg/cmd/pr/view/view.go +++ b/pkg/cmd/pr/view/view.go @@ -35,7 +35,7 @@ type ViewOptions struct { Remotes func() (context.Remotes, error) Branch func() (string, error) - Export *cmdutil.ExportFormat + Exporter cmdutil.Exporter SelectorArg string BrowserMode bool @@ -85,7 +85,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman cmd.Flags().BoolVarP(&opts.BrowserMode, "web", "w", false, "Open a pull request in the browser") cmd.Flags().BoolVarP(&opts.Comments, "comments", "c", false, "View pull request comments") - cmdutil.AddJSONFlags(cmd, &opts.Export, api.PullRequestFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields) return cmd } @@ -116,9 +116,9 @@ func viewRun(opts *ViewOptions) error { } defer opts.IO.StopPager() - if opts.Export != nil { - exportPR := pr.ExportData(opts.Export.Fields) - return opts.Export.Write(opts.IO.Out, exportPR, opts.IO.ColorEnabled()) + if opts.Exporter != nil { + exportPR := pr.ExportData(opts.Exporter.Fields()) + return opts.Exporter.Write(opts.IO.Out, exportPR, opts.IO.ColorEnabled()) } if connectedToTerminal { diff --git a/pkg/cmdutil/json_flags.go b/pkg/cmdutil/json_flags.go index 0aaa64f55..30630fee1 100644 --- a/pkg/cmdutil/json_flags.go +++ b/pkg/cmdutil/json_flags.go @@ -19,7 +19,7 @@ type JSONFlagError struct { error } -func AddJSONFlags(cmd *cobra.Command, exportTarget **ExportFormat, fields []string) { +func AddJSONFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) { f := cmd.Flags() f.StringSlice("json", nil, "Output JSON with the specified `fields`") f.StringP("jq", "q", "", "Filter JSON output using a jq `expression`") @@ -62,9 +62,9 @@ func checkJSONFlags(cmd *cobra.Command) (*ExportFormat, error) { } jv := jsonFlag.Value.(pflag.SliceValue) return &ExportFormat{ - Fields: jv.GetSlice(), - Filter: jqFlag.Value.String(), - Template: tplFlag.Value.String(), + fields: jv.GetSlice(), + filter: jqFlag.Value.String(), + template: tplFlag.Value.String(), }, nil } else if jqFlag.Changed { return nil, errors.New("cannot use `--jq` without specifying `--json`") @@ -74,10 +74,19 @@ func checkJSONFlags(cmd *cobra.Command) (*ExportFormat, error) { return nil, nil } +type Exporter interface { + Fields() []string + Write(w io.Writer, data interface{}, colorEnabled bool) error +} + type ExportFormat struct { - Fields []string - Filter string - Template string + fields []string + filter string + template string +} + +func (e *ExportFormat) Fields() []string { + return e.fields } 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 return err } - if e.Filter != "" { - return export.FilterJSON(w, &buf, e.Filter) - } else if e.Template != "" { - return export.ExecuteTemplate(w, &buf, e.Template, colorEnabled) + if e.filter != "" { + return export.FilterJSON(w, &buf, e.filter) + } else if e.template != "" { + return export.ExecuteTemplate(w, &buf, e.template, colorEnabled) } else if colorEnabled { return jsoncolor.Write(w, &buf, " ") }