refactor for GlobalOptions and CommonOptions

This commit is contained in:
Cory Bennett
2017-09-02 14:05:27 -07:00
parent 65891e7b3b
commit 979da1f3a5
37 changed files with 544 additions and 647 deletions
+17 -20
View File
@@ -19,35 +19,32 @@ const (
)
type WatchOptions struct {
jiracli.GlobalOptions `yaml:",inline" json:",inline" figtree:",inline"`
Issue string `yaml:"issue,omitempty" json:"issue,omitempty"`
Watcher string `yaml:"watcher,omitempty" json:"watcher,omitempty"`
Action WatchAction `yaml:"-" json:"-"`
jiracli.CommonOptions `yaml:",inline" json:",inline" figtree:",inline"`
Issue string `yaml:"issue,omitempty" json:"issue,omitempty"`
Watcher string `yaml:"watcher,omitempty" json:"watcher,omitempty"`
Action WatchAction `yaml:"-" json:"-"`
}
func CmdWatchRegistry(fig *figtree.FigTree, o *oreo.Client) *jiracli.CommandRegistryEntry {
func CmdWatchRegistry(o *oreo.Client) *jiracli.CommandRegistryEntry {
opts := WatchOptions{
GlobalOptions: jiracli.GlobalOptions{},
CommonOptions: jiracli.CommonOptions{},
Action: WatcherAdd,
}
return &jiracli.CommandRegistryEntry{
"Add/Remove watcher to issue",
func() error {
return CmdWatch(o, &opts)
},
func(cmd *kingpin.CmdClause) error {
func(fig *figtree.FigTree, cmd *kingpin.CmdClause) error {
jiracli.LoadConfigs(cmd, fig, &opts)
return CmdWatchUsage(cmd, &opts)
},
func(globals *jiracli.GlobalOptions) error {
return CmdWatch(o, globals, &opts)
},
}
}
func CmdWatchUsage(cmd *kingpin.CmdClause, opts *WatchOptions) error {
if err := jiracli.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
jiracli.BrowseUsage(cmd, &opts.GlobalOptions)
jiracli.BrowseUsage(cmd, &opts.CommonOptions)
cmd.Flag("remove", "remove watcher from issue").Short('r').PreAction(func(ctx *kingpin.ParseContext) error {
opts.Action = WatcherRemove
return nil
@@ -59,24 +56,24 @@ func CmdWatchUsage(cmd *kingpin.CmdClause, opts *WatchOptions) error {
// CmdWatch will add the given watcher to the issue (or remove the watcher
// with the 'remove' flag)
func CmdWatch(o *oreo.Client, opts *WatchOptions) error {
func CmdWatch(o *oreo.Client, globals *jiracli.GlobalOptions, opts *WatchOptions) error {
if opts.Watcher == "" {
opts.Watcher = opts.User.Value
opts.Watcher = globals.User.Value
}
if opts.Action == WatcherAdd {
if err := jira.IssueAddWatcher(o, opts.Endpoint.Value, opts.Issue, opts.Watcher); err != nil {
if err := jira.IssueAddWatcher(o, globals.Endpoint.Value, opts.Issue, opts.Watcher); err != nil {
return err
}
} else {
if err := jira.IssueRemoveWatcher(o, opts.Endpoint.Value, opts.Issue, opts.Watcher); err != nil {
if err := jira.IssueRemoveWatcher(o, globals.Endpoint.Value, opts.Issue, opts.Watcher); err != nil {
return err
}
}
fmt.Printf("OK %s %s/browse/%s\n", opts.Issue, opts.Endpoint.Value, opts.Issue)
fmt.Printf("OK %s %s/browse/%s\n", opts.Issue, globals.Endpoint.Value, opts.Issue)
if opts.Browse.Value {
return CmdBrowse(&BrowseOptions{opts.GlobalOptions, opts.Issue})
return CmdBrowse(globals, opts.Issue)
}
return nil