graphql-engine/cli/util/viper.go
Kali Vara Purushotham Santhati 94a3be3e6e cli: fix lint errors
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/1749
Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 4515f7f2c58b7f28645b2c5a5d9842aa7a844eae
2021-10-13 14:39:15 +00:00

28 lines
700 B
Go

package util
import (
"fmt"
"os"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// ViperEnvPrefix - Env prefix to be used in viper
const ViperEnvPrefix = "HASURA_GRAPHQL"
// ViperEnvReplacer - Env replacer to be used in viper
var ViperEnvReplacer = strings.NewReplacer(".", "_")
// BindPFlag - binds flag with viper along with env usage
func BindPFlag(v *viper.Viper, key string, f *pflag.Flag) {
err := v.BindPFlag(key, f)
if err != nil {
fmt.Fprintf(os.Stderr, "viper failed binding pflag: %v with error: %v \n", key, err)
}
key = ViperEnvReplacer.Replace(key)
key = strings.ToUpper(ViperEnvPrefix + "_" + key)
f.Usage = f.Usage + fmt.Sprintf(` (env "%s")`, key)
}