2022-12-14 14:37:14 +00:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-05-31 12:22:26 +00:00
|
|
|
"io/fs"
|
2022-12-14 14:37:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WriteMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
OverwriteIfExists WriteMode = iota
|
|
|
|
CreateParentDirectories = iota << 1
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileAlreadyExistsError struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err FileAlreadyExistsError) Error() string {
|
|
|
|
return fmt.Sprintf("file already exists: %s", err.path)
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:47:00 +00:00
|
|
|
func (err FileAlreadyExistsError) Is(other error) bool {
|
|
|
|
return other == fs.ErrExist
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:24:20 +00:00
|
|
|
type FileDoesNotExistError struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:47:00 +00:00
|
|
|
func (err FileDoesNotExistError) Is(other error) bool {
|
|
|
|
return other == fs.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:24:20 +00:00
|
|
|
func (err FileDoesNotExistError) Error() string {
|
|
|
|
return fmt.Sprintf("file does not exist: %s", err.path)
|
|
|
|
}
|
|
|
|
|
2022-12-14 14:37:14 +00:00
|
|
|
type NoSuchDirectoryError struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err NoSuchDirectoryError) Error() string {
|
|
|
|
return fmt.Sprintf("no such directory: %s", err.path)
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:47:00 +00:00
|
|
|
func (err NoSuchDirectoryError) Is(other error) bool {
|
|
|
|
return other == fs.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2022-12-14 14:37:14 +00:00
|
|
|
// Filer is used to access files in a workspace.
|
|
|
|
// It has implementations for accessing files in WSFS and in DBFS.
|
|
|
|
type Filer interface {
|
|
|
|
// Write file at `path`.
|
|
|
|
// Use the mode to further specify behavior.
|
|
|
|
Write(ctx context.Context, path string, reader io.Reader, mode ...WriteMode) error
|
|
|
|
|
|
|
|
// Read file at `path`.
|
|
|
|
Read(ctx context.Context, path string) (io.Reader, error)
|
|
|
|
|
|
|
|
// Delete file at `path`.
|
|
|
|
Delete(ctx context.Context, path string) error
|
2023-05-31 09:11:17 +00:00
|
|
|
|
|
|
|
// Return contents of directory at `path`.
|
2023-05-31 12:22:26 +00:00
|
|
|
ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error)
|
2023-05-31 09:11:17 +00:00
|
|
|
|
|
|
|
// Creates directory at `path`, creating any intermediate directories as required.
|
|
|
|
Mkdir(ctx context.Context, path string) error
|
2022-12-14 14:37:14 +00:00
|
|
|
}
|