mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Write json files in sink
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -103,6 +104,7 @@ type ByteReader struct {
|
||||
WrappingKind string
|
||||
|
||||
// Path indicates file path which is being read
|
||||
// empty if it is being read from stdin
|
||||
Path string
|
||||
}
|
||||
|
||||
@@ -123,12 +125,20 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) {
|
||||
index := 0
|
||||
for i := range values {
|
||||
value := values[i]
|
||||
if strings.HasSuffix(r.Path, JSONSuffix) {
|
||||
value, err = yaml.ConvertJSONToYamlString(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
if r.Path != "" {
|
||||
for _, g := range JSONMatch {
|
||||
if match, err := filepath.Match(g, filepath.Ext(r.Path)); err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
} else if match {
|
||||
value, err = yaml.ConvertJSONToYamlString(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decoder := yaml.NewDecoder(bytes.NewBufferString(value))
|
||||
node, err := r.decode(index, decoder)
|
||||
if err == io.EOF {
|
||||
|
||||
@@ -5,6 +5,7 @@ package kio
|
||||
|
||||
import (
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||
@@ -42,6 +43,9 @@ type ByteWriter struct {
|
||||
|
||||
// Sort if set, will cause ByteWriter to sort the the nodes before writing them.
|
||||
Sort bool
|
||||
|
||||
// Path is the output file path to write to
|
||||
Path string
|
||||
}
|
||||
|
||||
var _ Writer = ByteWriter{}
|
||||
@@ -90,9 +94,8 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error {
|
||||
// don't wrap the elements
|
||||
if w.WrappingKind == "" {
|
||||
for i := range nodes {
|
||||
err := encoder.Encode(nodes[i].Document())
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
if err := w.encode(encoder, nodes[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -125,7 +128,29 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error {
|
||||
for i := range nodes {
|
||||
items.Content = append(items.Content, nodes[i].YNode())
|
||||
}
|
||||
err := errors.Wrap(encoder.Encode(doc))
|
||||
rNode := yaml.RNode{}
|
||||
rNode.SetYNode(doc)
|
||||
err := w.encode(encoder, &rNode)
|
||||
yaml.UndoSerializationHacksOnNodes(nodes)
|
||||
return err
|
||||
}
|
||||
|
||||
// encode encodes the input RNode to appropriate node format based on file path extension
|
||||
func (w ByteWriter) encode(encoder *yaml.Encoder, node *yaml.RNode) error {
|
||||
for _, g := range JSONMatch {
|
||||
if match, err := filepath.Match(g, filepath.Ext(w.Path)); err != nil {
|
||||
return errors.Wrap(err)
|
||||
} else if match {
|
||||
json, err := yaml.ConvertYamlNodeToJSONString(node)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
err = encoder.Encode(json)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return encoder.Encode(node.Document())
|
||||
}
|
||||
|
||||
@@ -166,8 +166,6 @@ type LocalPackageReader struct {
|
||||
|
||||
var _ Reader = LocalPackageReader{}
|
||||
|
||||
const JSONSuffix = ".json"
|
||||
|
||||
var DefaultMatch = []string{"*.yaml", "*.yml"}
|
||||
var JSONMatch = []string{"*.json"}
|
||||
var MatchAll = append(DefaultMatch, JSONMatch...)
|
||||
|
||||
@@ -98,6 +98,7 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error {
|
||||
Writer: f,
|
||||
KeepReaderAnnotations: r.KeepReaderAnnotations,
|
||||
ClearAnnotations: r.ClearAnnotations,
|
||||
Path: outputPath,
|
||||
}
|
||||
if err = w.Write(outputFiles[path]); err != nil {
|
||||
return errors.Wrap(err)
|
||||
|
||||
Reference in New Issue
Block a user