-
Notifications
You must be signed in to change notification settings - Fork 251
/
viper.go
35 lines (29 loc) · 1.06 KB
/
viper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package config
import (
"strings"
"github.com/cilium/hubble/pkg/defaults"
"github.com/spf13/viper"
)
// NewViper creates a new viper instance configured for Hubble.
func NewViper() *viper.Viper {
vp := viper.New()
// read config from a file
vp.SetConfigName("config") // name of config file (without extension)
vp.SetConfigType("yaml") // useful if the given config file does not have the extension in the name
vp.AddConfigPath(".") // look for a config in the working directory first
if defaults.ConfigDir != "" {
vp.AddConfigPath(defaults.ConfigDir)
}
if defaults.ConfigDirFallback != "" {
vp.AddConfigPath(defaults.ConfigDirFallback)
}
// read config from environment variables
vp.SetEnvPrefix("hubble") // env var must start with HUBBLE_
// replace - by _ for environment variable names
// (eg: the env var for tls-server-name is TLS_SERVER_NAME)
vp.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
vp.AutomaticEnv() // read in environment variables that match
return vp
}