1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
package file
import (
"fmt"
"github.com/blang/semver"
"github.com/kong/deck/state"
"github.com/kong/deck/utils"
"github.com/pkg/errors"
)
// RenderConfig contains necessary information to render a correct
// KongConfig from a file.
type RenderConfig struct {
CurrentState *state.KongState
KongVersion semver.Version
}
// GetContentFromFiles reads in a file with a slice of filenames and constructs
// a state. If filename is `-`, then it will read from os.Stdin.
// If filename represents a directory, it will traverse the tree
// rooted at filename, read all the files with .yaml, .yml and .json extensions
// and generate a content after a merge of the content from all the files.
//
// It will return an error if the file representation is invalid
// or if there is any error during processing.
func GetContentFromFiles(filenames []string) (*Content, error) {
if len(filenames) == 0 {
return nil, errors.New("filename cannot be empty")
}
return getContent(filenames)
}
// Get process the fileContent and renders a RawState.
// IDs of entities are matches based on currentState.
func Get(fileContent *Content, opt RenderConfig) (*utils.KongRawState, error) {
var builder stateBuilder
// setup
builder.targetContent = fileContent
builder.currentState = opt.CurrentState
builder.kongVersion = opt.KongVersion
d, err := utils.GetKongDefaulter()
if err != nil {
return nil, errors.Wrap(err, "creating defaulter")
}
builder.defaulter = d
state, err := builder.build()
if err != nil {
return nil, errors.Wrap(err, "building state")
}
return state, nil
}
func ensureJSON(m map[string]interface{}) map[string]interface{} {
res := map[string]interface{}{}
for k, v := range m {
switch v2 := v.(type) {
case map[interface{}]interface{}:
res[fmt.Sprint(k)] = yamlToJSON(v2)
case []interface{}:
var array []interface{}
for _, element := range v2 {
switch el := element.(type) {
case map[interface{}]interface{}:
array = append(array, yamlToJSON(el))
default:
array = append(array, el)
}
}
if array != nil {
res[fmt.Sprint(k)] = array
} else {
res[fmt.Sprint(k)] = v
}
default:
res[fmt.Sprint(k)] = v
}
}
return res
}
func yamlToJSON(m map[interface{}]interface{}) map[string]interface{} {
res := map[string]interface{}{}
for k, v := range m {
switch v2 := v.(type) {
case map[interface{}]interface{}:
res[fmt.Sprint(k)] = yamlToJSON(v2)
default:
res[fmt.Sprint(k)] = v
}
}
return res
}
|