feat: Support multiple configuration files (#396)
* Revert "Revert "feat: Support multiple configuration files" (#395)"
This reverts commit 87740e74a6
.
* feat: Properly implement support for config directory
This commit is contained in:
1
vendor/github.com/TwiN/deepmerge/.gitattributes
generated
vendored
Normal file
1
vendor/github.com/TwiN/deepmerge/.gitattributes
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
13
vendor/github.com/TwiN/deepmerge/.gitignore
generated
vendored
Normal file
13
vendor/github.com/TwiN/deepmerge/.gitignore
generated
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
# IDE
|
||||
*.iml
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
|
||||
# JS
|
||||
node_modules
|
||||
|
||||
# Go
|
||||
/vendor
|
21
vendor/github.com/TwiN/deepmerge/LICENSE
generated
vendored
Normal file
21
vendor/github.com/TwiN/deepmerge/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 TwiN
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
59
vendor/github.com/TwiN/deepmerge/README.md
generated
vendored
Normal file
59
vendor/github.com/TwiN/deepmerge/README.md
generated
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
# deepmerge
|
||||

|
||||
|
||||
Go library for deep merging YAML files.
|
||||
|
||||
|
||||
## Usage
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/TwiN/deepmerge"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dst := `
|
||||
debug: true
|
||||
client:
|
||||
insecure: true
|
||||
users:
|
||||
- id: 1
|
||||
firstName: John
|
||||
lastName: Doe
|
||||
- id: 2
|
||||
firstName: Jane
|
||||
lastName: Doe`
|
||||
src := `
|
||||
client:
|
||||
timeout: 5s
|
||||
users:
|
||||
- id: 3
|
||||
firstName: Bob
|
||||
lastName: Smith`
|
||||
|
||||
output, err := deepmerge.YAML([]byte(dst), []byte(src))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
println(string(output))
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
```yaml
|
||||
client:
|
||||
insecure: true
|
||||
timeout: 5s
|
||||
debug: true
|
||||
users:
|
||||
- firstName: John
|
||||
id: 1
|
||||
lastName: Doe
|
||||
- firstName: Jane
|
||||
id: 2
|
||||
lastName: Doe
|
||||
- firstName: Bob
|
||||
id: 3
|
||||
lastName: Smith
|
||||
```
|
83
vendor/github.com/TwiN/deepmerge/yaml.go
generated
vendored
Normal file
83
vendor/github.com/TwiN/deepmerge/yaml.go
generated
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
package deepmerge
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDeepMergeDuplicatePrimitiveKey = errors.New("error deep merging YAML files due to duplicate primitive key: only maps and slices/arrays can be merged, which means you cannot have define the same key twice for parameters that are not maps or slices/arrays")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
// PreventDuplicateKeysWithPrimitiveValue causes YAML to return an error if dst and src define the same key if
|
||||
// said key has a value with a primitive type
|
||||
// This does not apply to slices or maps. Defaults to true
|
||||
PreventDuplicateKeysWithPrimitiveValue bool
|
||||
}
|
||||
|
||||
// YAML merges the contents of src into dst
|
||||
func YAML(dst, src []byte, optionalConfig ...Config) ([]byte, error) {
|
||||
var cfg Config
|
||||
if len(optionalConfig) > 0 {
|
||||
cfg = optionalConfig[0]
|
||||
} else {
|
||||
cfg = Config{PreventDuplicateKeysWithPrimitiveValue: true}
|
||||
}
|
||||
var dstMap, srcMap map[string]interface{}
|
||||
err := yaml.Unmarshal(dst, &dstMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = yaml.Unmarshal(src, &srcMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dstMap == nil {
|
||||
dstMap = make(map[string]interface{})
|
||||
}
|
||||
if err = deepMerge(dstMap, srcMap, cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return yaml.Marshal(dstMap)
|
||||
}
|
||||
|
||||
func deepMerge(dst, src map[string]interface{}, config Config) error {
|
||||
for srcKey, srcValue := range src {
|
||||
if srcValueAsMap, ok := srcValue.(map[string]interface{}); ok { // handle maps
|
||||
if dstValue, ok := dst[srcKey]; ok {
|
||||
if dstValueAsMap, ok := dstValue.(map[string]interface{}); ok {
|
||||
err := deepMerge(dstValueAsMap, srcValueAsMap, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
dst[srcKey] = make(map[string]interface{})
|
||||
}
|
||||
err := deepMerge(dst[srcKey].(map[string]interface{}), srcValueAsMap, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if srcValueAsSlice, ok := srcValue.([]interface{}); ok { // handle slices
|
||||
if dstValue, ok := dst[srcKey]; ok {
|
||||
if dstValueAsSlice, ok := dstValue.([]interface{}); ok {
|
||||
// If both src and dst are slices, we'll copy the elements from that src slice over to the dst slice
|
||||
dst[srcKey] = append(dstValueAsSlice, srcValueAsSlice...)
|
||||
continue
|
||||
}
|
||||
}
|
||||
dst[srcKey] = srcValueAsSlice
|
||||
} else { // handle primitives
|
||||
if config.PreventDuplicateKeysWithPrimitiveValue {
|
||||
if _, ok := dst[srcKey]; ok {
|
||||
return ErrDeepMergeDuplicatePrimitiveKey
|
||||
}
|
||||
}
|
||||
dst[srcKey] = srcValue
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user