1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "github.com/ghodss/yaml"
18 "github.com/imdario/mergo"
21 type logger interface {
22 Warnf(string, ...interface{})
25 func LoadFile(path string, log logger) (*arvados.Config, error) {
26 f, err := os.Open(path)
34 func Load(rdr io.Reader, log logger) (*arvados.Config, error) {
35 var cfg arvados.Config
36 buf, err := ioutil.ReadAll(rdr)
41 // Load the config into a dummy map to get the cluster ID
42 // keys, discarding the values; then set up defaults for each
43 // cluster ID; then load the real config on top of the
46 Clusters map[string]struct{}
48 err = yaml.Unmarshal(buf, &dummy)
52 if len(dummy.Clusters) == 0 {
53 return nil, errors.New("config does not define any clusters")
56 // We can't merge deep structs here; instead, we unmarshal the
57 // default & loaded config files into generic maps, merge
58 // those, and then json-encode+decode the result into the
59 // config struct type.
60 var merged map[string]interface{}
61 for id := range dummy.Clusters {
62 var src map[string]interface{}
63 err = yaml.Unmarshal(bytes.Replace(DefaultYAML, []byte(" xxxxx:"), []byte(" "+id+":"), -1), &src)
65 return nil, fmt.Errorf("loading defaults for %s: %s", id, err)
67 mergo.Merge(&merged, src, mergo.WithOverride)
69 var src map[string]interface{}
70 err = yaml.Unmarshal(buf, &src)
72 return nil, fmt.Errorf("loading config data: %s", err)
74 mergo.Merge(&merged, src, mergo.WithOverride)
79 errEnc = json.NewEncoder(pw).Encode(merged)
82 err = json.NewDecoder(pr).Decode(&cfg)
87 return nil, fmt.Errorf("transcoding config data: %s", err)
90 err = applyDeprecatedConfig(&cfg, buf, log)