file

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: AGPL-3.0 Imports: 10 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}
)

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) 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 marchalable 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) 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