|
| 1 | +// Copyright (C) 2017-present Arctic Ice Studio <[email protected]> |
| 2 | +// Copyright (C) 2017-present Sven Greb <[email protected]> |
| 3 | +// |
| 4 | +// Project: snowsaw |
| 5 | +// Repository: https://github.com/arcticicestudio/snowsaw |
| 6 | +// License: MIT |
| 7 | + |
| 8 | +// Author: Arctic Ice Studio <[email protected]> |
| 9 | +// Author: Sven Greb <[email protected]> |
| 10 | +// Since: 0.4.0 |
| 11 | + |
| 12 | +// Package builder provides methods to load and merge configuration files using the builder design pattern. |
| 13 | +package builder |
| 14 | + |
| 15 | +import ( |
| 16 | + "fmt" |
| 17 | + "io/ioutil" |
| 18 | + "path/filepath" |
| 19 | + "reflect" |
| 20 | + |
| 21 | + "github.com/imdario/mergo" |
| 22 | + |
| 23 | + "github.com/arcticicestudio/snowsaw/internal/util" |
| 24 | + "github.com/arcticicestudio/snowsaw/pkg/config/encoder" |
| 25 | + "github.com/arcticicestudio/snowsaw/pkg/config/source/file" |
| 26 | + "github.com/arcticicestudio/snowsaw/pkg/prt" |
| 27 | +) |
| 28 | + |
| 29 | +// builder contains the current configuration building state. |
| 30 | +type builder struct { |
| 31 | + Files []*file.File |
| 32 | +} |
| 33 | + |
| 34 | +// Load tries to load all given configuration files. |
| 35 | +// It checks if the path is valid and exists, tries to assign a matching encoder.Encoder based on the file extension and |
| 36 | +// returns a pointer to a builder to chain and pass the loaded files to the Merge function. |
| 37 | +func Load(files ...*file.File) *builder { |
| 38 | + s := &builder{Files: []*file.File{}} |
| 39 | + |
| 40 | + for _, f := range files { |
| 41 | + // Convert to absolute path and check if file exists, otherwise ignore and check next. |
| 42 | + f.Path, _ = util.AbsPath(f.Path) |
| 43 | + if exists, _ := util.FileExists(f.Path); !exists { |
| 44 | + prt.Debugf("Ignoring non-existent configuration file: %s", f.Path) |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + // Find matching encoder by file extension if not already set. |
| 49 | + if f.Encoder == nil { |
| 50 | + fileExt := filepath.Ext(f.Path) |
| 51 | + if len(fileExt) <= 1 { |
| 52 | + prt.Debugf("Ignoring configuration file without supported extension: %s", f.Path) |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // Strip dot character separating the file name and extension. |
| 57 | + fileExt = fileExt[1:] |
| 58 | + |
| 59 | + // Only add files with supported encoders. |
| 60 | + for ext, enc := range encoder.ExtensionMapping { |
| 61 | + if ext == fileExt { |
| 62 | + f.Encoder = enc |
| 63 | + s.Files = append(s.Files, f) |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + s.Files = append(s.Files, f) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return s |
| 73 | +} |
| 74 | + |
| 75 | +// Into accepts a configuration struct pointer and populates it with the current config state. |
| 76 | +func (s *builder) Into(c interface{}) error { |
| 77 | + base := reflect.New(reflect.TypeOf(c).Elem()).Interface() |
| 78 | + |
| 79 | + for _, f := range s.Files { |
| 80 | + content, err := ioutil.ReadFile(f.Path) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + raw := base |
| 86 | + // Decode the read content using the assigned encoder. |
| 87 | + if encErr := f.Encoder.Decode(content, &raw); encErr != nil { |
| 88 | + return fmt.Errorf("%s\n%v", f.Path, err) |
| 89 | + } |
| 90 | + |
| 91 | + // Merge decoded data into given base configuration state. |
| 92 | + if encErr := mergo.Merge(c, raw, mergo.WithAppendSlice, mergo.WithOverride); encErr != nil { |
| 93 | + return fmt.Errorf("%s\n%v", f.Path, err) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments