| Total Lines | 37 |
| Duplicated Lines | 0 % |
| Changes | 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 | } |
||
| 40 |