fs

package
v0.5.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotDirectory is returned when a directory is expected but a file is found.
	ErrNotDirectory = errors.New("not a directory")
	ErrInvalidSHA   = errors.New("invalid SHA for file")
)

Functions

func Operate

func Operate(f FS, opts ...Option) error

Operate applies the specified options to the filesystem. This allows for declaring a set of conditions that must be present (like creating a directory path) before the final operation is performed.

Example:

Operate(fs,
	MakeDir("tmp", 0755),
	MakeDir("tmp/foo", 0755),
	MakeDir("tmp/foo/bar", 0755),
	WriteFile("tmp/foo/bar/baz.txt", []byte("hello world"), 0644))

Types

type FS

type FS interface {
	fs.FS

	// Mkdir creates a directory with the specified permissions.  Should match os.Mkdir().
	Mkdir(path string, perm fs.FileMode) error

	// ReadFile reads the file and returns the contents.  Should match os.ReadFile().
	ReadFile(name string) ([]byte, error)

	// WriteFile writes the file with the specified permissions.  Should match os.WriteFile().
	WriteFile(name string, data []byte, perm fs.FileMode) error
}

FS is the interface for a filesystem used by the program.

type Option

type Option interface {
	// Apply applies the option to the filesystem.
	Apply(FS) error
}

Option is an interface for options that can be applied in order via the Operate function.

func Options

func Options(opts ...Option) Option

Options provides a way to group multiple options together.

func ReadFileWithSHA256

func ReadFileWithSHA256(name string, data *[]byte) Option

ReadFileWithSHA256 reads the specified file ensures the checksum matches. The checksum is stored in a file of the same name ending with `.sha256`. If the file checksum does not match an error is returned.

The format of the .sha256 file is expected to match shasum format.

func WithDir

func WithDir(dir string, perm fs.FileMode) Option

WithDir is an option that ensures the specified directory exists. If it does not, create it with the specified permissions.

func WithDirs

func WithDirs(path string, perm fs.FileMode) Option

WithDirs is an option that ensures the specified directory path exists with the specified permissions. The path is split on the path separator and each directory is created in order if needed.

Notes:

  • The path should not contain the filename or that will be created as a directory.
  • The same permissions are applied to all directories that are created.

func WithPath

func WithPath(name string, perm fs.FileMode) Option

WithPath is an option that ensures the set of directories for the specified file exists. The directory is determined by calling filepath.Dir on the name.

Notes:

  • The name should contain the filename and any path to ensure is present.
  • The same permissions are applied to all directories that are created.

func WriteFileWithSHA256

func WriteFileWithSHA256(name string, data []byte, perm fs.FileMode) Option

WriteFileWithSHA256 calculates and writes both the file and a checksum file.

The format of the checksum file matches shasum format. The name of the file is the file name with an `.sha256` appended to the file name.

type OptionFunc

type OptionFunc func(FS) error

OptionFunc is a function that implements the Option interface.

func (OptionFunc) Apply

func (f OptionFunc) Apply(fs FS) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL