Skip to content

Commit

Permalink
feat(patch): append to array entries (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske authored Jul 18, 2023
1 parent 5d19fe6 commit d19b622
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
20 changes: 15 additions & 5 deletions cmd/file_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func executePatch(cmd *cobra.Command, args []string) error {
{
var err error
valuesPatch.SelectorSources = cmdPatchSelectors
valuesPatch.Values, valuesPatch.Remove, err = patch.ValidateValuesFlags(cmdPatchValues)
valuesPatch.ObjValues, valuesPatch.Remove, err = patch.ValidateValuesFlags(cmdPatchValues)
if err != nil {
return fmt.Errorf("failed parsing '--value' entry; %w", err)
}
Expand All @@ -53,11 +53,14 @@ func executePatch(cmd *cobra.Command, args []string) error {
trackInfo := deckformat.HistoryNewEntry("patch")
trackInfo["input"] = cmdPatchInputFilename
trackInfo["output"] = cmdPatchOutputFilename
if len(valuesPatch.Values) != 0 || len(valuesPatch.Remove) != 0 {
if (len(valuesPatch.ObjValues) + len(valuesPatch.Remove) + len(valuesPatch.ArrValues)) > 0 {
trackInfo["selector"] = valuesPatch.SelectorSources
}
if len(valuesPatch.Values) != 0 {
trackInfo["values"] = valuesPatch.Values
if len(valuesPatch.ObjValues) != 0 {
trackInfo["values"] = valuesPatch.ObjValues
}
if len(valuesPatch.ArrValues) != 0 {
trackInfo["values"] = valuesPatch.ArrValues
}
if len(valuesPatch.Remove) != 0 {
trackInfo["remove"] = valuesPatch.Remove
Expand All @@ -75,7 +78,7 @@ func executePatch(cmd *cobra.Command, args []string) error {

yamlNode := jsonbasics.ConvertToYamlNode(data)

if (len(valuesPatch.Values) + len(valuesPatch.Remove)) > 0 {
if (len(valuesPatch.ObjValues) + len(valuesPatch.Remove) + len(valuesPatch.ArrValues)) > 0 {
// apply selector + value flags
logbasics.Debug("applying value-flags")
err = valuesPatch.ApplyToNodes(yamlNode)
Expand Down Expand Up @@ -120,6 +123,8 @@ When using '--selector' and '--values', the items are selected by the 'selector'
which is a JSONpath query. From the array of nodes found, only the objects are updated.
The 'values' are applied on each of the JSONObjects returned by the 'selector'.
Objects:
The value must be a valid JSON snippet, so use single/double quotes
appropriately. If the value is empty, the field is removed from the object.
Expand Down Expand Up @@ -147,6 +152,11 @@ patches that are applied in order:
}
]
}
Arrays:
If the 'values' object instead is an array, then any arrays returned by the selectors
will get the 'values' appended to them.
`,
RunE: executePatch,
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/hexops/gotextdiff v1.0.3
github.com/imdario/mergo v0.3.16
github.com/kong/go-apiops v0.1.18
github.com/kong/go-apiops v0.1.19
github.com/kong/go-kong v0.44.0
github.com/mitchellh/go-homedir v1.1.0
github.com/shirou/gopsutil/v3 v3.23.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kong/go-apiops v0.1.18 h1:Bk6m3B9m8R0iykEyzpQa5rCHkhFGUpaXixFmaw/W3PM=
github.com/kong/go-apiops v0.1.18/go.mod h1:3P9DBGLcU6Gp4wo8z4xohcg8PMutBAknc54pLZoQtDs=
github.com/kong/go-apiops v0.1.19 h1:mm1XO7tKh5f71dcdXZZWSSh0bpHMd6KOZ6qCNGZ/ftU=
github.com/kong/go-apiops v0.1.19/go.mod h1:3P9DBGLcU6Gp4wo8z4xohcg8PMutBAknc54pLZoQtDs=
github.com/kong/go-kong v0.44.0 h1:1x3w/TYdJjIZ6c1j9HiYP8755c923XN2O6j3kEaUkTA=
github.com/kong/go-kong v0.44.0/go.mod h1:41Sot1N/n8UHBp+gE/6nOw3vuzoHbhMSyU/zOS7VzPE=
github.com/kong/semver/v4 v4.0.1 h1:DIcNR8W3gfx0KabFBADPalxxsp+q/5COwIFkkhrFQ2Y=
Expand Down

0 comments on commit d19b622

Please sign in to comment.