Skip to content

Commit

Permalink
refactor to allow for --insecure and --unixproxy arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Sep 5, 2017
1 parent 21920c5 commit c0358eb
Show file tree
Hide file tree
Showing 38 changed files with 146 additions and 120 deletions.
88 changes: 44 additions & 44 deletions cmd/jira/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,178 +107,178 @@ func main() {
registry := []jiracli.CommandRegistry{
jiracli.CommandRegistry{
Command: "login",
Entry: jiracmd.CmdLoginRegistry(o),
Entry: jiracmd.CmdLoginRegistry(),
},
jiracli.CommandRegistry{
Command: "logout",
Entry: jiracmd.CmdLogoutRegistry(o),
Entry: jiracmd.CmdLogoutRegistry(),
},
jiracli.CommandRegistry{
Command: "list",
Aliases: []string{"ls"},
Entry: jiracmd.CmdListRegistry(o),
Entry: jiracmd.CmdListRegistry(),
},
jiracli.CommandRegistry{
Command: "view",
Entry: jiracmd.CmdViewRegistry(o),
Entry: jiracmd.CmdViewRegistry(),
},
jiracli.CommandRegistry{
Command: "create",
Entry: jiracmd.CmdCreateRegistry(o),
Entry: jiracmd.CmdCreateRegistry(),
},
jiracli.CommandRegistry{
Command: "edit",
Entry: jiracmd.CmdEditRegistry(o),
Entry: jiracmd.CmdEditRegistry(),
},
jiracli.CommandRegistry{
Command: "comment",
Entry: jiracmd.CmdCommentRegistry(o),
Entry: jiracmd.CmdCommentRegistry(),
},
jiracli.CommandRegistry{
Command: "worklog list",
Entry: jiracmd.CmdWorklogListRegistry(o),
Entry: jiracmd.CmdWorklogListRegistry(),
Default: true,
},
jiracli.CommandRegistry{
Command: "worklog add",
Entry: jiracmd.CmdWorklogAddRegistry(o),
Entry: jiracmd.CmdWorklogAddRegistry(),
},
jiracli.CommandRegistry{
Command: "fields",
Entry: jiracmd.CmdFieldsRegistry(o),
Entry: jiracmd.CmdFieldsRegistry(),
},
jiracli.CommandRegistry{
Command: "createmeta",
Entry: jiracmd.CmdCreateMetaRegistry(o),
Entry: jiracmd.CmdCreateMetaRegistry(),
},
jiracli.CommandRegistry{
Command: "editmeta",
Entry: jiracmd.CmdEditMetaRegistry(o),
Entry: jiracmd.CmdEditMetaRegistry(),
},
jiracli.CommandRegistry{
Command: "subtask",
Entry: jiracmd.CmdSubtaskRegistry(o),
Entry: jiracmd.CmdSubtaskRegistry(),
},
jiracli.CommandRegistry{
Command: "dup",
Entry: jiracmd.CmdDupRegistry(o),
Entry: jiracmd.CmdDupRegistry(),
},
jiracli.CommandRegistry{
Command: "block",
Entry: jiracmd.CmdBlockRegistry(o),
Entry: jiracmd.CmdBlockRegistry(),
},
jiracli.CommandRegistry{
Command: "issuelink",
Entry: jiracmd.CmdIssueLinkRegistry(o),
Entry: jiracmd.CmdIssueLinkRegistry(),
},
jiracli.CommandRegistry{
Command: "issuelinktypes",
Entry: jiracmd.CmdIssueLinkTypesRegistry(o),
Entry: jiracmd.CmdIssueLinkTypesRegistry(),
},
jiracli.CommandRegistry{
Command: "transition",
Aliases: []string{"trans"},
Entry: jiracmd.CmdTransitionRegistry(o, ""),
Entry: jiracmd.CmdTransitionRegistry(""),
},
jiracli.CommandRegistry{
Command: "transitions",
Entry: jiracmd.CmdTransitionsRegistry(o, "transitions"),
Entry: jiracmd.CmdTransitionsRegistry("transitions"),
},
jiracli.CommandRegistry{
Command: "transmeta",
Entry: jiracmd.CmdTransitionsRegistry(o, "debug"),
Entry: jiracmd.CmdTransitionsRegistry("debug"),
},
jiracli.CommandRegistry{
Command: "close",
Entry: jiracmd.CmdTransitionRegistry(o, "close"),
Entry: jiracmd.CmdTransitionRegistry("close"),
},
jiracli.CommandRegistry{
Command: "acknowledge",
Aliases: []string{"ack"},
Entry: jiracmd.CmdTransitionRegistry(o, "acknowledge"),
Entry: jiracmd.CmdTransitionRegistry("acknowledge"),
},
jiracli.CommandRegistry{
Command: "reopen",
Entry: jiracmd.CmdTransitionRegistry(o, "reopen"),
Entry: jiracmd.CmdTransitionRegistry("reopen"),
},
jiracli.CommandRegistry{
Command: "resolve",
Entry: jiracmd.CmdTransitionRegistry(o, "resolve"),
Entry: jiracmd.CmdTransitionRegistry("resolve"),
},
jiracli.CommandRegistry{
Command: "start",
Entry: jiracmd.CmdTransitionRegistry(o, "start"),
Entry: jiracmd.CmdTransitionRegistry("start"),
},
jiracli.CommandRegistry{
Command: "stop",
Entry: jiracmd.CmdTransitionRegistry(o, "stop"),
Entry: jiracmd.CmdTransitionRegistry("stop"),
},
jiracli.CommandRegistry{
Command: "todo",
Entry: jiracmd.CmdTransitionRegistry(o, "To Do"),
Entry: jiracmd.CmdTransitionRegistry("To Do"),
},
jiracli.CommandRegistry{
Command: "backlog",
Entry: jiracmd.CmdTransitionRegistry(o, "Backlog"),
Entry: jiracmd.CmdTransitionRegistry("Backlog"),
},
jiracli.CommandRegistry{
Command: "done",
Entry: jiracmd.CmdTransitionRegistry(o, "Done"),
Entry: jiracmd.CmdTransitionRegistry("Done"),
},
jiracli.CommandRegistry{
Command: "in-progress",
Aliases: []string{"prog", "progress"},
Entry: jiracmd.CmdTransitionRegistry(o, "Progress"),
Entry: jiracmd.CmdTransitionRegistry("Progress"),
},
jiracli.CommandRegistry{
Command: "vote",
Entry: jiracmd.CmdVoteRegistry(o),
Entry: jiracmd.CmdVoteRegistry(),
},
jiracli.CommandRegistry{
Command: "rank",
Entry: jiracmd.CmdRankRegistry(o),
Entry: jiracmd.CmdRankRegistry(),
},
jiracli.CommandRegistry{
Command: "watch",
Entry: jiracmd.CmdWatchRegistry(o),
Entry: jiracmd.CmdWatchRegistry(),
},
jiracli.CommandRegistry{
Command: "labels add",
Entry: jiracmd.CmdLabelsAddRegistry(o),
Entry: jiracmd.CmdLabelsAddRegistry(),
},
jiracli.CommandRegistry{
Command: "labels set",
Entry: jiracmd.CmdLabelsSetRegistry(o),
Entry: jiracmd.CmdLabelsSetRegistry(),
},
jiracli.CommandRegistry{
Command: "labels remove",
Entry: jiracmd.CmdLabelsRemoveRegistry(o),
Entry: jiracmd.CmdLabelsRemoveRegistry(),
Aliases: []string{"rm"},
},
jiracli.CommandRegistry{
Command: "take",
Entry: jiracmd.CmdTakeRegistry(o),
Entry: jiracmd.CmdTakeRegistry(),
},
jiracli.CommandRegistry{
Command: "assign",
Entry: jiracmd.CmdAssignRegistry(o),
Entry: jiracmd.CmdAssignRegistry(),
Aliases: []string{"give"},
},
jiracli.CommandRegistry{
Command: "unassign",
Entry: jiracmd.CmdUnassignRegistry(o),
Entry: jiracmd.CmdUnassignRegistry(),
},
jiracli.CommandRegistry{
Command: "component add",
Entry: jiracmd.CmdComponentAddRegistry(o),
Entry: jiracmd.CmdComponentAddRegistry(),
},
jiracli.CommandRegistry{
Command: "components",
Entry: jiracmd.CmdComponentsRegistry(o),
Entry: jiracmd.CmdComponentsRegistry(),
},
jiracli.CommandRegistry{
Command: "issuetypes",
Entry: jiracmd.CmdIssueTypesRegistry(o),
Entry: jiracmd.CmdIssueTypesRegistry(),
},
jiracli.CommandRegistry{
Command: "export-templates",
Expand All @@ -295,12 +295,12 @@ func main() {
},
jiracli.CommandRegistry{
Command: "request",
Entry: jiracmd.CmdRequestRegistry(o),
Entry: jiracmd.CmdRequestRegistry(),
Aliases: []string{"req"},
},
}

jiracli.Register(app, fig, registry)
jiracli.Register(app, o, fig, registry)

// register custom commands
data := struct {
Expand Down
29 changes: 26 additions & 3 deletions jiracli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package jiracli

import (
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"reflect"
"strings"

"github.com/AlecAivazis/survey"
"github.com/coryb/figtree"
"github.com/coryb/oreo"
"github.com/jinzhu/copier"
shellquote "github.com/kballard/go-shellquote"
kingpin "gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -29,6 +32,8 @@ type GlobalOptions struct {
Endpoint figtree.StringOption `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
User figtree.StringOption `json:"user,omitempty" yaml:"user,omitempty"`
PasswordSource figtree.StringOption `json:"password-source,omitempty" yaml:"password-source,omitempty"`
UnixProxy figtree.StringOption `yaml:"unixproxy,omitempty" json:"unixproxy,omitempty"`
Insecure figtree.BoolOption `yaml:"insecure,omitempty" json:"insecure,omitempty"`
}

type CommonOptions struct {
Expand All @@ -41,7 +46,7 @@ type CommonOptions struct {
type CommandRegistryEntry struct {
Help string
UsageFunc func(*figtree.FigTree, *kingpin.CmdClause) error
ExecuteFunc func(*GlobalOptions) error
ExecuteFunc func(*oreo.Client, *GlobalOptions) error
}

type CommandRegistry struct {
Expand All @@ -57,12 +62,30 @@ type kingpinAppOrCommand interface {
GetCommand(string) *kingpin.CmdClause
}

func Register(app *kingpin.Application, fig *figtree.FigTree, reg []CommandRegistry) {
func Register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree, reg []CommandRegistry) {
globals := GlobalOptions{
User: figtree.NewStringOption(os.Getenv("USER")),
}
app.Flag("endpoint", "Base URI to use for Jira").Short('e').SetValue(&globals.Endpoint)
app.Flag("user", "Login name used for authentication with Jira service").Short('u').SetValue(&globals.User)
app.Flag("unixproxy", "Path for a unix-socket proxy").SetValue(&globals.UnixProxy)
app.Flag("insecure", "Disable TLS certificate verification").Short('k').SetValue(&globals.Insecure)

app.PreAction(func(_ *kingpin.ParseContext) error {
if globals.Insecure.Value {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
o = o.WithTransport(transport)
}
if globals.UnixProxy.Value != "" {
o = o.WithTransport(unixProxy(globals.UnixProxy.Value))
}
return nil
})

for _, command := range reg {
copy := command
Expand Down Expand Up @@ -93,7 +116,7 @@ func Register(app *kingpin.Application, fig *figtree.FigTree, reg []CommandRegis

cmd.Action(
func(_ *kingpin.ParseContext) error {
return copy.Entry.ExecuteFunc(&globals)
return copy.Entry.ExecuteFunc(o, &globals)
},
)
}
Expand Down
12 changes: 6 additions & 6 deletions jiracli/unixproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"time"
)

type Transport struct {
type transport struct {
shadow *http.Transport
}

func NewUnixProxyTransport(path string) *Transport {
func newUnixProxyTransport(path string) *transport {
dial := func(network, addr string) (net.Conn, error) {
return net.Dial("unix", path)
}
Expand All @@ -25,14 +25,14 @@ func NewUnixProxyTransport(path string) *Transport {
ExpectContinueTimeout: 10 * time.Second,
}

return &Transport{shadow}
return &transport{shadow}
}

func UnixProxy(path string) *Transport {
return NewUnixProxyTransport(os.ExpandEnv(path))
func unixProxy(path string) *transport {
return newUnixProxyTransport(os.ExpandEnv(path))
}

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
req2 := *req
url2 := *req.URL
req2.URL = &url2
Expand Down
4 changes: 2 additions & 2 deletions jiracmd/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type AssignOptions struct {
Assignee string `yaml:"assignee,omitempty" json:"assignee,omitempty"`
}

func CmdAssignRegistry(o *oreo.Client) *jiracli.CommandRegistryEntry {
func CmdAssignRegistry() *jiracli.CommandRegistryEntry {
opts := AssignOptions{}

return &jiracli.CommandRegistryEntry{
Expand All @@ -25,7 +25,7 @@ func CmdAssignRegistry(o *oreo.Client) *jiracli.CommandRegistryEntry {
jiracli.LoadConfigs(cmd, fig, &opts)
return CmdAssignUsage(cmd, &opts)
},
func(globals *jiracli.GlobalOptions) error {
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
return CmdAssign(o, globals, &opts)
},
}
Expand Down
4 changes: 2 additions & 2 deletions jiracmd/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type BlockOptions struct {
jiradata.LinkIssueRequest `yaml:",inline" json:",inline" figtree:",inline"`
}

func CmdBlockRegistry(o *oreo.Client) *jiracli.CommandRegistryEntry {
func CmdBlockRegistry() *jiracli.CommandRegistryEntry {
opts := BlockOptions{
CommonOptions: jiracli.CommonOptions{
Template: figtree.NewStringOption("edit"),
Expand All @@ -37,7 +37,7 @@ func CmdBlockRegistry(o *oreo.Client) *jiracli.CommandRegistryEntry {
jiracli.LoadConfigs(cmd, fig, &opts)
return CmdBlockUsage(cmd, &opts)
},
func(globals *jiracli.GlobalOptions) error {
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
return CmdBlock(o, globals, &opts)
},
}
Expand Down
Loading

0 comments on commit c0358eb

Please sign in to comment.