mirror of
https://github.com/Threnklyn/jira.git
synced 2026-06-06 21:20:48 +02:00
add --jq option to run a json query against Jira service response json
This commit is contained in:
+34
-4
@@ -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"
|
||||
@@ -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 {
|
||||
@@ -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"} {
|
||||
|
||||
Reference in New Issue
Block a user