Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Allow to read config also from environment (#14)
Browse files Browse the repository at this point in the history
* Allow to read config also from environment

Fixes #13


Co-authored-by: Mark Yen <3977982+mook-as@users.noreply.github.com>
  • Loading branch information
mudler and mook-as committed Apr 24, 2020
1 parent 8c8504b commit 3b15598
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
46 changes: 38 additions & 8 deletions cmd/root.go
@@ -1,6 +1,9 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"

eirinix "github.com/SUSE/eirinix"
Expand All @@ -22,6 +25,14 @@ var rootCmd = &cobra.Command{
Short: "eirini-loggregator-bridge streams Eirini application logs to CloudFoundry loggregator",
Run: func(cmd *cobra.Command, args []string) {
var err error

LogDebug("Namespace: ", config.Namespace)
LogDebug("Loggregator-endpoint: ", config.LoggregatorEndpoint)
LogDebug("Loggregator-ca-path: ", config.LoggregatorCAPath)
LogDebug("Loggregator-cert-path: ", config.LoggregatorCertPath)
LogDebug("Loggregator-key-path: ", config.LoggregatorKeyPath)
LogDebug("Starting Loggregator")

err = config.Validate()
if err != nil {
LogError(err.Error())
Expand Down Expand Up @@ -64,17 +75,36 @@ func init() {
}

func initConfig() {

// As Viper cannot unmarshal and merge configs from yaml automatically,
// define inline there the mapping explictly.
// See: https://github.com/spf13/viper/issues/761
viper.SetDefault("NAMESPACE", "")
viper.SetDefault("LOGGREGATOR_KEY_PATH", "")
viper.SetDefault("LOGGREGATOR_ENDPOINT", "")
viper.SetDefault("LOGGREGATOR_CA_PATH", "")
viper.SetDefault("LOGGREGATOR_CERT_PATH", "")
viper.BindEnv("namespace", "NAMESPACE")
viper.BindEnv("loggregator-key-path", "LOGGREGATOR_KEY_PATH")
viper.BindEnv("loggregator-endpoint", "LOGGREGATOR_ENDPOINT")
viper.BindEnv("loggregator-ca-path", "LOGGREGATOR_CA_PATH")
viper.BindEnv("loggregator-cert-path", "LOGGREGATOR_CERT_PATH")

if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
LogError("You didn't specify a config file (Use --help)")
os.Exit(1)
yamlFile, err := ioutil.ReadFile(cfgFile)
if err != nil {
LogError(err.Error())
os.Exit(1)
}

viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBuffer(yamlFile))
}

if err := viper.ReadInConfig(); err != nil {
LogError("Can't read config:", err.Error())
// Now this call will take into account the env as well
err := viper.Unmarshal(&config)
if err != nil {
LogError(err.Error())
os.Exit(1)
}
viper.Unmarshal(&config)
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -9,6 +9,7 @@ require (
github.com/onsi/gomega v1.9.0
github.com/spf13/cobra v0.0.7
github.com/spf13/viper v1.6.3
gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.0.0-20200404061942-2a93acf49b83
k8s.io/apimachinery v0.0.0-20200410010401-7378bafd8ae2
k8s.io/client-go v0.0.0-20200330143601-07e69aceacd6
Expand Down

0 comments on commit 3b15598

Please sign in to comment.