...

Source file src/edge-infra.dev/pkg/k8s/kustomize/fs.go

Documentation: edge-infra.dev/pkg/k8s/kustomize

     1  package kustomize
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/fs"
     7  	"path/filepath"
     8  
     9  	"sigs.k8s.io/kustomize/kyaml/filesys"
    10  )
    11  
    12  // ErrReadOnly is returned when write operations are called against [FS]
    13  var ErrReadOnly = fmt.Errorf("read-only FS")
    14  
    15  // File is a read-only implementation of kyaml/filesys.File to used to impelement
    16  // a read-only Kustomize FS that can be instantiated form [fs.FS] implementations
    17  // such as [embed.FS]. Calls to [File.Write] will return [ErrReadOnly].
    18  type File struct {
    19  	fs.File
    20  }
    21  
    22  var _ filesys.File = &File{}
    23  
    24  // Write returns [ErrReadOnly]
    25  func (f *File) Write(_ []byte) (n int, err error) {
    26  	return 0, fmt.Errorf("%w: cannot call file.Write", ErrReadOnly)
    27  }
    28  
    29  // FS is a read-only implementation of kyaml/filesys.FileSystem to allow creating
    30  // Kustomize file systems from fs.FS implementations, such as [embed.FS]. Calls
    31  // to functions that write to the FileSystem will return a [ErrReadOnly].
    32  type FS struct {
    33  	fs.FS
    34  }
    35  
    36  var _ filesys.FileSystem = &FS{}
    37  
    38  // Open opens the named file for reading.
    39  func (f *FS) Open(path string) (filesys.File, error) {
    40  	file, err := f.FS.Open(path)
    41  	return &File{file}, err
    42  }
    43  
    44  // IsDir returns true if the path is a directory. The error from fs.Stat is
    45  // discarded.
    46  func (f *FS) IsDir(path string) bool {
    47  	stat, err := fs.Stat(f.FS, path)
    48  	if err != nil {
    49  		return false
    50  	}
    51  	return stat.IsDir()
    52  }
    53  
    54  // ReadDir returns a list of files and directories within a directory.
    55  func (f *FS) ReadDir(path string) ([]string, error) {
    56  	dirEntries, err := fs.ReadDir(f.FS, path)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	result := make([]string, 0, len(dirEntries))
    61  	for i, e := range dirEntries {
    62  		result[i] = e.Name()
    63  	}
    64  	return result, nil
    65  }
    66  
    67  // CleanedAbs converts the given path into a directory and a file name,
    68  // where the directory is represented as a ConfirmedDir and all that implies.
    69  // If the entire path is a directory, the file component is an empty string.
    70  //
    71  // NOTE: Results are relative to the root of the backing [fs.FS] and symlinks
    72  // are not evaluated.
    73  func (f *FS) CleanedAbs(path string) (filesys.ConfirmedDir, string, error) {
    74  	if f.IsDir(path) {
    75  		return filesys.ConfirmedDir(path), "", nil
    76  	}
    77  
    78  	return filesys.ConfirmedDir(filepath.Dir(path)), filepath.Base(path), nil
    79  }
    80  
    81  // Exists is true if the path exists in the file system.
    82  func (f *FS) Exists(path string) bool {
    83  	var perr *fs.PathError
    84  	_, err := fs.Stat(f.FS, path)
    85  	return !errors.Is(err, fs.ErrNotExist) && !errors.As(err, &perr)
    86  }
    87  
    88  // Glob returns the list of matching files,
    89  // emulating https://golang.org/pkg/path/filepath/#Glob
    90  func (f *FS) Glob(pattern string) ([]string, error) {
    91  	return fs.Glob(f.FS, pattern)
    92  }
    93  
    94  // ReadFile returns the contents of the file at the given path.
    95  func (f *FS) ReadFile(path string) ([]byte, error) {
    96  	return fs.ReadFile((f.FS), path)
    97  }
    98  
    99  // Walk walks the file system with the given WalkFunc. It uses fs.WalkDir under
   100  // the covers instead of filepath.Walk.
   101  func (f *FS) Walk(path string, walkFn filepath.WalkFunc) error {
   102  	return fs.WalkDir(f.FS, path, func(path string, d fs.DirEntry, err error) error {
   103  		if err != nil {
   104  			return err
   105  		}
   106  		info, err := d.Info()
   107  		if err != nil {
   108  			return err
   109  		}
   110  		return walkFn(path, info, err)
   111  	})
   112  }
   113  
   114  // WriteFile returns ErrReadOnly
   115  func (f *FS) WriteFile(_ string, _ []byte) error {
   116  	return fmt.Errorf("%w: cannot call WriteFile", ErrReadOnly)
   117  }
   118  
   119  // MkDir returns ErrReadOnly.
   120  func (f *FS) Mkdir(_ string) error {
   121  	return fmt.Errorf("%w: cannot call Mkdir", ErrReadOnly)
   122  }
   123  
   124  // MkDirAll returns ErrReadOnly.
   125  func (f *FS) MkdirAll(_ string) error {
   126  	return fmt.Errorf("%w: cannot create MkdirAll", ErrReadOnly)
   127  }
   128  
   129  // RemoveAll returns ErrReadOnly.
   130  func (f *FS) RemoveAll(_ string) error {
   131  	return fmt.Errorf("%w: cannot call RemoveAll", ErrReadOnly)
   132  }
   133  
   134  // Create a file.
   135  func (f *FS) Create(_ string) (filesys.File, error) {
   136  	return nil, fmt.Errorf("%w: cannot call Create", ErrReadOnly)
   137  }
   138  

View as plain text