Skip to content

Commit

Permalink
add --jq option to run a json query against Jira service response json
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Sep 10, 2017
1 parent e0e1e5b commit 1d26918
Show file tree
Hide file tree
Showing 50 changed files with 2,476 additions and 30 deletions.
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ import:
- package: gopkg.in/coryb/yaml.v2
- package: gopkg.in/op/go-logging.v1
version: ^1.0.0
- package: github.com/savaki/jq
38 changes: 34 additions & 4 deletions jiracli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/coryb/oreo"
"github.com/jinzhu/copier"
shellquote "github.com/kballard/go-shellquote"
"github.com/savaki/jq"
"gopkg.in/AlecAivazis/survey.v1"
kingpin "gopkg.in/alecthomas/kingpin.v2"
yaml "gopkg.in/coryb/yaml.v2"
Expand All @@ -38,10 +39,12 @@ type GlobalOptions struct {
}

type CommonOptions struct {
Browse figtree.BoolOption `yaml:"browse,omitempty" json:"browse,omitempty"`
Editor figtree.StringOption `yaml:"editor,omitempty" json:"editor,omitempty"`
SkipEditing figtree.BoolOption `yaml:"noedit,omitempty" json:"noedit,omitempty"`
Template figtree.StringOption `yaml:"template,omitempty" json:"template,omitempty"`
Browse figtree.BoolOption `yaml:"browse,omitempty" json:"browse,omitempty"`
Editor figtree.StringOption `yaml:"editor,omitempty" json:"editor,omitempty"`
JsonQuery figtree.StringOption `yaml:"jq,omitempty" json:"jq,omitempty"`
JsonQueryRaw figtree.BoolOption `yaml:"jq-raw,omitempty" json":jq-raw,omitempty"`
SkipEditing figtree.BoolOption `yaml:"noedit,omitempty" json:"noedit,omitempty"`
Template figtree.StringOption `yaml:"template,omitempty" json:"template,omitempty"`
}

type CommandRegistryEntry struct {
Expand Down Expand Up @@ -169,6 +172,33 @@ func TemplateUsage(cmd *kingpin.CmdClause, opts *CommonOptions) {
cmd.Flag("template", "Template to use for output").Short('t').SetValue(&opts.Template)
}

func JsonQueryUsage(cmd *kingpin.CmdClause, opts *CommonOptions) {
cmd.Flag("jq", "JSON Query to filter output").SetValue(&opts.JsonQuery)
cmd.Flag("raw", "Return unquoted raw data from JSON Query").Hidden().SetValue(&opts.JsonQueryRaw)
}

func (o *CommonOptions) PrintTemplate(data interface{}) error {
if o.JsonQuery.Value != "" {
buf := bytes.NewBufferString("")
RunTemplate("json", data, buf)
op, err := jq.Parse(o.JsonQuery.Value)
if err != nil {
return err
}
value, err := op.Apply(buf.Bytes())
if err != nil {
return err
}
if o.JsonQueryRaw.Value {
value = []byte(strings.TrimPrefix(strings.TrimSuffix(string(value), "\""), "\""))
}
_, err = os.Stdout.Write(value)
os.Stdout.Write([]byte{'\n'})
return err
}
return RunTemplate(o.Template.Value, data, nil)
}

func (o *CommonOptions) editFile(fileName string) (changes bool, err error) {
var editor string
for _, ed := range []string{o.Editor.Value, os.Getenv("JIRA_EDITOR"), os.Getenv("EDITOR"), "vim"} {
Expand Down
25 changes: 13 additions & 12 deletions jiracli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,26 @@ func RunTemplate(templateName string, data interface{}, out io.Writer) error {
}

var AllTemplates = map[string]string{
"comment": defaultCommentTemplate,
"component-add": defaultComponentAddTemplate,
"components": defaultComponentsTemplate,
"create": defaultCreateTemplate,
"createmeta": defaultDebugTemplate,
"debug": defaultDebugTemplate,
"fields": defaultDebugTemplate,
"edit": defaultEditTemplate,
"editmeta": defaultDebugTemplate,
"transmeta": defaultDebugTemplate,
"createmeta": defaultDebugTemplate,
"fields": defaultDebugTemplate,
"issuelinktypes": defaultDebugTemplate,
"list": defaultListTemplate,
"table": defaultTableTemplate,
"view": defaultViewTemplate,
"edit": defaultEditTemplate,
"transitions": defaultTransitionsTemplate,
"components": defaultComponentsTemplate,
"issuetypes": defaultIssuetypesTemplate,
"create": defaultCreateTemplate,
"json": defaultDebugTemplate,
"list": defaultListTemplate,
"request": defaultDebugTemplate,
"subtask": defaultSubtaskTemplate,
"comment": defaultCommentTemplate,
"table": defaultTableTemplate,
"transition": defaultTransitionTemplate,
"request": defaultDebugTemplate,
"transitions": defaultTransitionsTemplate,
"transmeta": defaultDebugTemplate,
"view": defaultViewTemplate,
"worklog": defaultWorklogTemplate,
"worklogs": defaultWorklogsTemplate,
}
Expand Down
3 changes: 2 additions & 1 deletion jiracmd/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func CmdComponentsRegistry() *jiracli.CommandRegistryEntry {

func CmdComponentsUsage(cmd *kingpin.CmdClause, opts *ComponentsOptions) error {
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Flag("project", "project to list components").Short('p').StringVar(&opts.Project)

return nil
Expand All @@ -51,5 +52,5 @@ func CmdComponents(o *oreo.Client, globals *jiracli.GlobalOptions, opts *Compone
if err != nil {
return err
}
return jiracli.RunTemplate(opts.Template.Value, data, nil)
return opts.PrintTemplate(data)
}
3 changes: 2 additions & 1 deletion jiracmd/createmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func CmdCreateMetaRegistry() *jiracli.CommandRegistryEntry {

func CmdCreateMetaUsage(cmd *kingpin.CmdClause, opts *CreateMetaOptions) error {
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Flag("project", "project to fetch create metadata").Short('p').StringVar(&opts.Project)
cmd.Flag("issuetype", "issuetype in project to fetch create metadata").Short('i').StringVar(&opts.IssueType)
return nil
Expand All @@ -49,5 +50,5 @@ func CmdCreateMeta(o *oreo.Client, globals *jiracli.GlobalOptions, opts *CreateM
if err != nil {
return err
}
return jiracli.RunTemplate(opts.Template.Value, createMeta, nil)
return opts.PrintTemplate(createMeta)
}
3 changes: 2 additions & 1 deletion jiracmd/editmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func CmdEditMetaRegistry() *jiracli.CommandRegistryEntry {
func CmdEditMetaUsage(cmd *kingpin.CmdClause, opts *EditMetaOptions) error {
jiracli.BrowseUsage(cmd, &opts.CommonOptions)
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Arg("ISSUE", "edit metadata for issue id").Required().StringVar(&opts.Issue)
return nil
}
Expand All @@ -46,7 +47,7 @@ func CmdEditMeta(o *oreo.Client, globals *jiracli.GlobalOptions, opts *EditMetaO
if err != nil {
return err
}
if err := jiracli.RunTemplate(opts.Template.Value, editMeta, nil); err != nil {
if err := opts.PrintTemplate(editMeta); err != nil {
return err
}
if opts.Browse.Value {
Expand Down
3 changes: 2 additions & 1 deletion jiracmd/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func CmdFieldsRegistry() *jiracli.CommandRegistryEntry {
func(fig *figtree.FigTree, cmd *kingpin.CmdClause) error {
jiracli.LoadConfigs(cmd, fig, &opts)
jiracli.TemplateUsage(cmd, &opts)
jiracli.JsonQueryUsage(cmd, &opts)
return nil
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
Expand All @@ -31,5 +32,5 @@ func CmdFields(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.Com
if err != nil {
return err
}
return jiracli.RunTemplate(opts.Template.Value, data, nil)
return opts.PrintTemplate(data)
}
3 changes: 2 additions & 1 deletion jiracmd/issuelinktypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func CmdIssueLinkTypesRegistry() *jiracli.CommandRegistryEntry {

func CmdIssueLinkTypesUsage(cmd *kingpin.CmdClause, opts *jiracli.CommonOptions) error {
jiracli.TemplateUsage(cmd, opts)
jiracli.JsonQueryUsage(cmd, opts)
return nil
}

Expand All @@ -36,5 +37,5 @@ func CmdIssueLinkTypes(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jir
if err != nil {
return err
}
return jiracli.RunTemplate(opts.Template.Value, data, nil)
return opts.PrintTemplate(data)
}
3 changes: 2 additions & 1 deletion jiracmd/issuetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func CmdIssueTypesRegistry() *jiracli.CommandRegistryEntry {

func CmdIssueTypesUsage(cmd *kingpin.CmdClause, opts *IssueTypesOptions) error {
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Flag("project", "project to list issueTypes").Short('p').StringVar(&opts.Project)

return nil
Expand All @@ -51,5 +52,5 @@ func CmdIssueTypes(o *oreo.Client, globals *jiracli.GlobalOptions, opts *IssueTy
if err != nil {
return err
}
return jiracli.RunTemplate(opts.Template.Value, data, nil)
return opts.PrintTemplate(data)
}
3 changes: 2 additions & 1 deletion jiracmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func CmdListRegistry() *jiracli.CommandRegistryEntry {

func CmdListUsage(cmd *kingpin.CmdClause, opts *ListOptions) error {
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Flag("assignee", "User assigned the issue").Short('a').StringVar(&opts.Assignee)
cmd.Flag("component", "Component to search for").Short('c').StringVar(&opts.Component)
cmd.Flag("issuetype", "Issue type to search for").Short('i').StringVar(&opts.IssueType)
Expand All @@ -62,5 +63,5 @@ func CmdList(o *oreo.Client, globals *jiracli.GlobalOptions, opts *ListOptions)
if err != nil {
return err
}
return jiracli.RunTemplate(opts.Template.Value, data, nil)
return opts.PrintTemplate(data)
}
4 changes: 3 additions & 1 deletion jiracmd/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func CmdRequestRegistry() *jiracli.CommandRegistryEntry {
if opts.Method == "" {
opts.Method = "GET"
}
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
return CmdRequestUsage(cmd, &opts)
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
Expand Down Expand Up @@ -89,5 +91,5 @@ func CmdRequest(o *oreo.Client, globals *jiracli.GlobalOptions, opts *RequestOpt
return fmt.Errorf("JSON Parse Error: %s from %q", err, content)
}

return jiracli.RunTemplate(opts.Template.Value, &data, nil)
return opts.PrintTemplate(&data)
}
3 changes: 2 additions & 1 deletion jiracmd/transitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func CmdTransitionsRegistry(defaultTemplate string) *jiracli.CommandRegistryEntr
func CmdTransitionsUsage(cmd *kingpin.CmdClause, opts *TransitionsOptions) error {
jiracli.BrowseUsage(cmd, &opts.CommonOptions)
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Arg("ISSUE", "issue to list valid transitions").Required().StringVar(&opts.Issue)
return nil
}
Expand All @@ -45,7 +46,7 @@ func CmdTransitions(o *oreo.Client, globals *jiracli.GlobalOptions, opts *Transi
if err != nil {
return err
}
if err := jiracli.RunTemplate(opts.Template.Value, editMeta, nil); err != nil {
if err := opts.PrintTemplate(editMeta); err != nil {
return err
}
if opts.Browse.Value {
Expand Down
3 changes: 2 additions & 1 deletion jiracmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func CmdViewRegistry() *jiracli.CommandRegistryEntry {
func CmdViewUsage(cmd *kingpin.CmdClause, opts *ViewOptions) error {
jiracli.BrowseUsage(cmd, &opts.CommonOptions)
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Flag("expand", "field to expand for the issue").StringsVar(&opts.Expand)
cmd.Flag("field", "field to return for the issue").StringsVar(&opts.Fields)
cmd.Flag("property", "property to return for issue").StringsVar(&opts.Properties)
Expand All @@ -49,7 +50,7 @@ func CmdView(o *oreo.Client, globals *jiracli.GlobalOptions, opts *ViewOptions)
if err != nil {
return err
}
if err := jiracli.RunTemplate(opts.Template.Value, data, nil); err != nil {
if err := opts.PrintTemplate(data); err != nil {
return err
}
if opts.Browse.Value {
Expand Down
5 changes: 3 additions & 2 deletions jiracmd/worklogList.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func CmdWorklogListRegistry() *jiracli.CommandRegistryEntry {
func CmdWorklogListUsage(cmd *kingpin.CmdClause, opts *WorklogListOptions) error {
jiracli.BrowseUsage(cmd, &opts.CommonOptions)
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.JsonQueryUsage(cmd, &opts.CommonOptions)
cmd.Arg("ISSUE", "issue id to fetch worklogs").Required().StringVar(&opts.Issue)
return nil
}
Expand All @@ -45,9 +46,9 @@ func CmdWorklogList(o *oreo.Client, globals *jiracli.GlobalOptions, opts *Worklo
if err != nil {
return err
}
if err := jiracli.RunTemplate(opts.Template.Value, struct {
if err := opts.PrintTemplate(struct {
Worklogs *jiradata.Worklogs `json:"worklogs,omitempty" yaml:"worklogs,omitempty"`
}{data}, nil); err != nil {
}{data}); err != nil {
return err
}
if opts.Browse.Value {
Expand Down
33 changes: 33 additions & 0 deletions vendor/github.com/savaki/jq/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d26918

Please sign in to comment.