Support export yaml with indent (#1219)

This commit is contained in:
Hang Yan
2019-12-28 12:23:03 +08:00
committed by GitHub
parent f272e7fb00
commit 88a3a27ca8
20 changed files with 11201 additions and 38 deletions
+2
View File
@@ -63,6 +63,8 @@ type ConvertOptions struct {
IsReplicaSetFlag bool
IsDeploymentConfigFlag bool
IsNamespaceFlag bool
YAMLIndent int
}
// ServiceConfig holds the basic struct of a container
+45 -5
View File
@@ -30,11 +30,11 @@ import (
"text/template"
"time"
"github.com/ghodss/yaml"
"github.com/joho/godotenv"
"github.com/kubernetes/kompose/pkg/kobject"
"github.com/kubernetes/kompose/pkg/transformer"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
@@ -194,7 +194,7 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
if err != nil {
return err
}
data, err := marshal(convertedList, opt.GenerateJSON)
data, err := marshal(convertedList, opt.GenerateJSON, opt.YAMLIndent)
if err != nil {
return fmt.Errorf("error in marshalling the List: %v", err)
}
@@ -220,7 +220,7 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
if err != nil {
return err
}
data, err := marshal(versionedObject, opt.GenerateJSON)
data, err := marshal(versionedObject, opt.GenerateJSON, opt.YAMLIndent)
if err != nil {
return err
}
@@ -266,12 +266,12 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
}
// marshal object runtime.Object and return byte array
func marshal(obj runtime.Object, jsonFormat bool) (data []byte, err error) {
func marshal(obj runtime.Object, jsonFormat bool, indent int) (data []byte, err error) {
// convert data to yaml or json
if jsonFormat {
data, err = json.MarshalIndent(obj, "", " ")
} else {
data, err = yaml.Marshal(obj)
data, err = marshalWithIndent(obj, indent)
}
if err != nil {
data = nil
@@ -279,6 +279,46 @@ func marshal(obj runtime.Object, jsonFormat bool) (data []byte, err error) {
return
}
// Convert JSON to YAML.
func jsonToYaml(j []byte, spaces int) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal(j, &jsonObj)
if err != nil {
return nil, err
}
var b bytes.Buffer
encoder := yaml.NewEncoder(&b)
encoder.SetIndent(spaces)
if err := encoder.Encode(jsonObj); err != nil {
return nil, err
}
return b.Bytes(), nil
// Marshal this object into YAML.
// return yaml.Marshal(jsonObj)
}
func marshalWithIndent(o interface{}, indent int) ([]byte, error) {
j, err := json.Marshal(o)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %s", err.Error())
}
y, err := jsonToYaml(j, indent)
if err != nil {
return nil, fmt.Errorf("error converting JSON to YAML: %s", err.Error())
}
return y, nil
}
// Convert object to versioned object
// if groupVersion is empty (unversioned.GroupVersion{}), use version from original object (obj)
func convertToVersion(obj runtime.Object, groupVersion unversioned.GroupVersion) (runtime.Object, error) {