file

package
v2.20.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package file provides functions that combine file handling, JSON marshaling and file system abstraction.

Index

Constants

This section is empty.

Variables

View Source
var (
	// OptNone is a no-op.
	OptNone = Option{optNone}
	// OptOverwrite overwrites an existing file.
	OptOverwrite = Option{optOverwrite}
	// OptMkdirAll creates the path to the file.
	OptMkdirAll = Option{optMkdirAll}
	// OptAppend appends to the file.
	OptAppend = Option{optAppend}
)

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles file interaction.

func NewHandler

func NewHandler(fs afero.Fs) Handler

NewHandler returns a new file handler.

func (*Handler) CopyDir added in v2.8.0

func (h *Handler) CopyDir(src, dst string, opts ...Option) error

CopyDir copies the src directory recursively into dst with the given options. OptMkdirAll is always set. CopyDir does not follow symlinks.

func (*Handler) CopyFile added in v2.8.0

func (h *Handler) CopyFile(src, dst string, opts ...Option) error

CopyFile copies the file from src to dst with the given options, respecting file permissions.

func (*Handler) IsEmpty added in v2.13.0

func (h *Handler) IsEmpty(dirName string) (bool, error)

IsEmpty returns true if the given directory is empty.

func (*Handler) MkdirAll added in v2.3.0

func (h *Handler) MkdirAll(name string) error

MkdirAll creates a directory path and all parents that does not exist yet.

func (*Handler) Read

func (h *Handler) Read(name string) ([]byte, error)

Read reads the file given name and returns the bytes read.

func (*Handler) ReadJSON

func (h *Handler) ReadJSON(name string, content any) error

ReadJSON reads a JSON file from name and unmarshals it into the content interface. The interface content must be a pointer to a JSON marchalable object.

func (*Handler) ReadYAML

func (h *Handler) ReadYAML(name string, content any) error

ReadYAML reads a YAML file from name and unmarshals it into the content interface. The interface content must be a pointer to a YAML marshalable object.

func (*Handler) ReadYAMLStrict

func (h *Handler) ReadYAMLStrict(name string, content any) error

ReadYAMLStrict does the same as ReadYAML, but fails if YAML contains fields that are not specified in content.

func (*Handler) Remove

func (h *Handler) Remove(name string) error

Remove deletes the file with the given name.

func (*Handler) RemoveAll

func (h *Handler) RemoveAll(name string) error

RemoveAll deletes the file or directory with the given name.

func (*Handler) RenameFile added in v2.12.0

func (h *Handler) RenameFile(a, b string) error

RenameFile renames a file, overwriting any existing file at the destination.

func (*Handler) Stat

func (h *Handler) Stat(name string) (fs.FileInfo, error)

Stat returns a FileInfo describing the named file, or an error, if any happens.

func (*Handler) Write

func (h *Handler) Write(name string, data []byte, options ...Option) error

Write writes the data bytes into the file with the given name.

func (*Handler) WriteJSON

func (h *Handler) WriteJSON(name string, content any, options ...Option) error

WriteJSON marshals the content interface to JSON and writes it to the path with the given name.

func (*Handler) WriteYAML

func (h *Handler) WriteYAML(name string, content any, options ...Option) (err error)

WriteYAML marshals the content interface to YAML and writes it to the path with the given name.

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option is a bitmask of options for file operations.

Jump to

Keyboard shortcuts

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