-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_command.go
219 lines (193 loc) · 6.33 KB
/
config_command.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package clio
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/anchore/fangs"
"github.com/anchore/go-logger/adapter/redact"
)
func ConfigCommand(app Application, opts *ConfigCommandConfig) *cobra.Command {
if opts == nil {
opts = DefaultConfigCommandConfig()
}
id := app.ID()
internalApp := extractInternalApp(app)
if internalApp == nil {
return &cobra.Command{
RunE: func(_ *cobra.Command, _ []string) error {
return fmt.Errorf("unable to extract internal application, provided: %v", app)
},
}
}
cmd := &cobra.Command{
Use: "config",
Short: fmt.Sprintf("show the %s configuration", id.Name),
RunE: func(cmd *cobra.Command, _ []string) error {
allConfigs := allCommandConfigs(internalApp)
var err error
if opts.LoadConfig {
err = loadAllConfigs(cmd, internalApp.setupConfig.FangsConfig, allConfigs)
}
summary := summarizeConfig(cmd, internalApp.setupConfig.FangsConfig, opts.makeFilters(internalApp.state.RedactStore), allConfigs)
_, writeErr := os.Stdout.WriteString(summary)
if writeErr != nil {
writeErr = fmt.Errorf("an error occurred writing configuration summary: %w", writeErr)
err = errors.Join(err, writeErr)
}
if err != nil {
// space before the error display
_, _ = os.Stderr.WriteString("\n")
}
return err
},
}
cmd.Flags().BoolVarP(&opts.LoadConfig, "load", "", opts.LoadConfig, fmt.Sprintf("load and validate the %s configuration", id.Name))
if opts.IncludeLocationsSubcommand {
// sub-command to print expanded configuration file search locations
cmd.AddCommand(summarizeLocationsCommand(internalApp))
}
return cmd
}
type valueFilterFunc func(string) string
type ConfigCommandConfig struct {
LoadConfig bool
IncludeLocationsSubcommand bool
ReplaceHomeDirWithTilde bool
}
func DefaultConfigCommandConfig() *ConfigCommandConfig {
return &ConfigCommandConfig{
IncludeLocationsSubcommand: true,
ReplaceHomeDirWithTilde: true,
}
}
// WithIncludeLocationsSubcommand true will include a `config locations` subcommand which lists each location that will
// be used to locate configuration files based on the configured environment
func (c *ConfigCommandConfig) WithIncludeLocationsSubcommand(include bool) *ConfigCommandConfig {
c.IncludeLocationsSubcommand = include
return c
}
// WithReplaceHomeDirWithTilde adds a value filter function which replaces matching home directory values in strings
// starting with the user's home directory to make configurations more portable. Note: this does not apply to the
// locations subcommand, only the config command itself
func (c *ConfigCommandConfig) WithReplaceHomeDirWithTilde(replace bool) *ConfigCommandConfig {
c.ReplaceHomeDirWithTilde = replace
return c
}
func (c *ConfigCommandConfig) makeFilters(redactStore redact.Store) (filter valueFilterFunc) {
if redactStore != nil {
filter = chainFilterFuncs(redactStore.RedactString, filter)
}
if c.ReplaceHomeDirWithTilde {
userHome, _ := homedir.Dir()
if userHome != "" {
filter = chainFilterFuncs(filter, func(s string) string {
// make any defaults based on the user's home directory more portable
if strings.HasPrefix(s, userHome) {
s = strings.ReplaceAll(s, userHome, "~")
}
return s
})
}
}
return filter
}
func chainFilterFuncs(f1, f2 valueFilterFunc) valueFilterFunc {
if f1 == nil {
return f2
}
if f2 == nil {
return f1
}
return func(s string) string {
s = f1(s)
s = f2(s)
return s
}
}
func extractInternalApp(app Application) *application {
if a, ok := app.(*application); ok {
return a
}
return nil
}
func allCommandConfigs(internalApp *application) []any {
return append([]any{&internalApp.state.Config, internalApp}, internalApp.state.Config.FromCommands...)
}
func loadAllConfigs(cmd *cobra.Command, fangsCfg fangs.Config, allConfigs []any) error {
var errs []error
for _, cfg := range allConfigs {
// load each config individually, as there may be conflicting names / types that will cause
// viper to fail to read them all and panic
if err := fangs.Load(fangsCfg, cmd, cfg); err != nil {
t := reflect.TypeOf(cfg)
for t.Kind() == reflect.Pointer {
t = t.Elem()
}
errs = appendConfigLoadError(errs, t, err)
}
}
if len(errs) == 0 {
return nil
}
return fmt.Errorf("error(s) occurred loading configuration: %w", errors.Join(errs...))
}
func summarizeConfig(commandWithRootParent *cobra.Command, fangsCfg fangs.Config, redact func(string) string, allConfigs []any) string {
summary := fangs.SummarizeCommand(fangsCfg, commandWithRootParent, redact, allConfigs...)
summary = strings.TrimSpace(summary) + "\n"
return summary
}
func summarizeLocationsCommand(internalApp *application) *cobra.Command {
var all bool
cmd := &cobra.Command{
Use: "locations",
Short: fmt.Sprintf("shows all locations and the order in which %s will look for a configuration file", internalApp.ID().Name),
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
suffix := ".yaml"
if all {
suffix = ""
}
summary := summarizeLocations(internalApp.setupConfig.FangsConfig, suffix)
_, err := os.Stdout.WriteString(summary)
return err
},
}
cmd.Flags().BoolVarP(&all, "all", "", all, "include every file extension supported")
return cmd
}
func summarizeLocations(fangsCfg fangs.Config, onlySuffix string) string {
out := ""
for _, f := range fangs.SummarizeLocations(fangsCfg) {
if onlySuffix != "" && !strings.HasSuffix(f, onlySuffix) {
continue
}
out += f + "\n"
}
return out
}
// appendConfigLoadError appends errors including originating struct, but deduplicates identical errors that occur across multiple load calls
func appendConfigLoadError(errs []error, t reflect.Type, err error) []error {
if err == nil {
return errs
}
msg := err.Error()
// remove configuration object source when we get the same error from multiple sources
for i, e := range errs {
// already have this error, don't append
if e.Error() == msg {
return errs
}
// if we have an identical wrapped error, this occurred when loading multiple configurations so just show the error
if e, ok := e.(interface{ Unwrap() error }); ok {
if e.Unwrap().Error() == msg {
errs[i] = err
return errs
}
}
}
return append(errs, fmt.Errorf("error loading config '%s.%s': %w", t.PkgPath(), t.Name(), err))
}