Passed
Push — master ( fa43e1...831c24 )
by Valentin
02:08
created

configuration.getType   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
package configuration
2
3
import (
4
	"fmt"
5
	"github.com/vvval/go-metadata-scanner/util"
6
	"github.com/vvval/go-metadata-scanner/util/log"
7
	"io/ioutil"
8
	"reflect"
9
)
10
11
func Load(conf Config, filename string) Config {
12
	var err error
13
14
	if util.FileExists(filename) {
15
		data, err := ioutil.ReadFile(filename)
16
		if err == nil {
17
			loadedConf, err := conf.Schema().Parse(data)
18
			if err == nil {
19
				return loadedConf.MergeDefault(conf)
20
			}
21
		}
22
	} else {
23
		log.Failure("Config load failed, file not exists", getType(conf))
24
	}
25
26
	if err != nil {
27
		log.Failure(fmt.Sprintf("Config \"%s\" read", filename), err.Error())
28
	}
29
30
	return conf
31
}
32
33
func getType(v interface{}) string {
34
	if t := reflect.TypeOf(v); t.Kind() == reflect.Ptr {
35
		return "*" + t.Elem().Name()
36
	} else {
37
		return t.Name()
38
	}
39
}
40