Skip to content

Commit

Permalink
Merge pull request #194 from noborus/yaml-check
Browse files Browse the repository at this point in the history
Added yaml check
  • Loading branch information
noborus authored May 26, 2022
2 parents 6965d05 + 5761bb0 commit a24a9b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
51 changes: 51 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"testing"

"github.com/spf13/viper"
)

// This test is a yaml file check.
func Test_Config(t *testing.T) {
type args struct {
cfgFile string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "test ov.yaml",
args: args{
cfgFile: "ov.yaml",
},
wantErr: false,
},
{
name: "test ov-less.yaml",
args: args{
cfgFile: "ov-less.yaml",
},
wantErr: false,
},
{
name: "test error",
args: args{
cfgFile: "config-fail.yaml",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
viper.SetConfigFile(tt.args.cfgFile)
err := viper.ReadInConfig()
if (err != nil) != tt.wantErr {
t.Errorf("Config error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,10 @@ func initConfig() {
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
_ = viper.ReadInConfig()

if err := viper.ReadInConfig(); err != nil {
fmt.Printf("%s: %s\n", viper.ConfigFileUsed(), err.Error())
os.Exit(1)
}
if err := viper.Unmarshal(&config); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down

0 comments on commit a24a9b8

Please sign in to comment.