files

package
v1.0.0-beta.18 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package files provides various features to read, write and validate files with JSON schema.

Validation feature is based on JSON Schema (https://json-schema.org/) and santhosh-tekuri/jsonschema (https://github.com/santhosh-tekuri/jsonschema).

Example:

type config struct { ... }

func main() {
	destdir, _ := os.Getwd()
	dest := filepath.Join(destdir, "filename.ext")

	if err := files.Validate(
		func(out any) error { return files.ReadJSON("path/to/schema.json", out, customfs.ReadFile) },
		func(out any) error { return files.ReadYAML(dest, out, os.ReadFile) },
	); err != nil {
		logger.Fatal(err)
	}

	var c config
	if err := configuration.ReadYAML(dest, &config); err != nil {
		logger.Fatal(err)
	}

	...

	if err := configuration.WriteYAML(dest, config, yaml.Indent(2), yaml.IndentSequence(true)); err != nil {
		logger.Fatal(err)
	}
}

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 ReadJSON

func ReadJSON(src string, config any, read func(src string) ([]byte, error)) error

ReadJSON reads the input src and unmarshal it in JSON format into the out configuration.

func ReadYAML

func ReadYAML(src string, out any, read func(src string) ([]byte, error)) error

ReadYAML reads the input src and unmarshal it in YAML into the out configuration.

func Validate

func Validate(readSchema, readFile func(v 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() {
	err := files.Validate(
		func(out any) error { return files.ReadJSON("path/to/schema", out, os.ReadFile) },
		func(out any) error { return files.ReadYAML("path/to/file/to/validate", out, os.ReadFile) },
	)
	// handle err
}

func WriteJSON

func WriteJSON(dst string, config any) error

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

func WriteYAML

func WriteYAML(dst string, config any, opts ...yaml.EncodeOption) error

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

Types

type ValidationError

type ValidationError struct {
	Message  string
	Property string
}

ValidationError represents a simplified view of jsonschema.ValidationError.

It it used to override specific error messages (like kind.FalseSchema "false schema") in craft 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