github.com/databricks/cli@v0.203.0/libs/filer/filer.go (about)

     1  package filer
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"io/fs"
     8  )
     9  
    10  type WriteMode int
    11  
    12  const (
    13  	OverwriteIfExists       WriteMode = iota
    14  	CreateParentDirectories           = iota << 1
    15  )
    16  
    17  type DeleteMode int
    18  
    19  const (
    20  	DeleteRecursively DeleteMode = iota
    21  )
    22  
    23  type FileAlreadyExistsError struct {
    24  	path string
    25  }
    26  
    27  func (err FileAlreadyExistsError) Error() string {
    28  	return fmt.Sprintf("file already exists: %s", err.path)
    29  }
    30  
    31  func (err FileAlreadyExistsError) Is(other error) bool {
    32  	return other == fs.ErrExist
    33  }
    34  
    35  type FileDoesNotExistError struct {
    36  	path string
    37  }
    38  
    39  func (err FileDoesNotExistError) Is(other error) bool {
    40  	return other == fs.ErrNotExist
    41  }
    42  
    43  func (err FileDoesNotExistError) Error() string {
    44  	return fmt.Sprintf("file does not exist: %s", err.path)
    45  }
    46  
    47  type NoSuchDirectoryError struct {
    48  	path string
    49  }
    50  
    51  func (err NoSuchDirectoryError) Error() string {
    52  	return fmt.Sprintf("no such directory: %s", err.path)
    53  }
    54  
    55  func (err NoSuchDirectoryError) Is(other error) bool {
    56  	return other == fs.ErrNotExist
    57  }
    58  
    59  type NotADirectory struct {
    60  	path string
    61  }
    62  
    63  func (err NotADirectory) Error() string {
    64  	return fmt.Sprintf("not a directory: %s", err.path)
    65  }
    66  
    67  func (err NotADirectory) Is(other error) bool {
    68  	return other == fs.ErrInvalid
    69  }
    70  
    71  type NotAFile struct {
    72  	path string
    73  }
    74  
    75  func (err NotAFile) Error() string {
    76  	return fmt.Sprintf("not a file: %s", err.path)
    77  }
    78  
    79  func (err NotAFile) Is(other error) bool {
    80  	return other == fs.ErrInvalid
    81  }
    82  
    83  type DirectoryNotEmptyError struct {
    84  	path string
    85  }
    86  
    87  func (err DirectoryNotEmptyError) Error() string {
    88  	return fmt.Sprintf("directory not empty: %s", err.path)
    89  }
    90  
    91  func (err DirectoryNotEmptyError) Is(other error) bool {
    92  	return other == fs.ErrInvalid
    93  }
    94  
    95  type CannotDeleteRootError struct {
    96  }
    97  
    98  func (err CannotDeleteRootError) Error() string {
    99  	return "unable to delete filer root"
   100  }
   101  
   102  func (err CannotDeleteRootError) Is(other error) bool {
   103  	return other == fs.ErrInvalid
   104  }
   105  
   106  // Filer is used to access files in a workspace.
   107  // It has implementations for accessing files in WSFS and in DBFS.
   108  type Filer interface {
   109  	// Write file at `path`.
   110  	// Use the mode to further specify behavior.
   111  	Write(ctx context.Context, path string, reader io.Reader, mode ...WriteMode) error
   112  
   113  	// Read file at `path`.
   114  	Read(ctx context.Context, path string) (io.ReadCloser, error)
   115  
   116  	// Delete file or directory at `path`.
   117  	Delete(ctx context.Context, path string, mode ...DeleteMode) error
   118  
   119  	// Return contents of directory at `path`.
   120  	ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error)
   121  
   122  	// Creates directory at `path`, creating any intermediate directories as required.
   123  	Mkdir(ctx context.Context, path string) error
   124  
   125  	// Stat returns information about the file at `path`.
   126  	Stat(ctx context.Context, name string) (fs.FileInfo, error)
   127  }