Files
jira/jiracmd/request.go
T
Mike Pountney 27f57b2bbe Switch over to using github.com/go-jira/jira, from gopkg.in
There should be no reason to use gopkg.in versioned imports now that
we're using go modules. I think, IANAE.

gopkg.in kind of gets in the way of modules, as it only pulls over
tagged releases from github.com -- this then means that you need to use
go modules 'replace' syntax in the go.mod to use a non-versioned commit
or branch. This is feasible, but kind of ugly.

go modules defaults to pulling the latest version, so the default
behavior is the same as when pulling go-jira.v1 from gopkg.in.
2019-09-14 21:31:11 -07:00

83 lines
2.3 KiB
Go

package jiracmd
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/coryb/figtree"
"github.com/coryb/oreo"
"github.com/go-jira/jira/jiracli"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type RequestOptions struct {
jiracli.CommonOptions `yaml:",inline" json:",inline" figtree:",inline"`
Method string `yaml:"method,omitempty" json:"method,omitempty"`
URI string `yaml:"uri,omitempty" json:"uri,omitempty"`
Data string `yaml:"data,omitempty" json:"data,omitempty"`
}
func CmdRequestRegistry() *jiracli.CommandRegistryEntry {
opts := RequestOptions{
CommonOptions: jiracli.CommonOptions{
Template: figtree.NewStringOption("request"),
},
}
return &jiracli.CommandRegistryEntry{
"Open issue in requestr",
func(fig *figtree.FigTree, cmd *kingpin.CmdClause) error {
jiracli.LoadConfigs(cmd, fig, &opts)
jiracli.TemplateUsage(cmd, &opts.CommonOptions)
jiracli.GJsonQueryUsage(cmd, &opts.CommonOptions)
return CmdRequestUsage(cmd, &opts)
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
if opts.Method == "" {
opts.Method = "GET"
}
return CmdRequest(o, globals, &opts)
},
}
}
func CmdRequestUsage(cmd *kingpin.CmdClause, opts *RequestOptions) error {
cmd.Flag("method", "HTTP request method to use").Short('M').EnumVar(&opts.Method, "GET", "PUT", "POST", "DELETE")
cmd.Arg("API", "Path to Jira API (ie: /rest/api/2/issue)").Required().StringVar(&opts.URI)
cmd.Arg("JSON", "JSON Content to send to API").StringVar(&opts.Data)
return nil
}
// CmdRequest open the default system requestr to the provided issue
func CmdRequest(o *oreo.Client, globals *jiracli.GlobalOptions, opts *RequestOptions) error {
uri := opts.URI
if !strings.HasPrefix(uri, "http") {
uri = globals.Endpoint.Value + uri
}
parsedURI, err := url.Parse(uri)
if err != nil {
return err
}
builder := oreo.RequestBuilder(parsedURI).WithMethod(opts.Method)
if opts.Data != "" {
builder = builder.WithJSON(opts.Data)
}
resp, err := o.Do(builder.Build())
if err != nil {
return err
}
defer resp.Body.Close()
var data interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return fmt.Errorf("JSON Parse Error: %v", err)
}
return opts.PrintTemplate(&data)
}