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 return load(rdr, log, true)
39 func load(rdr io.Reader, log logger, useDeprecated bool) (*arvados.Config, error) {
40 buf, err := ioutil.ReadAll(rdr)
45 // Load the config into a dummy map to get the cluster ID
46 // keys, discarding the values; then set up defaults for each
47 // cluster ID; then load the real config on top of the
50 Clusters map[string]struct{}
52 err = yaml.Unmarshal(buf, &dummy)
56 if len(dummy.Clusters) == 0 {
57 return nil, errors.New("config does not define any clusters")
60 // We can't merge deep structs here; instead, we unmarshal the
61 // default & loaded config files into generic maps, merge
62 // those, and then json-encode+decode the result into the
63 // config struct type.
64 var merged map[string]interface{}
65 for id := range dummy.Clusters {
66 var src map[string]interface{}
67 err = yaml.Unmarshal(bytes.Replace(DefaultYAML, []byte(" xxxxx:"), []byte(" "+id+":"), -1), &src)
69 return nil, fmt.Errorf("loading defaults for %s: %s", id, err)
71 err = mergo.Merge(&merged, src, mergo.WithOverride)
73 return nil, fmt.Errorf("merging defaults for %s: %s", id, err)
76 var src map[string]interface{}
77 err = yaml.Unmarshal(buf, &src)
79 return nil, fmt.Errorf("loading config data: %s", err)
81 err = mergo.Merge(&merged, src, mergo.WithOverride)
83 return nil, fmt.Errorf("merging config data: %s", err)
86 // map[string]interface{} => json => arvados.Config
87 var cfg arvados.Config
91 errEnc = json.NewEncoder(pw).Encode(merged)
94 err = json.NewDecoder(pr).Decode(&cfg)
99 return nil, fmt.Errorf("transcoding config data: %s", err)
103 err = applyDeprecatedConfig(&cfg, buf, log)
109 // Check for known mistakes
110 for id, cc := range cfg.Clusters {
111 err = checkKeyConflict(fmt.Sprintf("Clusters.%s.PostgreSQL.Connection", id), cc.PostgreSQL.Connection)
119 func checkKeyConflict(label string, m map[string]string) error {
120 saw := map[string]bool{}
122 k = strings.ToLower(k)
124 return fmt.Errorf("%s: multiple keys with tolower(key)==%q (use same case as defaults)", label, k)