Skip to content

Commit

Permalink
refactor for GlobalOptions and CommonOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Sep 2, 2017
1 parent 65891e7 commit 979da1f
Show file tree
Hide file tree
Showing 37 changed files with 544 additions and 647 deletions.
95 changes: 48 additions & 47 deletions cmd/jira/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

jira "gopkg.in/Netflix-Skunkworks/go-jira.v1"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiracli"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiracmd"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/op/go-logging.v1"
)
Expand Down Expand Up @@ -100,200 +101,200 @@ func main() {
registry := []jiracli.CommandRegistry{
jiracli.CommandRegistry{
Command: "login",
Entry: jiracli.CmdLoginRegistry(fig, o),
Entry: jiracmd.CmdLoginRegistry(o),
},
jiracli.CommandRegistry{
Command: "logout",
Entry: jiracli.CmdLogoutRegistry(fig, o),
Entry: jiracmd.CmdLogoutRegistry(o),
},
jiracli.CommandRegistry{
Command: "list",
Aliases: []string{"ls"},
Entry: jiracli.CmdListRegistry(fig, o),
Entry: jiracmd.CmdListRegistry(o),
},
jiracli.CommandRegistry{
Command: "view",
Entry: jiracli.CmdViewRegistry(fig, o),
Entry: jiracmd.CmdViewRegistry(o),
},
jiracli.CommandRegistry{
Command: "create",
Entry: jiracli.CmdCreateRegistry(fig, o),
Entry: jiracmd.CmdCreateRegistry(o),
},
jiracli.CommandRegistry{
Command: "edit",
Entry: jiracli.CmdEditRegistry(fig, o),
Entry: jiracmd.CmdEditRegistry(o),
},
jiracli.CommandRegistry{
Command: "comment",
Entry: jiracli.CmdCommentRegistry(fig, o),
Entry: jiracmd.CmdCommentRegistry(o),
},
jiracli.CommandRegistry{
Command: "worklog list",
Entry: jiracli.CmdWorklogListRegistry(fig, o),
Entry: jiracmd.CmdWorklogListRegistry(o),
Default: true,
},
jiracli.CommandRegistry{
Command: "worklog add",
Entry: jiracli.CmdWorklogAddRegistry(fig, o),
Entry: jiracmd.CmdWorklogAddRegistry(o),
},
jiracli.CommandRegistry{
Command: "fields",
Entry: jiracli.CmdFieldsRegistry(fig, o),
Entry: jiracmd.CmdFieldsRegistry(o),
},
jiracli.CommandRegistry{
Command: "createmeta",
Entry: jiracli.CmdCreateMetaRegistry(fig, o),
Entry: jiracmd.CmdCreateMetaRegistry(o),
},
jiracli.CommandRegistry{
Command: "editmeta",
Entry: jiracli.CmdEditMetaRegistry(fig, o),
Entry: jiracmd.CmdEditMetaRegistry(o),
},
jiracli.CommandRegistry{
Command: "subtask",
Entry: jiracli.CmdSubtaskRegistry(fig, o),
Entry: jiracmd.CmdSubtaskRegistry(o),
},
jiracli.CommandRegistry{
Command: "dup",
Entry: jiracli.CmdDupRegistry(fig, o),
Entry: jiracmd.CmdDupRegistry(o),
},
jiracli.CommandRegistry{
Command: "block",
Entry: jiracli.CmdBlockRegistry(fig, o),
Entry: jiracmd.CmdBlockRegistry(o),
},
jiracli.CommandRegistry{
Command: "issuelink",
Entry: jiracli.CmdIssueLinkRegistry(fig, o),
Entry: jiracmd.CmdIssueLinkRegistry(o),
},
jiracli.CommandRegistry{
Command: "issuelinktypes",
Entry: jiracli.CmdIssueLinkTypesRegistry(fig, o),
Entry: jiracmd.CmdIssueLinkTypesRegistry(o),
},
jiracli.CommandRegistry{
Command: "transition",
Aliases: []string{"trans"},
Entry: jiracli.CmdTransitionRegistry(fig, o, ""),
Entry: jiracmd.CmdTransitionRegistry(o, ""),
},
jiracli.CommandRegistry{
Command: "transitions",
Entry: jiracli.CmdTransitionsRegistry(fig, o, "transitions"),
Entry: jiracmd.CmdTransitionsRegistry(o, "transitions"),
},
jiracli.CommandRegistry{
Command: "transmeta",
Entry: jiracli.CmdTransitionsRegistry(fig, o, "debug"),
Entry: jiracmd.CmdTransitionsRegistry(o, "debug"),
},
jiracli.CommandRegistry{
Command: "close",
Entry: jiracli.CmdTransitionRegistry(fig, o, "close"),
Entry: jiracmd.CmdTransitionRegistry(o, "close"),
},
jiracli.CommandRegistry{
Command: "acknowledge",
Aliases: []string{"ack"},
Entry: jiracli.CmdTransitionRegistry(fig, o, "acknowledge"),
Entry: jiracmd.CmdTransitionRegistry(o, "acknowledge"),
},
jiracli.CommandRegistry{
Command: "reopen",
Entry: jiracli.CmdTransitionRegistry(fig, o, "reopen"),
Entry: jiracmd.CmdTransitionRegistry(o, "reopen"),
},
jiracli.CommandRegistry{
Command: "resolve",
Entry: jiracli.CmdTransitionRegistry(fig, o, "resolve"),
Entry: jiracmd.CmdTransitionRegistry(o, "resolve"),
},
jiracli.CommandRegistry{
Command: "start",
Entry: jiracli.CmdTransitionRegistry(fig, o, "start"),
Entry: jiracmd.CmdTransitionRegistry(o, "start"),
},
jiracli.CommandRegistry{
Command: "stop",
Entry: jiracli.CmdTransitionRegistry(fig, o, "stop"),
Entry: jiracmd.CmdTransitionRegistry(o, "stop"),
},
jiracli.CommandRegistry{
Command: "todo",
Entry: jiracli.CmdTransitionRegistry(fig, o, "To Do"),
Entry: jiracmd.CmdTransitionRegistry(o, "To Do"),
},
jiracli.CommandRegistry{
Command: "backlog",
Entry: jiracli.CmdTransitionRegistry(fig, o, "Backlog"),
Entry: jiracmd.CmdTransitionRegistry(o, "Backlog"),
},
jiracli.CommandRegistry{
Command: "done",
Entry: jiracli.CmdTransitionRegistry(fig, o, "Done"),
Entry: jiracmd.CmdTransitionRegistry(o, "Done"),
},
jiracli.CommandRegistry{
Command: "in-progress",
Aliases: []string{"prog", "progress"},
Entry: jiracli.CmdTransitionRegistry(fig, o, "Progress"),
Entry: jiracmd.CmdTransitionRegistry(o, "Progress"),
},
jiracli.CommandRegistry{
Command: "vote",
Entry: jiracli.CmdVoteRegistry(fig, o),
Entry: jiracmd.CmdVoteRegistry(o),
},
jiracli.CommandRegistry{
Command: "rank",
Entry: jiracli.CmdRankRegistry(fig, o),
Entry: jiracmd.CmdRankRegistry(o),
},
jiracli.CommandRegistry{
Command: "watch",
Entry: jiracli.CmdWatchRegistry(fig, o),
Entry: jiracmd.CmdWatchRegistry(o),
},
jiracli.CommandRegistry{
Command: "labels add",
Entry: jiracli.CmdLabelsAddRegistry(fig, o),
Entry: jiracmd.CmdLabelsAddRegistry(o),
},
jiracli.CommandRegistry{
Command: "labels set",
Entry: jiracli.CmdLabelsAddRegistry(fig, o),
Entry: jiracmd.CmdLabelsAddRegistry(o),
},
jiracli.CommandRegistry{
Command: "labels remove",
Entry: jiracli.CmdLabelsAddRegistry(fig, o),
Entry: jiracmd.CmdLabelsAddRegistry(o),
Aliases: []string{"rm"},
},
jiracli.CommandRegistry{
Command: "take",
Entry: jiracli.CmdTakeRegistry(fig, o),
Entry: jiracmd.CmdTakeRegistry(o),
},
jiracli.CommandRegistry{
Command: "assign",
Entry: jiracli.CmdAssignRegistry(fig, o),
Entry: jiracmd.CmdAssignRegistry(o),
Aliases: []string{"give"},
},
jiracli.CommandRegistry{
Command: "unassign",
Entry: jiracli.CmdUnassignRegistry(fig, o),
Entry: jiracmd.CmdUnassignRegistry(o),
},
jiracli.CommandRegistry{
Command: "component add",
Entry: jiracli.CmdComponentAddRegistry(fig, o),
Entry: jiracmd.CmdComponentAddRegistry(o),
},
jiracli.CommandRegistry{
Command: "components",
Entry: jiracli.CmdComponentsRegistry(fig, o),
Entry: jiracmd.CmdComponentsRegistry(o),
},
jiracli.CommandRegistry{
Command: "issuetypes",
Entry: jiracli.CmdIssueTypesRegistry(fig, o),
Entry: jiracmd.CmdIssueTypesRegistry(o),
},
jiracli.CommandRegistry{
Command: "export-templates",
Entry: jiracli.CmdExportTemplatesRegistry(fig),
Entry: jiracmd.CmdExportTemplatesRegistry(),
},
jiracli.CommandRegistry{
Command: "unexport-templates",
Entry: jiracli.CmdUnexportTemplatesRegistry(fig),
Entry: jiracmd.CmdUnexportTemplatesRegistry(),
},
jiracli.CommandRegistry{
Command: "browse",
Entry: jiracli.CmdBrowseRegistry(fig),
Entry: jiracmd.CmdBrowseRegistry(),
Aliases: []string{"b"},
},
jiracli.CommandRegistry{
Command: "request",
Entry: jiracli.CmdRequestRegistry(fig, o),
Entry: jiracmd.CmdRequestRegistry(o),
Aliases: []string{"req"},
},
}

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

// register custom commands
data := struct {
Expand Down
53 changes: 26 additions & 27 deletions jiracli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ type Exit struct {
}

type GlobalOptions struct {
Browse figtree.BoolOption `json:"browse,omitempty" yaml:"browse,omitempty"`
Editor figtree.StringOption `json:"editor,omitempty" yaml:"editor,omitempty"`
Endpoint figtree.StringOption `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
SkipEditing figtree.BoolOption `json:"noedit,omitempty" yaml:"noedit,omitempty"`
PasswordSource figtree.StringOption `json:"password-source,omitempty" yaml:"password-source,omitempty"`
Template figtree.StringOption `json:"template,omitempty" yaml:"template,omitempty"`
User figtree.StringOption `json:"user,omitempty" yaml:"user,omitempty"`
PasswordSource figtree.StringOption `json:"password-source,omitempty" yaml:"password-source,omitempty"`
}

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

type CommandRegistryEntry struct {
Help string
ExecuteFunc func() error
UsageFunc func(*kingpin.CmdClause) error
UsageFunc func(*figtree.FigTree, *kingpin.CmdClause) error
ExecuteFunc func(*GlobalOptions) error
}

type CommandRegistry struct {
Expand All @@ -54,7 +57,13 @@ type kingpinAppOrCommand interface {
GetCommand(string) *kingpin.CmdClause
}

func Register(app *kingpin.Application, reg []CommandRegistry) {
func Register(app *kingpin.Application, 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)

for _, command := range reg {
copy := command
commandFields := strings.Fields(copy.Command)
Expand All @@ -70,36 +79,26 @@ func Register(app *kingpin.Application, reg []CommandRegistry) {
}

cmd := appOrCmd.Command(commandFields[len(commandFields)-1], copy.Entry.Help)
LoadConfigs(cmd, fig, &globals)

for _, alias := range copy.Aliases {
cmd = cmd.Alias(alias)
}
if copy.Default {
cmd = cmd.Default()
}
if copy.Entry.UsageFunc != nil {
copy.Entry.UsageFunc(cmd)
copy.Entry.UsageFunc(fig, cmd)
}

cmd.Action(
func(_ *kingpin.ParseContext) error {
return copy.Entry.ExecuteFunc()
return copy.Entry.ExecuteFunc(&globals)
},
)
}
}

func GlobalUsage(cmd *kingpin.CmdClause, opts *GlobalOptions) error {
cmd.PreAction(func(_ *kingpin.ParseContext) error {
if opts.User.Value == "" {
opts.User = figtree.NewStringOption(os.Getenv("USER"))
}
return nil
})
cmd.Flag("endpoint", "Base URI to use for Jira").Short('e').SetValue(&opts.Endpoint)
cmd.Flag("user", "Login name used for authentication with Jira service").Short('u').SetValue(&opts.User)
return nil
}

func LoadConfigs(cmd *kingpin.CmdClause, fig *figtree.FigTree, opts interface{}) {
cmd.PreAction(func(_ *kingpin.ParseContext) error {
os.Setenv("JIRA_OPERATION", cmd.FullCommand())
Expand All @@ -112,19 +111,19 @@ func LoadConfigs(cmd *kingpin.CmdClause, fig *figtree.FigTree, opts interface{})
})
}

func BrowseUsage(cmd *kingpin.CmdClause, opts *GlobalOptions) {
func BrowseUsage(cmd *kingpin.CmdClause, opts *CommonOptions) {
cmd.Flag("browse", "Open issue(s) in browser after operation").Short('b').SetValue(&opts.Browse)
}

func EditorUsage(cmd *kingpin.CmdClause, opts *GlobalOptions) {
func EditorUsage(cmd *kingpin.CmdClause, opts *CommonOptions) {
cmd.Flag("editor", "Editor to use").SetValue(&opts.Editor)
}

func TemplateUsage(cmd *kingpin.CmdClause, opts *GlobalOptions) {
func TemplateUsage(cmd *kingpin.CmdClause, opts *CommonOptions) {
cmd.Flag("template", "Template to use for output").Short('t').SetValue(&opts.Template)
}

func (o *GlobalOptions) editFile(fileName string) (changes bool, err error) {
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"} {
if ed != "" {
Expand Down Expand Up @@ -187,7 +186,7 @@ func (o *GlobalOptions) editFile(fileName string) (changes bool, err error) {
return false, err
}

func EditLoop(opts *GlobalOptions, input interface{}, output interface{}, submit func() error) error {
func EditLoop(opts *CommonOptions, input interface{}, output interface{}, submit func() error) error {
tmpFile, err := tmpTemplate(opts.Template.Value, input)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 979da1f

Please sign in to comment.