generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
57 lines (48 loc) · 1.49 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cli
import (
"github.com/broadinstitute/thelma/internal/thelma/app/builder"
"github.com/spf13/cobra"
"io"
)
// Option configuration option for a ThelmaCLI
type Option func(*Options)
// Options configuration options for a ThelmaCLI
type Options struct {
commands map[string]ThelmaCommand
thelmaConfigHook func(builder.ThelmaBuilder)
cobraConfigHooks []func(*cobra.Command)
skipRun bool
}
// DefaultOptions default options
func DefaultOptions() *Options {
return &Options{
commands: make(map[string]ThelmaCommand),
}
}
// AddCommand add a subcommand to the ThelmaCLI
func (opts *Options) AddCommand(name string, cmd ThelmaCommand) {
if err := validateCommandName(name); err != nil {
panic(err)
}
opts.commands[name] = cmd
}
// ConfigureThelma add a configuration hook for Thelma
func (opts *Options) ConfigureThelma(hook func(builder.ThelmaBuilder)) {
opts.thelmaConfigHook = hook
}
// SetOut write output to the given writer instead of os.Stdout
func (opts *Options) SetOut(writer io.Writer) {
opts.cobraConfigHooks = append(opts.cobraConfigHooks, func(command *cobra.Command) {
command.SetOut(writer)
})
}
// SetArgs set CLI args to the given list instead of os.Args
func (opts *Options) SetArgs(args []string) {
opts.cobraConfigHooks = append(opts.cobraConfigHooks, func(command *cobra.Command) {
command.SetArgs(args)
})
}
// SkipRun if true, skip run phase and only execute pre/post run hooks
func (opts *Options) SkipRun(skipRun bool) {
opts.skipRun = skipRun
}