mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Move demandDirectoryRoot into kyaml/filesys (#4677)
* Move demandDirectoryRoot into kyaml/filesys * Make root directory platform-agnostic Support windows root directory. Dogfood own error package. * Use cleaner windows support implementation * Address feedback * Address feedback x2 * Re-apply go.sum changes to avoid CI errors
This commit is contained in:
@@ -4,7 +4,10 @@
|
||||
package filesys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -16,40 +19,70 @@ const (
|
||||
// FileSystem groups basic os filesystem methods.
|
||||
// It's supposed be functional subset of https://golang.org/pkg/os
|
||||
type FileSystem interface {
|
||||
|
||||
// Create a file.
|
||||
Create(path string) (File, error)
|
||||
|
||||
// MkDir makes a directory.
|
||||
Mkdir(path string) error
|
||||
|
||||
// MkDirAll makes a directory path, creating intervening directories.
|
||||
MkdirAll(path string) error
|
||||
|
||||
// RemoveAll removes path and any children it contains.
|
||||
RemoveAll(path string) error
|
||||
|
||||
// Open opens the named file for reading.
|
||||
Open(path string) (File, error)
|
||||
|
||||
// IsDir returns true if the path is a directory.
|
||||
IsDir(path string) bool
|
||||
|
||||
// ReadDir returns a list of files and directories within a directory.
|
||||
ReadDir(path string) ([]string, error)
|
||||
|
||||
// CleanedAbs converts the given path into a
|
||||
// directory and a file name, where the directory
|
||||
// is represented as a ConfirmedDir and all that implies.
|
||||
// If the entire path is a directory, the file component
|
||||
// is an empty string.
|
||||
CleanedAbs(path string) (ConfirmedDir, string, error)
|
||||
|
||||
// Exists is true if the path exists in the file system.
|
||||
Exists(path string) bool
|
||||
|
||||
// Glob returns the list of matching files,
|
||||
// emulating https://golang.org/pkg/path/filepath/#Glob
|
||||
Glob(pattern string) ([]string, error)
|
||||
|
||||
// ReadFile returns the contents of the file at the given path.
|
||||
ReadFile(path string) ([]byte, error)
|
||||
|
||||
// WriteFile writes the data to a file at the given path,
|
||||
// overwriting anything that's already there.
|
||||
WriteFile(path string, data []byte) error
|
||||
|
||||
// Walk walks the file system with the given WalkFunc.
|
||||
Walk(path string, walkFn filepath.WalkFunc) error
|
||||
}
|
||||
|
||||
// ConfirmDir returns an error if the user-specified path is not an existing directory on fSys.
|
||||
// Otherwise, ConfirmDir returns path, which can be relative, as a ConfirmedDir and all that implies.
|
||||
func ConfirmDir(fSys FileSystem, path string) (ConfirmedDir, error) {
|
||||
if path == "" {
|
||||
return "", errors.Errorf("directory path cannot be empty")
|
||||
}
|
||||
|
||||
d, f, err := fSys.CleanedAbs(path)
|
||||
if err != nil {
|
||||
return "", errors.WrapPrefixf(err, "not a valid directory")
|
||||
}
|
||||
if f != "" {
|
||||
return "", errors.WrapPrefixf(errors.Errorf("file is not directory"), fmt.Sprintf("'%s'", path))
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// FileSystemOrOnDisk satisfies the FileSystem interface by forwarding
|
||||
// all of its method calls to the given FileSystem whenever it's not nil.
|
||||
// If it's nil, the call is forwarded to the OS's underlying file system.
|
||||
|
||||
Reference in New Issue
Block a user