MCPcopy Create free account
hub / github.com/quay/clair / LoadConfig

Function LoadConfig

cmd/config.go:40–139  ·  view source on GitHub ↗

LoadConfig loads the named config file or reports an error. JSON and YAML formatted files are supported, as determined by the file extension ("json" or "yaml" -- "yml" is not supported). If a directory suffixed with ".d" exists (e.g. a file "config.json" and a directory "config.json.d"), then all f

(cfg *config.Config, name string, strict bool)

Source from the content-addressed store, hash-verified

38// error, or runs the full routine and returns all accumulated errors at the
39// end.
40func LoadConfig(cfg *config.Config, name string, strict bool) error {
41 // This function would probably benefit from some logging, but the logging
42 // configuration is specified _inside_ the configuration, so it's hard to
43 // say what should be done here.
44 name = filepath.Clean(name)
45 ext := filepath.Ext(name)
46 switch ext {
47 case ".yaml": // OK
48 case ".json": // OK
49 default:
50 return fmt.Errorf("unknown config kind %q", ext)
51 }
52 var errs []error
53
54 b, err := loadAsJSON(name)
55 if err != nil {
56 if strict {
57 return err
58 }
59 errs = append(errs, err)
60 }
61 dropinDir := name + ".d"
62 err = filepath.WalkDir(dropinDir, func(path string, d fs.DirEntry, err error) error {
63 switch {
64 case path == dropinDir:
65 return nil
66 case !errors.Is(err, nil):
67 return fmt.Errorf("error walking filesystem: %w", err)
68 case d.IsDir():
69 return fs.SkipDir
70 }
71 // After this, make sure everything assigns errors to "err" so that the
72 // non-strict behavior works.
73
74 var doc []byte
75 switch dext := filepath.Ext(path); {
76 case dext == ext:
77 doc, err = loadAsJSON(path)
78 if err != nil {
79 break
80 }
81 b, err = jsonpatch.MergePatch(b, doc)
82 if err != nil {
83 err = fmt.Errorf("error merging drop-in %q: %w", path, err)
84 break
85 }
86 case dext == ext+"-patch":
87 doc, err = loadAsJSON(path)
88 if err != nil {
89 break
90 }
91 var p jsonpatch.Patch
92 p, err = jsonpatch.DecodePatch(doc)
93 if err != nil {
94 err = fmt.Errorf("bad patch %q: %w", path, err)
95 break
96 }
97 b, err = p.Apply(b)

Callers 4

SetClairConfigMethod · 0.92
TestLoadConfigFunction · 0.92
mainFunction · 0.92
loadConfigFunction · 0.92

Calls 2

loadAsJSONFunction · 0.85
ErrorMethod · 0.45

Tested by 1

TestLoadConfigFunction · 0.74