MCPcopy
hub / github.com/keploy/keploy / BindFlagsToViper

Function BindFlagsToViper

utils/utils.go:229–262  ·  view source on GitHub ↗
(logger *zap.Logger, cmd *cobra.Command, viperKeyPrefix string)

Source from the content-addressed store, hash-verified

227}
228
229func BindFlagsToViper(logger *zap.Logger, cmd *cobra.Command, viperKeyPrefix string) error {
230 var bindErr error
231 cmd.Flags().VisitAll(func(flag *pflag.Flag) {
232 camelCaseName := kebabToCamel(flag.Name)
233 err := viper.BindPFlag(camelCaseName, flag)
234 if err != nil {
235 LogError(logger, err, "failed to bind flag Name to flag")
236 bindErr = err
237 }
238 // Construct the Viper key and the env variable name
239 if viperKeyPrefix == "" {
240 viperKeyPrefix = cmd.Name()
241 }
242 viperKey := viperKeyPrefix + "." + camelCaseName
243 envVarName := strings.ToUpper(viperKeyPrefix + "_" + camelCaseName)
244 envVarName = strings.ReplaceAll(envVarName, ".", "_") // Why do we need this?
245
246 // Bind the flag to Viper with the constructed key
247 err = viper.BindPFlag(viperKey, flag)
248 if err != nil {
249 LogError(logger, err, "failed to bind flag to config")
250 bindErr = err
251 }
252
253 // Tell Viper to also read this flag's value from the corresponding env variable
254 err = viper.BindEnv(viperKey, envVarName)
255 logger.Debug("Binding flag to viper", zap.String("viperKey", viperKey), zap.String("envVarName", envVarName))
256 if err != nil {
257 LogError(logger, err, "failed to bind environment variables to config")
258 bindErr = err
259 }
260 })
261 return bindErr
262}
263
264//func ModifyToSentryLogger(ctx context.Context, logger *zap.Logger, client *sentry.Client, configDb *configdb.ConfigDb) *zap.Logger {
265// cfg := zapsentry.Configuration{

Callers 1

PreProcessFlagsMethod · 0.92

Calls 5

kebabToCamelFunction · 0.85
LogErrorFunction · 0.85
NameMethod · 0.80
DebugMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected