Skip to content

Commit

Permalink
added export command
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverisaac committed Apr 16, 2023
1 parent 5bae49c commit 15aac1d
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 2 deletions.
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ require (
github.com/sirupsen/logrus v1.8.1
)

require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
require (
github.com/tidwall/gjson v1.14.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)

require (
github.com/tidwall/sjson v1.2.5
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
gopkg.in/yaml.v3 v3.0.1
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
108 changes: 108 additions & 0 deletions koi/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package koi

import (
"fmt"
"io"
"strconv"
"strings"

"gopkg.in/yaml.v3"
)

func ExportCommand(input io.Reader, output io.Writer) (exitCode int, runError error) {
inputContent, err := io.ReadAll(input)
if err != nil {
return 1, fmt.Errorf("failed to read input: %w", err)
}

if string(inputContent) == "" {
return 1, fmt.Errorf("no input")
}

inputObject := make(map[string]interface{})

err = yaml.Unmarshal(inputContent, &inputObject)
if err != nil {
return 1, fmt.Errorf("failed to unmarshal input: %w", err)
}

fields_to_remove := []string{
"metadata.ownerReferences",
"metadata.resourceVersion",
"metadata.selfLink",
"metadata.uid",
"metadata.generation",
"metadata.creationTimestamp",
"metadata.managedFields",
"metadata.generateName",
"status",
"spec.nodeName",
}

for _, field := range fields_to_remove {
deletePathIfExists(inputObject, strings.Split(field, ".")...)
deletePathIfExists(inputObject, strings.Split("items.[]."+field, ".")...)
}

encoder := yaml.NewEncoder(output)
encoder.SetIndent(2)
defer encoder.Close()
err = encoder.Encode(inputObject)
if err != nil {
return 1, fmt.Errorf("failed to encode output: %w", err)
}
return 0, nil
}

func deletePathIfExists(inputObject interface{}, path ...string) {
if len(path) == 0 {
return
}
currKey := path[0]
switch inputObject.(type) {
case map[string]interface{}:
asMap := inputObject.(map[string]interface{})
if len(path) == 1 {
delete(asMap, currKey)
} else if next, ok := asMap[currKey]; ok {
if len(path) > 2 {
deletePathIfExists(next, path[1:]...)
} else {
nextKey := path[1]
switch next.(type) {
case map[string]interface{}:
deletePathIfExists(asMap[path[0]], path[1:]...)
case []interface{}:
if nextKey == "[]" {
delete(asMap, currKey)
} else {
nextInt, err := strconv.Atoi(nextKey)
if err != nil {
return
}
nextSlice := next.([]interface{})
if nextInt >= 0 && nextInt < len(nextSlice) {
nextSlice = append(nextSlice[:nextInt], nextSlice[nextInt+1:]...)
}
}
}
}
}
case []interface{}:
asSice := inputObject.([]interface{})
currKeyInt, err := strconv.Atoi(currKey)
if err != nil {
currKeyInt = -1
}
if len(path) == 1 && currKeyInt >= 0 && currKeyInt < len(asSice) {
asSice = append(asSice[:currKeyInt], asSice[currKeyInt+1:]...)
return
} else if currKeyInt >= 0 && currKeyInt < len(asSice) {
deletePathIfExists(asSice[currKeyInt], path[1:]...)
} else if currKey == "[]" {
for _, v := range asSice {
deletePathIfExists(v, path[1:]...)
}
}
}
}
Loading

0 comments on commit 15aac1d

Please sign in to comment.