mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
bd5ceb5142
Signed-off-by: Anders Eknert <anders@styra.com>
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package env
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type cmdFlags interface {
|
|
CheckEnvironmentVariables(command *cobra.Command) error
|
|
}
|
|
|
|
type cmdFlagsImpl struct{}
|
|
|
|
var (
|
|
CmdFlags cmdFlags = cmdFlagsImpl{}
|
|
errorMessagePrefix = "error mapping environment variables to command flags"
|
|
)
|
|
|
|
const globalPrefix = "opa"
|
|
|
|
func (cmdFlagsImpl) CheckEnvironmentVariables(command *cobra.Command) error {
|
|
var errs []string
|
|
v := viper.New()
|
|
v.AutomaticEnv()
|
|
if command.Name() == globalPrefix {
|
|
v.SetEnvPrefix(command.Name())
|
|
} else {
|
|
v.SetEnvPrefix(fmt.Sprintf("%s_%s", globalPrefix, command.Name()))
|
|
}
|
|
command.Flags().VisitAll(func(f *pflag.Flag) {
|
|
configName := f.Name
|
|
configName = strings.ReplaceAll(configName, "-", "_")
|
|
if !f.Changed && v.IsSet(configName) {
|
|
val := v.Get(configName)
|
|
err := command.Flags().Set(f.Name, fmt.Sprintf("%v", val))
|
|
if err != nil {
|
|
errs = append(errs, err.Error())
|
|
}
|
|
}
|
|
})
|
|
|
|
if len(errs) == 0 {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%s: %s", errorMessagePrefix, strings.Join(errs, "; "))
|
|
}
|