Added FileSystem field to kio.LocalPackageWriter

This commit is contained in:
Francesc Campoy
2021-07-16 12:03:46 -07:00
parent 6c4e8019f8
commit 4eb8232495

View File

@@ -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 errors.Wrap(err)
}
if st.IsDir() {
return fmt.Errorf("config.kubernetes.io/path cannot be a directory: %s", path) 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 {
return errors.Wrap(err)
}
if err := func() error {
defer f.Close()
w := ByteWriter{ w := ByteWriter{
Writer: f, Writer: buf,
KeepReaderAnnotations: r.KeepReaderAnnotations, KeepReaderAnnotations: r.KeepReaderAnnotations,
ClearAnnotations: r.ClearAnnotations, ClearAnnotations: r.ClearAnnotations,
} }
if err = w.Write(outputFiles[path]); err != nil { if err = w.Write(outputFiles[path]); err != nil {
return errors.Wrap(err) return errors.Wrap(err)
} }
return nil
}(); err != nil { if err := r.FileSystem.WriteFile(outputPath, buf.Bytes()); err != nil {
return errors.Wrap(err) return errors.Wrap(err)
} }
} }