1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "github.com/ghodss/yaml"
19 "github.com/imdario/mergo"
22 type logger interface {
23 Warnf(string, ...interface{})
26 func LoadFile(path string, log logger) (*arvados.Config, error) {
27 f, err := os.Open(path)
35 func Load(rdr io.Reader, log logger) (*arvados.Config, error) {
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 err = mergo.Merge(&merged, src, mergo.WithOverride)
69 return nil, fmt.Errorf("merging defaults for %s: %s", id, err)
72 var src map[string]interface{}
73 err = yaml.Unmarshal(buf, &src)
75 return nil, fmt.Errorf("loading config data: %s", err)
77 err = mergo.Merge(&merged, src, mergo.WithOverride)
79 return nil, fmt.Errorf("merging config data: %s", err)
82 // map[string]interface{} => json => arvados.Config
83 var cfg arvados.Config
87 errEnc = json.NewEncoder(pw).Encode(merged)
90 err = json.NewDecoder(pr).Decode(&cfg)
95 return nil, fmt.Errorf("transcoding config data: %s", err)
98 err = applyDeprecatedConfig(&cfg, buf, log)
102 for id, cc := range cfg.Clusters {
103 err = checkKeyConflict(fmt.Sprintf("Clusters.%s.PostgreSQL.Connection", id), cc.PostgreSQL.Connection)
111 func checkKeyConflict(label string, m map[string]string) error {
112 saw := map[string]bool{}
114 k = strings.ToLower(k)
116 return fmt.Errorf("%s: multiple keys with tolower(key)==%q (use same case as defaults)", label, k)