Merge pull request #341 from HuKeping/parse-viper

Add a function to parse viper
This commit is contained in:
David Lawrence 2015-12-08 17:31:55 -08:00
commit 2bfadacf34
4 changed files with 47 additions and 27 deletions

View File

@ -8,8 +8,6 @@ import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"path/filepath"
"strings"
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
@ -186,19 +184,9 @@ func main() {
ctx := context.Background() ctx := context.Background()
filename := filepath.Base(configFile) // parse viper config
ext := filepath.Ext(configFile) if err := utils.ParseViper(mainViper, configFile); err != nil {
configPath := filepath.Dir(configFile) logrus.Fatal(err.Error())
mainViper.SetConfigType(strings.TrimPrefix(ext, "."))
mainViper.SetConfigName(strings.TrimSuffix(filename, ext))
mainViper.AddConfigPath(configPath)
err := mainViper.ReadInConfig()
if err != nil {
logrus.Error("Viper Error: ", err.Error())
logrus.Error("Could not read config at ", configFile)
os.Exit(1)
} }
// default is error level // default is error level

View File

@ -12,7 +12,6 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
@ -181,17 +180,9 @@ func main() {
// when the signer starts print the version for debugging and issue logs later // when the signer starts print the version for debugging and issue logs later
logrus.Infof("Version: %s, Git commit: %s", version.NotaryVersion, version.GitCommit) logrus.Infof("Version: %s, Git commit: %s", version.NotaryVersion, version.GitCommit)
filename := filepath.Base(configFile) // parse viper config
ext := filepath.Ext(configFile) if err := utils.ParseViper(mainViper, configFile); err != nil {
configPath := filepath.Dir(configFile) logrus.Fatal(err.Error())
mainViper.SetConfigType(strings.TrimPrefix(ext, "."))
mainViper.SetConfigName(strings.TrimSuffix(filename, ext))
mainViper.AddConfigPath(configPath)
if err := mainViper.ReadInConfig(); err != nil {
logrus.Errorf("Could not read config at :%s, viper error: %v", configFile, err)
os.Exit(1)
} }
// default is error level // default is error level

View File

@ -159,3 +159,19 @@ func SetUpBugsnag(config *bugsnag.Configuration) error {
} }
return nil return nil
} }
// ParseViper tries to parse out a Viper from a configuration file.
func ParseViper(v *viper.Viper, configFile string) error {
filename := filepath.Base(configFile)
ext := filepath.Ext(configFile)
configPath := filepath.Dir(configFile)
v.SetConfigType(strings.TrimPrefix(ext, "."))
v.SetConfigName(strings.TrimSuffix(filename, ext))
v.AddConfigPath(configPath)
if err := v.ReadInConfig(); err != nil {
return fmt.Errorf("Could not read config at :%s, viper error: %v", configFile, err)
}
return nil
}

View File

@ -335,3 +335,28 @@ func TestParseTLSWithEnvironmentVariables(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, *tlsOpts) assert.Equal(t, expected, *tlsOpts)
} }
func TestParseViperWithInvalidFile(t *testing.T) {
v := viper.New()
SetupViper(v, envPrefix)
err := ParseViper(v, "Chronicle_Of_Dark_Secrets.json")
assert.Error(t, err)
assert.Contains(t, err.Error(), "Could not read config")
}
func TestParseViperWithValidFile(t *testing.T) {
file, err := os.Create("/tmp/Chronicle_Of_Dark_Secrets.json")
assert.NoError(t, err)
defer os.Remove(file.Name())
file.WriteString(`{"logging": {"level": "debug"}}`)
v := viper.New()
SetupViper(v, envPrefix)
err = ParseViper(v, "/tmp/Chronicle_Of_Dark_Secrets.json")
assert.NoError(t, err)
assert.Equal(t, "debug", v.GetString("logging.level"))
}