From 4eb82324959a24c227615416fe184c25bb3c877a Mon Sep 17 00:00:00 2001 From: Francesc Campoy Date: Fri, 16 Jul 2021 12:03:46 -0700 Subject: [PATCH] Added FileSystem field to kio.LocalPackageWriter --- kyaml/kio/pkgio_writer.go | 49 ++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/kyaml/kio/pkgio_writer.go b/kyaml/kio/pkgio_writer.go index b671512e3..a31abacb2 100644 --- a/kyaml/kio/pkgio_writer.go +++ b/kyaml/kio/pkgio_writer.go @@ -4,12 +4,14 @@ package kio import ( + "bytes" "fmt" "os" "path/filepath" "strings" "sigs.k8s.io/kustomize/kyaml/errors" + "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" "sigs.k8s.io/kustomize/kyaml/yaml" ) @@ -26,6 +28,9 @@ type LocalPackageWriter struct { // ClearAnnotations will clear annotations before writing the resources ClearAnnotations []string `yaml:"clearAnnotations,omitempty"` + + // FileSystem can be used to mock the disk file system. + FileSystem filesys.FileSystemOrOnDisk } var _ Writer = LocalPackageWriter{} @@ -36,9 +41,10 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error { return err } - if s, err := os.Stat(r.PackagePath); err != nil { - return err - } else if !s.IsDir() { + if !r.FileSystem.Exists(r.PackagePath) { + return errors.WrapPrefixf(os.ErrNotExist, "could not write to %q", r.PackagePath) + } + if !r.FileSystem.IsDir(r.PackagePath) { // if the user specified input isn't a directory, the package is the directory of the // target r.PackagePath = filepath.Dir(r.PackagePath) @@ -65,45 +71,36 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error { // validate outputs before writing any for path := range outputFiles { outputPath := filepath.Join(r.PackagePath, path) - if st, err := os.Stat(outputPath); !os.IsNotExist(err) { - if err != nil { - return errors.Wrap(err) - } - if st.IsDir() { - return fmt.Errorf("config.kubernetes.io/path cannot be a directory: %s", path) - } + if r.FileSystem.IsDir(outputPath) { + 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 { return errors.Wrap(err) } } // write files + buf := bytes.NewBuffer(nil) for path := range outputFiles { 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 { return errors.Wrap(err) } - f, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0600)) - if err != nil { + buf.Reset() + w := ByteWriter{ + Writer: buf, + KeepReaderAnnotations: r.KeepReaderAnnotations, + ClearAnnotations: r.ClearAnnotations, + } + if err = w.Write(outputFiles[path]); err != nil { return errors.Wrap(err) } - if err := func() error { - defer f.Close() - 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 { + + if err := r.FileSystem.WriteFile(outputPath, buf.Bytes()); err != nil { return errors.Wrap(err) } }