2020-04-08 14:55:42 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-13 17:38:07 +03:00
|
|
|
"os"
|
2020-04-08 14:55:42 +03:00
|
|
|
"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) {
|
2021-10-13 17:38:07 +03:00
|
|
|
err := v.BindPFlag(key, f)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "viper failed binding pflag: %v with error: %v \n", key, err)
|
|
|
|
}
|
2020-04-08 14:55:42 +03:00
|
|
|
key = ViperEnvReplacer.Replace(key)
|
|
|
|
key = strings.ToUpper(ViperEnvPrefix + "_" + key)
|
|
|
|
f.Usage = f.Usage + fmt.Sprintf(` (env "%s")`, key)
|
|
|
|
}
|