This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
97 lines (79 loc) · 2.89 KB
/
config.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
// Copyright (C) 2019 The CodeActual Go Environment Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package cobra
import (
"fmt"
"strings"
tp_viper "github.com/codeactual/aws-exec-cmd/internal/third_party/github.com/config/viper"
"github.com/pkg/errors"
std_cobra "github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
cage_viper "github.com/codeactual/aws-exec-cmd/internal/cage/config/viper"
)
// Config provides viper integration and enforces prefixed environment
// variables.
//
// It has a similar method set as cobra.Mixin implementations but is not a mixin.
// It is directly used in cobra.NewCommand to automatically add the functionality.
type Config struct {
*viper.Viper
// envPrefix records the value passed to Init for which there is no viper method,
// e.g. GetEnvPrefix, to retrieve later.
//
// If viper.SetEnvPrefix is called again after Init, the value will be stale.
envPrefix string
requiredKeys map[string]bool
cmd *std_cobra.Command
}
// Init creates the config storage instance.
func (c *Config) Init(envPrefix string, cmd *std_cobra.Command) *std_cobra.Command {
c.Viper = cage_viper.NewEnvSpace(envPrefix)
c.cmd = cmd
c.envPrefix = envPrefix
c.requiredKeys = make(map[string]bool)
return cmd
}
// BindEnvToAllFlags binds all flags in the command to the viper instance.
func (c *Config) BindEnvToAllFlags(cmd *std_cobra.Command) {
if err := c.Viper.BindPFlags(cmd.Flags()); err != nil {
panic(errors.WithStack(errors.Errorf("failed to bind all flags to environment variable aliases")))
}
}
// SetRequired registers config keys which must be provided as a flag or environment value.
//
// It panics if any key is invalid.
func (c *Config) SetRequired(keys ...string) {
validKeys := make(map[string]bool)
c.cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
validKeys[f.Name] = true
})
for _, key := range keys {
if !validKeys[key] {
panic(errors.WithStack(errors.Errorf("invalid required key selection [%s]", key)))
}
c.requiredKeys[key] = true
}
}
// PreRun checks for missing required flags, completes viper/cobra binding, etc.
//
// It returns an error if any config is missing from both flags and environment.
// The error string contains a list of all missing config keys.
func (c *Config) PreRun() error {
if err := tp_viper.MergeConfig(c.cmd.Flags(), c.Viper); err != nil {
return errors.WithStack(err)
}
var missing []string
for key := range c.requiredKeys {
if !cage_viper.IsSetInCommand(c.Viper, c.cmd, c.envPrefix, key) {
missing = append(missing, fmt.Sprintf("--%s/%s", key, cage_viper.EnvPrefixedName(c.envPrefix, strings.ToUpper(key))))
}
}
if len(missing) > 0 {
return errors.Errorf("missing required configs in flags/environment: %+v", missing)
}
return nil
}