Documentation ¶
Overview ¶
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
Index ¶
- Constants
- func BindFlagToEnv(viperSession *viper.Viper, envVarPrefix string, envVar string, ...) (err error)
- func Load(envVarPrefix string, configurationToSet IServiceConfiguration, ...) error
- func LoadFromViper(viperSession *viper.Viper, envVarPrefix string, ...) (err error)
- func ValidateEmbedded(cfg Validator) error
- type IServiceConfiguration
- type Validator
Constants ¶
const ( EnvVarSeparator = "_" DotEnvFile = ".env" )
Variables ¶
This section is empty.
Functions ¶
func BindFlagToEnv ¶
func BindFlagToEnv(viperSession *viper.Viper, envVarPrefix string, envVar string, flag *pflag.Flag) (err error)
BindFlagToEnv binds pflags to environment variable. Envvar is the environment variable string with or without the prefix envVarPrefix
func Load ¶
func Load(envVarPrefix string, configurationToSet IServiceConfiguration, defaultConfiguration IServiceConfiguration) error
Load loads the configuration from the environment (i.e. .env file, environment variables) and puts the entries into the configuration object configurationToSet. If not found in the environment, the values will come from the default values defined in defaultConfiguration. `envVarPrefix` defines a prefix that ENVIRONMENT variables will use. E.g. if your prefix is "spf", the env registry will look for env variables that start with "SPF_". make sure that the tags on the fields of configurationToSet are properly set using only `[_1-9a-zA-Z]` characters.
func LoadFromViper ¶
func LoadFromViper(viperSession *viper.Viper, envVarPrefix string, configurationToSet IServiceConfiguration, defaultConfiguration IServiceConfiguration) (err error)
LoadFromViper is the same as `Load` but instead of creating a new viper session, reuse the one provided. Important note: Viper's precedence order is maintained:
- values set using explicit calls to `Set`
- flags
- environment (variables or `.env`)
- configuration file
- key/value store
- default values (set via flag default values, or calls to `SetDefault` or via `defaultConfiguration` argument provided)
Nonetheless, when it comes to default values. It differs slightly from Viper as default values from the default Configuration (i.e. `defaultConfiguration` argument provided) will take precedence over defaults set via `SetDefault` or flags unless they are considered empty values according to `reflection.IsEmpty`.
func ValidateEmbedded ¶
Use reflection to find embedded structs and validate them
Types ¶
type IServiceConfiguration ¶
type IServiceConfiguration interface { // Validates configuration entries. Validate() error }