/
options.go
120 lines (102 loc) · 2.25 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cmd
import (
"context"
"github.com/urfave/cli/v2"
)
type Option func(o *Options)
type Options struct {
// Name of the application
Name string
// Description of the application
Description string
// Version of the application
Version string
// Action to execute when Run is called and there is no subcommand
Action func(*cli.Context) error
// TODO replace with built in command definition
Commands []*cli.Command
// TODO replace with built in flags definition
Flags []cli.Flag
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
// Command line Name
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
// Command line Description
func Description(d string) Option {
return func(o *Options) {
o.Description = d
}
}
// Command line Version
func Version(v string) Option {
return func(o *Options) {
o.Version = v
}
}
// Commands to add
func Commands(c ...*cli.Command) Option {
return func(o *Options) {
o.Commands = c
}
}
// Flags to add
func Flags(f ...cli.Flag) Option {
return func(o *Options) {
o.Flags = f
}
}
// Action to execute
func Action(a func(*cli.Context) error) Option {
return func(o *Options) {
o.Action = a
}
}
type beforeKey struct{}
type setupOnlyKey struct{}
// Before sets a function to be called before micro is setup
func Before(f cli.BeforeFunc) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, beforeKey{}, f)
}
}
func beforeFromContext(ctx context.Context, def cli.BeforeFunc) cli.BeforeFunc {
if ctx == nil {
return def
}
a, ok := ctx.Value(beforeKey{}).(cli.BeforeFunc)
if !ok {
return def
}
// perform the before func passed in the context before the default
return func(ctx *cli.Context) error {
if err := a(ctx); err != nil {
return err
}
return def(ctx)
}
}
// SetupOnly for to execute
func SetupOnly() Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, setupOnlyKey{}, true)
}
}
func setupOnlyFromContext(ctx context.Context) bool {
if ctx == nil {
return false
}
a, _ := ctx.Value(setupOnlyKey{}).(bool)
return a
}