adds error test for config
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
This commit is contained in:
parent
8cc2de5c2a
commit
538e23708a
@ -2,7 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@ -49,7 +48,7 @@ func createDefaultConf() *Conf {
|
||||
}
|
||||
|
||||
// GetConf returns config values
|
||||
func GetConf(filename string) *Conf {
|
||||
func GetConf(filename string) (*Conf, error) {
|
||||
|
||||
cnf := createDefaultConf()
|
||||
|
||||
@ -57,18 +56,17 @@ func GetConf(filename string) *Conf {
|
||||
vv.SetConfigName(filename)
|
||||
vv.SetConfigType("yaml")
|
||||
vv.AddConfigPath(".")
|
||||
log.Printf("Initialising config with filename %s", filename)
|
||||
|
||||
err := vv.ReadInConfig()
|
||||
if err != nil {
|
||||
log.Fatal("Could not load config file: \n", err)
|
||||
os.Exit(1)
|
||||
log.Print("Could not load config file: \n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = vv.Unmarshal(&cnf)
|
||||
if err != nil {
|
||||
log.Fatal("Could not read config file into struct: \n", err)
|
||||
os.Exit(1)
|
||||
log.Print("Could not read config file into struct: \n", err)
|
||||
return nil, err
|
||||
}
|
||||
return cnf
|
||||
return cnf, nil
|
||||
}
|
||||
|
@ -10,7 +10,8 @@ import (
|
||||
func TestSample(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf := config.GetConf("config-sample")
|
||||
conf, err := config.GetConf("config-sample")
|
||||
assert.Nil(err)
|
||||
|
||||
appConf := conf.App
|
||||
assert.Equal("devel", appConf.Environment)
|
||||
@ -25,10 +26,11 @@ func TestSample(t *testing.T) {
|
||||
assert.Equal("g2", dbConf.Database)
|
||||
}
|
||||
|
||||
func TestMissing(t *testing.T) {
|
||||
func TestDefault(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf := config.GetConf("config-missing-fields")
|
||||
conf, err := config.GetConf("config-missing-fields")
|
||||
assert.Nil(err)
|
||||
|
||||
appConf := conf.App
|
||||
assert.Equal("missingfield", appConf.Environment)
|
||||
@ -42,3 +44,11 @@ func TestMissing(t *testing.T) {
|
||||
assert.Equal("<password>", dbConf.Password)
|
||||
assert.Equal("gogmagog", dbConf.Database)
|
||||
}
|
||||
|
||||
func TestMissingFile(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf, err := config.GetConf("non-existent")
|
||||
assert.Nil(conf)
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
7
main.go
7
main.go
@ -5,11 +5,16 @@ import (
|
||||
"gitea.deepak.science/deepak/gogmagog/db"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Config
|
||||
conf := config.GetConf("config")
|
||||
conf, err := config.GetConf("config")
|
||||
if err != nil {
|
||||
log.Fatal("Could not get config", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
appConf := conf.App
|
||||
port := appConf.Port
|
||||
|
Loading…
x
Reference in New Issue
Block a user