This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
/
apimodel_merger.go
182 lines (162 loc) · 5.4 KB
/
apimodel_merger.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package transform
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"github.com/Jeffail/gabs"
log "github.com/sirupsen/logrus"
)
// APIModelValue represents a value in the APIModel JSON file
type APIModelValue struct {
value interface{}
arrayValue bool
arrayIndex int
arrayProperty string
arrayName string
}
// MapValues converts an arraw of rwa ApiModel values (like ["masterProfile.count=4","linuxProfile.adminUsername=admin"]) to a map
func MapValues(m map[string]APIModelValue, setFlagValues []string) {
if len(setFlagValues) == 0 {
return
}
// regex to find array[index].property pattern in the key, like linuxProfile.ssh.publicKeys[0].keyData
re := regexp.MustCompile(`(.*?)\[(.*?)\](?:\.(.*?))?$`)
for _, setFlagValue := range setFlagValues {
kvpMap := parseKeyValuePairs(setFlagValue)
for key, keyValue := range kvpMap {
flagValue := APIModelValue{}
// try to parse the value as integer, bool or fallback to string
if keyValueAsInteger, err := strconv.ParseInt(keyValue, 10, 64); err == nil {
flagValue.value = keyValueAsInteger
} else if keyValueAsBool, err := strconv.ParseBool(keyValue); err == nil {
flagValue.value = keyValueAsBool
} else {
flagValue.value = keyValue
}
// check if the key is an array property
keyArrayMatch := re.FindStringSubmatch(key)
// it's an array
if keyArrayMatch != nil {
i, err := strconv.ParseInt(keyArrayMatch[2], 10, 32)
if err != nil {
log.Warnln(fmt.Sprintf("array index is not specified for property %s", key))
} else {
arrayIndex := int(i)
flagValue.arrayValue = true
flagValue.arrayName = keyArrayMatch[1]
flagValue.arrayIndex = arrayIndex
flagValue.arrayProperty = keyArrayMatch[3]
m[key] = flagValue
}
} else {
m[key] = flagValue
}
}
}
}
// MergeValuesWithAPIModel takes the path to an ApiModel JSON file, loads it and merges it with the values in the map to another temp file
func MergeValuesWithAPIModel(apiModelPath string, m map[string]APIModelValue) (string, error) {
// load the apiModel file from path
fileContent, err := ioutil.ReadFile(apiModelPath)
if err != nil {
return "", err
}
// parse the json from file content
jsonObj, err := gabs.ParseJSON(fileContent)
if err != nil {
return "", err
}
// update api model definition with each value in the map
for key, flagValue := range m {
// working on an array
if flagValue.arrayValue {
log.Debugln(fmt.Sprintf("--set flag array value detected. Name: %s, Index: %d, PropertyName: %s", flagValue.arrayName, flagValue.arrayIndex, flagValue.arrayProperty))
arrayPath := fmt.Sprint("properties.", flagValue.arrayName)
arrayValue := jsonObj.Path(arrayPath)
if flagValue.arrayProperty != "" {
arrayValue.Index(flagValue.arrayIndex).SetP(flagValue.value, flagValue.arrayProperty)
} else {
count, _ := arrayValue.ArrayCount()
for i := count; i <= flagValue.arrayIndex; i++ {
jsonObj.ArrayAppendP(nil, arrayPath)
}
arrayValue = jsonObj.Path(arrayPath)
arrayValue.SetIndex(flagValue.value, flagValue.arrayIndex)
}
} else {
jsonObj.SetP(flagValue.value, fmt.Sprint("properties.", key))
}
}
// generate a new file
tmpFile, err := ioutil.TempFile("", "mergedApiModel")
if err != nil {
return "", err
}
tmpFileName := tmpFile.Name()
err = ioutil.WriteFile(tmpFileName, []byte(jsonObj.String()), os.ModeAppend)
if err != nil {
return "", err
}
return tmpFileName, nil
}
func parseKeyValuePairs(literal string) map[string]string {
log.Debugln(fmt.Sprintf("parsing --set flag key/value pairs from %s", literal))
inQuoteLiteral := false
inDblQuoteLiteral := false
inKey := true
kvpMap := map[string]string{}
currentKey := ""
currentValue := ""
for _, literalChar := range literal {
switch literalChar {
case '\'': // if we hit a ' char
if !inQuoteLiteral && !inDblQuoteLiteral { // and we are not already in a literal
inQuoteLiteral = true // start a new ' delimited literal value
inKey = false
} else if inQuoteLiteral { // we already are in a ' delimited literal value
inQuoteLiteral = false // stop it
inKey = true
}
case '"': // if we hit a " char
if !inDblQuoteLiteral && !inQuoteLiteral { // and we are not already in a literal
inDblQuoteLiteral = true // start a new " delimited literal value
inKey = false
} else if inDblQuoteLiteral { // we already are in a " delimited literal value
inDblQuoteLiteral = false // stop it
inKey = true
}
case ',': // if we hit a , char
if inQuoteLiteral || inDblQuoteLiteral { // we are in a literal
currentValue += string(literalChar)
} else {
log.Debugln(fmt.Sprintf("new key/value parsed: %s = %s", currentKey, currentValue))
kvpMap[currentKey] = currentValue
currentKey = ""
currentValue = ""
inKey = true
}
case '=': // if we hit a = char
if inQuoteLiteral || inDblQuoteLiteral || !inKey { // we are in a literal / value
currentValue += string(literalChar)
} else {
inKey = false
}
default: // we hit any other char
if inKey {
currentKey += string(literalChar)
} else {
currentValue += string(literalChar)
}
}
}
// push latest literal
if currentKey != "" {
log.Debugln(fmt.Sprintf("new key/value parsed: %s = %s", currentKey, currentValue))
kvpMap[currentKey] = currentValue
}
return kvpMap
}