Documentation
¶
Overview ¶
Package files provides helpers to read, write and validate files, and to locate files across a directory tree.
Index ¶
- Constants
- Variables
- func Exists(src string) bool
- func Glob(root, glob string, opts ...GlobOption) []string
- func ReadJSON(fsys fs.FS, src string, out any) error
- func ReadJSONFunc(fsys fs.FS, src string) func(out any) error
- func ReadTOML(fsys fs.FS, src string, out any) error
- func ReadTOMLFunc(fsys fs.FS, src string) func(out any) error
- func ReadYAML(fsys fs.FS, src string, out any) error
- func ReadYAMLFunc(fsys fs.FS, src string) func(out any) error
- func Umask() os.FileMode
- func Validate(readSchema, readFile func(out any) error) error
- func WriteJSON(out string, data any) error
- func WriteTOML(out string, data any) error
- func WriteYAML(out string, data any, opts ...yaml.EncodeOption) error
- type GlobOption
- type ValidationError
Constants ¶
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 ¶
var ErrNilRead = errors.New("read function is nil")
ErrNilRead is returned when the read function is nil.
Functions ¶
func Exists ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Umask returns the running process' umask, computed once for the process' lifetime on first call.
func Validate ¶
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
}
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 ¶
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