...

Source file src/sigs.k8s.io/kustomize/kyaml/filesys/filesystem.go

Documentation: sigs.k8s.io/kustomize/kyaml/filesys

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package filesys
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  
    10  	"sigs.k8s.io/kustomize/kyaml/errors"
    11  )
    12  
    13  const (
    14  	Separator = string(filepath.Separator)
    15  	SelfDir   = "."
    16  	ParentDir = ".."
    17  )
    18  
    19  // FileSystem groups basic os filesystem methods.
    20  // It's supposed be functional subset of https://golang.org/pkg/os
    21  type FileSystem interface {
    22  
    23  	// Create a file.
    24  	Create(path string) (File, error)
    25  
    26  	// MkDir makes a directory.
    27  	Mkdir(path string) error
    28  
    29  	// MkDirAll makes a directory path, creating intervening directories.
    30  	MkdirAll(path string) error
    31  
    32  	// RemoveAll removes path and any children it contains.
    33  	RemoveAll(path string) error
    34  
    35  	// Open opens the named file for reading.
    36  	Open(path string) (File, error)
    37  
    38  	// IsDir returns true if the path is a directory.
    39  	IsDir(path string) bool
    40  
    41  	// ReadDir returns a list of files and directories within a directory.
    42  	ReadDir(path string) ([]string, error)
    43  
    44  	// CleanedAbs converts the given path into a
    45  	// directory and a file name, where the directory
    46  	// is represented as a ConfirmedDir and all that implies.
    47  	// If the entire path is a directory, the file component
    48  	// is an empty string.
    49  	CleanedAbs(path string) (ConfirmedDir, string, error)
    50  
    51  	// Exists is true if the path exists in the file system.
    52  	Exists(path string) bool
    53  
    54  	// Glob returns the list of matching files,
    55  	// emulating https://golang.org/pkg/path/filepath/#Glob
    56  	Glob(pattern string) ([]string, error)
    57  
    58  	// ReadFile returns the contents of the file at the given path.
    59  	ReadFile(path string) ([]byte, error)
    60  
    61  	// WriteFile writes the data to a file at the given path,
    62  	// overwriting anything that's already there.
    63  	WriteFile(path string, data []byte) error
    64  
    65  	// Walk walks the file system with the given WalkFunc.
    66  	Walk(path string, walkFn filepath.WalkFunc) error
    67  }
    68  
    69  // ConfirmDir returns an error if the user-specified path is not an existing directory on fSys.
    70  // Otherwise, ConfirmDir returns path, which can be relative, as a ConfirmedDir and all that implies.
    71  func ConfirmDir(fSys FileSystem, path string) (ConfirmedDir, error) {
    72  	if path == "" {
    73  		return "", errors.Errorf("directory path cannot be empty")
    74  	}
    75  
    76  	d, f, err := fSys.CleanedAbs(path)
    77  	if err != nil {
    78  		return "", errors.WrapPrefixf(err, "not a valid directory")
    79  	}
    80  	if f != "" {
    81  		return "", errors.WrapPrefixf(errors.Errorf("file is not directory"), fmt.Sprintf("'%s'", path))
    82  	}
    83  	return d, nil
    84  }
    85  
    86  // FileSystemOrOnDisk satisfies the FileSystem interface by forwarding
    87  // all of its method calls to the given FileSystem whenever it's not nil.
    88  // If it's nil, the call is forwarded to the OS's underlying file system.
    89  type FileSystemOrOnDisk struct {
    90  	FileSystem FileSystem
    91  }
    92  
    93  // Set sets the given FileSystem as the target for all the FileSystem method calls.
    94  func (fs *FileSystemOrOnDisk) Set(f FileSystem) { fs.FileSystem = f }
    95  
    96  func (fs FileSystemOrOnDisk) fs() FileSystem {
    97  	if fs.FileSystem != nil {
    98  		return fs.FileSystem
    99  	}
   100  	return MakeFsOnDisk()
   101  }
   102  
   103  func (fs FileSystemOrOnDisk) Create(path string) (File, error) {
   104  	return fs.fs().Create(path)
   105  }
   106  
   107  func (fs FileSystemOrOnDisk) Mkdir(path string) error {
   108  	return fs.fs().Mkdir(path)
   109  }
   110  
   111  func (fs FileSystemOrOnDisk) MkdirAll(path string) error {
   112  	return fs.fs().MkdirAll(path)
   113  }
   114  
   115  func (fs FileSystemOrOnDisk) RemoveAll(path string) error {
   116  	return fs.fs().RemoveAll(path)
   117  }
   118  
   119  func (fs FileSystemOrOnDisk) Open(path string) (File, error) {
   120  	return fs.fs().Open(path)
   121  }
   122  
   123  func (fs FileSystemOrOnDisk) IsDir(path string) bool {
   124  	return fs.fs().IsDir(path)
   125  }
   126  
   127  func (fs FileSystemOrOnDisk) ReadDir(path string) ([]string, error) {
   128  	return fs.fs().ReadDir(path)
   129  }
   130  
   131  func (fs FileSystemOrOnDisk) CleanedAbs(path string) (ConfirmedDir, string, error) {
   132  	return fs.fs().CleanedAbs(path)
   133  }
   134  
   135  func (fs FileSystemOrOnDisk) Exists(path string) bool {
   136  	return fs.fs().Exists(path)
   137  }
   138  
   139  func (fs FileSystemOrOnDisk) Glob(pattern string) ([]string, error) {
   140  	return fs.fs().Glob(pattern)
   141  }
   142  
   143  func (fs FileSystemOrOnDisk) ReadFile(path string) ([]byte, error) {
   144  	return fs.fs().ReadFile(path)
   145  }
   146  
   147  func (fs FileSystemOrOnDisk) WriteFile(path string, data []byte) error {
   148  	return fs.fs().WriteFile(path, data)
   149  }
   150  
   151  func (fs FileSystemOrOnDisk) Walk(path string, walkFn filepath.WalkFunc) error {
   152  	return fs.fs().Walk(path, walkFn)
   153  }
   154  

View as plain text