mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Added FileSystem field to kio.LocalPackageWriter
This commit is contained in:
@@ -4,12 +4,14 @@
|
|||||||
package kio
|
package kio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
@@ -26,6 +28,9 @@ type LocalPackageWriter struct {
|
|||||||
|
|
||||||
// ClearAnnotations will clear annotations before writing the resources
|
// ClearAnnotations will clear annotations before writing the resources
|
||||||
ClearAnnotations []string `yaml:"clearAnnotations,omitempty"`
|
ClearAnnotations []string `yaml:"clearAnnotations,omitempty"`
|
||||||
|
|
||||||
|
// FileSystem can be used to mock the disk file system.
|
||||||
|
FileSystem filesys.FileSystemOrOnDisk
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Writer = LocalPackageWriter{}
|
var _ Writer = LocalPackageWriter{}
|
||||||
@@ -36,9 +41,10 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if s, err := os.Stat(r.PackagePath); err != nil {
|
if !r.FileSystem.Exists(r.PackagePath) {
|
||||||
return err
|
return errors.WrapPrefixf(os.ErrNotExist, "could not write to %q", r.PackagePath)
|
||||||
} else if !s.IsDir() {
|
}
|
||||||
|
if !r.FileSystem.IsDir(r.PackagePath) {
|
||||||
// if the user specified input isn't a directory, the package is the directory of the
|
// if the user specified input isn't a directory, the package is the directory of the
|
||||||
// target
|
// target
|
||||||
r.PackagePath = filepath.Dir(r.PackagePath)
|
r.PackagePath = filepath.Dir(r.PackagePath)
|
||||||
@@ -65,45 +71,36 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error {
|
|||||||
// validate outputs before writing any
|
// validate outputs before writing any
|
||||||
for path := range outputFiles {
|
for path := range outputFiles {
|
||||||
outputPath := filepath.Join(r.PackagePath, path)
|
outputPath := filepath.Join(r.PackagePath, path)
|
||||||
if st, err := os.Stat(outputPath); !os.IsNotExist(err) {
|
if r.FileSystem.IsDir(outputPath) {
|
||||||
if err != nil {
|
return fmt.Errorf("config.kubernetes.io/path cannot be a directory: %s", path)
|
||||||
return errors.Wrap(err)
|
|
||||||
}
|
|
||||||
if st.IsDir() {
|
|
||||||
return fmt.Errorf("config.kubernetes.io/path cannot be a directory: %s", path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.MkdirAll(filepath.Dir(outputPath), 0700)
|
err = r.FileSystem.MkdirAll(filepath.Dir(outputPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write files
|
// write files
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
for path := range outputFiles {
|
for path := range outputFiles {
|
||||||
outputPath := filepath.Join(r.PackagePath, path)
|
outputPath := filepath.Join(r.PackagePath, path)
|
||||||
err = os.MkdirAll(filepath.Dir(filepath.Join(r.PackagePath, path)), 0700)
|
err = r.FileSystem.MkdirAll(filepath.Dir(filepath.Join(r.PackagePath, path)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0600))
|
buf.Reset()
|
||||||
if err != nil {
|
w := ByteWriter{
|
||||||
|
Writer: buf,
|
||||||
|
KeepReaderAnnotations: r.KeepReaderAnnotations,
|
||||||
|
ClearAnnotations: r.ClearAnnotations,
|
||||||
|
}
|
||||||
|
if err = w.Write(outputFiles[path]); err != nil {
|
||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
}
|
}
|
||||||
if err := func() error {
|
|
||||||
defer f.Close()
|
if err := r.FileSystem.WriteFile(outputPath, buf.Bytes()); err != nil {
|
||||||
w := ByteWriter{
|
|
||||||
Writer: f,
|
|
||||||
KeepReaderAnnotations: r.KeepReaderAnnotations,
|
|
||||||
ClearAnnotations: r.ClearAnnotations,
|
|
||||||
}
|
|
||||||
if err = w.Write(outputFiles[path]); err != nil {
|
|
||||||
return errors.Wrap(err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}(); err != nil {
|
|
||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user