/
helpers.go
41 lines (33 loc) · 1.08 KB
/
helpers.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
package kubenav
import (
"encoding/json"
"github.com/wI2L/jsondiff"
"sigs.k8s.io/yaml"
)
// PrettifyYAML takes a json string as argument and returns a prettified yaml
// string. For that we have to unmarshal the json string into a
// map[string]interface{} which we can then marshal to the prettified yaml
// string.
func PrettifyYAML(jsonStr string) (string, error) {
var jsonObj map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &jsonObj)
if err != nil {
return "", err
}
yamlStr, err := yaml.Marshal(jsonObj)
if err != nil {
return "", err
}
return string(yamlStr), nil
}
// CreateJSONPatch creates a path for two given json strings. This is needed
// when a user edits a resource, where the source argument is the manifest of
// the current resource and the target is the edited manifest. The returned
// patch can then be send to the Kubernetes API to edit the resource.
func CreateJSONPatch(source, target string) (string, error) {
patch, err := jsondiff.CompareJSON([]byte(source), []byte(target))
if err != nil {
return "", err
}
return patch.String(), nil
}