-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsetup_config.go
More file actions
133 lines (111 loc) · 3.6 KB
/
Copy pathsetup_config.go
File metadata and controls
133 lines (111 loc) · 3.6 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
package clio
import (
"github.com/wagoodman/go-partybus"
"github.com/anchore/fangs"
"github.com/anchore/go-logger"
"github.com/anchore/go-logger/adapter/discard"
"github.com/anchore/go-logger/adapter/redact"
)
type SetupConfig struct {
// Metadata about the target application
ID Identification
// Default configuration items that end up in the target application configuration
DefaultLoggingConfig *LoggingConfig
DefaultDevelopmentConfig *DevelopmentConfig
// Items required for setting up the application (clio-only configuration)
FangsConfig fangs.Config
BusConstructor BusConstructor
LoggerConstructor LoggerConstructor
UIConstructor UIConstructor
Initializers []Initializer
postConstructs []postConstruct
postRuns []PostRun
mapExitCode MapExitCode
}
func NewSetupConfig(id Identification) *SetupConfig {
return &SetupConfig{
ID: id,
LoggerConstructor: DefaultLogger,
BusConstructor: newBus,
UIConstructor: newUI,
FangsConfig: fangs.NewConfig(id.Name).WithConfigEnvVar(),
DefaultLoggingConfig: &LoggingConfig{
Level: logger.WarnLevel,
},
// note: no ui selector or dev options by default...
}
}
func (c *SetupConfig) WithUI(uis ...UI) *SetupConfig {
c.UIConstructor = func(_ Config) (*UICollection, error) {
return NewUICollection(uis...), nil
}
return c
}
func (c *SetupConfig) WithUIConstructor(constructor UIConstructor) *SetupConfig {
c.UIConstructor = constructor
return c
}
func (c *SetupConfig) WithBusConstructor(constructor BusConstructor) *SetupConfig {
c.BusConstructor = constructor
return c
}
func (c *SetupConfig) WithNoBus() *SetupConfig {
c.BusConstructor = func(_ Config) *partybus.Bus {
return nil
}
return c
}
func (c *SetupConfig) WithLoggerConstructor(constructor LoggerConstructor) *SetupConfig {
c.LoggerConstructor = constructor
return c
}
func (c *SetupConfig) WithConfigFinders(finders ...fangs.Finder) *SetupConfig {
c.FangsConfig.Finders = append(c.FangsConfig.Finders, finders...)
return c
}
func (c *SetupConfig) WithDevelopmentConfig(cfg DevelopmentConfig) *SetupConfig {
c.DefaultDevelopmentConfig = &cfg
return c
}
func (c *SetupConfig) WithLoggingConfig(cfg LoggingConfig) *SetupConfig {
c.DefaultLoggingConfig = &cfg
return c
}
func (c *SetupConfig) WithNoLogging() *SetupConfig {
c.DefaultLoggingConfig = nil
c.LoggerConstructor = func(_ Config, _ redact.Store) (logger.Logger, error) {
return discard.New(), nil
}
return c
}
func (c *SetupConfig) WithInitializers(initializers ...Initializer) *SetupConfig {
c.Initializers = append(c.Initializers, initializers...)
return c
}
func (c *SetupConfig) WithPostRuns(postRuns ...PostRun) *SetupConfig {
c.postRuns = append(c.postRuns, postRuns...)
return c
}
func (c *SetupConfig) withPostConstructs(postConstructs ...postConstruct) *SetupConfig {
c.postConstructs = append(c.postConstructs, postConstructs...)
return c
}
// WithGlobalConfigFlag adds the global `-c` / `--config` flags to the root command
func (c *SetupConfig) WithGlobalConfigFlag() *SetupConfig {
return c.withPostConstructs(func(a *application) {
a.AddFlags(a.root.PersistentFlags(), &a.setupConfig.FangsConfig)
})
}
// WithGlobalLoggingFlags adds the global logging flags to the root command.
func (c *SetupConfig) WithGlobalLoggingFlags() *SetupConfig {
return c.withPostConstructs(func(a *application) {
a.AddFlags(a.root.PersistentFlags(), &a.state.Config)
})
}
func (c *SetupConfig) WithConfigInRootHelp() *SetupConfig {
return c
}
func (c *SetupConfig) WithMapExitCode(mapExitCode MapExitCode) *SetupConfig {
c.mapExitCode = mapExitCode
return c
}