files

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package files provides helpers to read, write and validate files, and to locate files across a directory tree.

Index

Constants

View Source
const (
	// Rw represents a file permission of read/write for current user
	// and no access for user's group and other groups.
	Rw fs.FileMode = 0o600

	// RwRR represents a file permission of read/write for current user
	// and read-only access for user's group and other groups.
	RwRR fs.FileMode = 0o644

	// RwRwRw represents a file permission of read/write for current user
	// and read/write too for user's group and other groups.
	RwRwRw fs.FileMode = 0o666

	// RwxRxRxRx represents a file permission of read/write/execute for current user
	// and read/execute for user's group and other groups.
	RwxRxRxRx fs.FileMode = 0o755
)

Variables

View Source
var ErrNilRead = errors.New("read function is nil")

ErrNilRead is returned when the read function is nil.

Functions

func Exists

func Exists(src string) bool

Exists returns a boolean indicating whether the provided input src can be stat'ed or not.

func Glob

func Glob(root, glob string, opts ...GlobOption) []string

Glob returns all matching files for the input glob and root (and its subdirectories).

In case root directory doesn't exist, no matches are returned (error is silenced).

func ReadJSON

func ReadJSON(fsys fs.FS, src string, out any) error

ReadJSON reads the input src from fsys and unmarshals it with JSON format into the out configuration.

Input src path must be relative to fsys (see io/fs.FS and io/fs.ValidPath), not absolute.

func ReadJSONFunc

func ReadJSONFunc(fsys fs.FS, src string) func(out any) error

ReadJSONFunc wraps ReadJSON as a delayed call, matching the func(out any) error signature expected by Validate.

Input src path must be relative to fsys (see io/fs.FS and io/fs.ValidPath), not absolute.

func ReadTOML

func ReadTOML(fsys fs.FS, src string, out any) error

ReadTOML reads the input src from fsys and unmarshals it with TOML format into the out configuration.

Input src path must be relative to fsys (see io/fs.FS and io/fs.ValidPath), not absolute.

func ReadTOMLFunc

func ReadTOMLFunc(fsys fs.FS, src string) func(out any) error

ReadTOMLFunc wraps ReadTOML as a delayed call, matching the func(out any) error signature expected by Validate.

Input src path must be relative to fsys (see io/fs.FS and io/fs.ValidPath), not absolute.

func ReadYAML

func ReadYAML(fsys fs.FS, src string, out any) error

ReadYAML reads the input src from fsys and unmarshals it with YAML format into the out configuration.

Input src path must be relative to fsys (see io/fs.FS and io/fs.ValidPath), not absolute.

func ReadYAMLFunc

func ReadYAMLFunc(fsys fs.FS, src string) func(out any) error

ReadYAMLFunc wraps ReadYAML as a delayed call, matching the func(out any) error signature expected by Validate.

Input src path must be relative to fsys (see io/fs.FS and io/fs.ValidPath), not absolute.

func Umask

func Umask() os.FileMode

Umask returns the running process' umask, computed once for the process' lifetime on first call.

func Validate

func Validate(readSchema, readFile func(out any) error) error

Validate validates a given file (read from readFile) with a JSON schema (read from readSchema).

This function takes both delegated functions to be able to read from various fs.

Example:

func main() {
	fsys := os.DirFS(".")
	err := files.Validate(files.ReadJSONFunc(fsys, "path/to/schema"), files.ReadYAMLFunc(fsys, "path/to/file/to/validate"))
	// handle err
}

func WriteJSON

func WriteJSON(out string, data any) error

WriteJSON writes the input configuration into the dest in JSON format.

func WriteTOML

func WriteTOML(out string, data any) error

WriteTOML writes the input configuration into the dest in TOML format.

func WriteYAML

func WriteYAML(out string, data any, opts ...yaml.EncodeOption) error

WriteYAML writes the input configuration into the dest in YAML format.

Types

type GlobOption

type GlobOption func(o globOptions) globOptions

GlobOption represents a function that can be given when calling Glob to add specific behaviors.

func GlobExcludedDirectories

func GlobExcludedDirectories(dirs ...string) GlobOption

GlobExcludedDirectories returns a GlobOption which adds excluded directories from Glob checking.

Excluded directories are applied to all directory levels (root and subdirectories) during checking.

func GlobExcludedFiles

func GlobExcludedFiles(files ...string) GlobOption

GlobExcludedFiles returns a GlobOption which adds excluded files from Glob checking.

Excluded files are applied to all directory levels (root and subdirectories) during checking.

type ValidationError

type ValidationError struct {
	Message  string
	Property string
}

ValidationError represents a simplified view of jsonschema.ValidationError.

It is used to override specific error messages (like kind.FalseSchema "false schema") in kickr validation context.

func (*ValidationError) Error

func (v *ValidationError) Error() string

Jump to

Keyboard shortcuts

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