Change JSON Exporter to an interface
This commit is contained in:
parent
e63904bacd
commit
3ad41e3e65
7 changed files with 63 additions and 52 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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, " ")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue