Help us shape the future of Go! Take the 2026 Go Developer Survey

system

package
v3.26.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package system provides a thin, swappable filesystem abstraction so the linter can read styles, dictionaries, vocabularies and other assets from the host OS (the default) or from an in-memory map (the WebAssembly host supplies one via the JavaScript API in cmd/valew).

All non-test code paths that previously called os.ReadFile, os.Open, filepath.Walk, filepath.Glob, etc. should go through the package-level helpers in this file. The pluggable backend is swapped with SetDefault.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsPath

func AbsPath(path string) string

AbsPath returns the absolute path of `path`.

func DeterminePath

func DeterminePath(fsys FS, configPath string, keyPath string) string

DeterminePath determines the path of `keyPath` based on `configPath`.

If `keyPath` is an absolute path, it is returned as is.

If `keyPath` is a relative path, it is joined with `configPath`.

If `configPath` is not a directory, the directory part of `configPath` is used.

func ExecuteWithInput

func ExecuteWithInput(exe, text string, args ...string) (string, error)

ExecuteWithInput runs a command with the given text as input.

func ExecuteWithInputEnv added in v3.23.0

func ExecuteWithInputEnv(exe, text string, env []string, args ...string) (string, error)

ExecuteWithInputEnv is ExecuteWithInput with variables added to the command's environment.

func FileExists

func FileExists(filename string) bool

FileExists determines if the path given by `filename` exists.

func FileNameWithoutExt

func FileNameWithoutExt(fileName string) string

FileNameWithoutExt returns the filename without its extension.

func Glob added in v3.26.0

func Glob(pattern string) ([]string, error)

Glob returns the names matching a shell-style pattern in a single directory.

func GlobRecursive added in v3.26.0

func GlobRecursive(root, pattern string) ([]string, error)

GlobRecursive returns every file under root whose basename matches the supplied shell-style pattern.

func IsDir

func IsDir(filename string) bool

IsDir determines if the path given by `filename` is a directory.

func IsLinux

func IsLinux() bool

IsLinux returns true if the current OS is Linux.

func IsMac

func IsMac() bool

IsMac returns true if the current OS is macOS.

func IsUnix

func IsUnix() bool

IsUnix returns true if the current OS is either macOS or Linux.

func IsWindows

func IsWindows() bool

IsWindows returns true if the current OS is Windows.

func Mkdir

func Mkdir(dir string) error

Mkdir creates a directory at the given path.

func Name

func Name() string

Name returns the current OS.

func NormalizePath

func NormalizePath(path string) string

NormalizePath expands a tilde and returns the absolute path of `path`.

func Open added in v3.26.0

func Open(name string) (io.ReadCloser, error)

Open opens the named file from the active backend.

func PlatformAndArch

func PlatformAndArch() string

PlatformAndArch returns the current platform and architecture.

func ReadFile added in v3.26.0

func ReadFile(name string) ([]byte, error)

ReadFile returns the contents of the named file from the active backend.

func Readlink(name string) (string, error)

Readlink returns the destination of a symlink (or an error from non-OS backends, which have no concept of links).

func RelInside added in v3.25.0

func RelInside(base, path string) (string, bool)

RelInside returns path relative to base when it lies inside base. A path outside base, or one that can't be made relative, is returned as given with false.

func SetDefault added in v3.26.0

func SetDefault(fsys FS)

SetDefault replaces the active filesystem backend.

func Stat added in v3.26.0

func Stat(name string) (fs.FileInfo, error)

Stat returns FileInfo for the named path from the active backend.

func Unarchive

func Unarchive(src, dest string) error

Unarchive extracts a ZIP archive to a destination directory.

func Walk

func Walk(path string, walkFn filepath.WalkFunc) error

Walk walks the tree at path on the active backend; on the host OS it also follows symlinks.

func Which

func Which(cmds []string) string

Which checks for the existence of any command in `cmds`.

Types

type FS added in v3.26.0

type FS interface {
	ReadFile(name string) ([]byte, error)
	Open(name string) (io.ReadCloser, error)
	Stat(name string) (fs.FileInfo, error)
	Readlink(name string) (string, error)
	Walk(root string, fn filepath.WalkFunc) error
	Glob(pattern string) ([]string, error)
	GlobRecursive(root, pattern string) ([]string, error)
	Exists(name string) bool
	IsDir(name string) bool
}

FS is the surface the linter needs from a filesystem. It intentionally follows the os/filepath naming so swapping callers is mechanical.

var Default FS = OSFS{}

Default is the active backend. The wasm entry point swaps in a MemFS; every other build keeps the OS-backed implementation so native tests and CLIs continue to behave as before.

type MemFS added in v3.26.0

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

MemFS is an in-memory FS implementation. Paths are normalized to forward-slash form and treated as absolute under a single virtual root, so a Vale config with `StylesPath = /styles` and a MemFS populated under `/styles/<package>/<rule>.yml` behaves identically to a host-OS install.

func NewMemFS added in v3.26.0

func NewMemFS() *MemFS

NewMemFS returns an empty in-memory filesystem.

func (*MemFS) AddFile added in v3.26.0

func (m *MemFS) AddFile(name string, data []byte)

AddFile writes the supplied bytes at the given virtual path.

func (*MemFS) AddZip added in v3.26.0

func (m *MemFS) AddZip(dest string, data []byte) error

AddZip extracts every entry in the supplied ZIP archive under dest. Directory entries are skipped; their existence is inferred from the files inside them.

func (*MemFS) Exists added in v3.26.0

func (m *MemFS) Exists(name string) bool

Exists implements FS.

func (*MemFS) Glob added in v3.26.0

func (m *MemFS) Glob(pattern string) ([]string, error)

Glob implements FS. Patterns are matched against entries in the directory portion of the pattern with `path.Match` semantics, which is sufficient for everything the linter uses (currently `*.dic`).

func (*MemFS) GlobRecursive added in v3.26.0

func (m *MemFS) GlobRecursive(root, pattern string) ([]string, error)

GlobRecursive implements FS. The pattern is interpreted as a basename pattern that matches anywhere under root.

func (*MemFS) IsDir added in v3.26.0

func (m *MemFS) IsDir(name string) bool

IsDir implements FS.

func (*MemFS) List added in v3.26.0

func (m *MemFS) List() []string

List returns every file path currently stored, sorted lexicographically.

func (*MemFS) Open added in v3.26.0

func (m *MemFS) Open(name string) (io.ReadCloser, error)

Open implements FS.

func (*MemFS) ReadFile added in v3.26.0

func (m *MemFS) ReadFile(name string) ([]byte, error)

ReadFile implements FS.

func (m *MemFS) Readlink(name string) (string, error)

Readlink implements FS. The in-memory store has no symlinks.

func (*MemFS) Remove added in v3.26.0

func (m *MemFS) Remove(name string)

Remove deletes the file at the given virtual path. It is a no-op if the path does not exist.

func (*MemFS) Reset added in v3.26.0

func (m *MemFS) Reset()

Reset empties the filesystem.

func (*MemFS) Stat added in v3.26.0

func (m *MemFS) Stat(name string) (fs.FileInfo, error)

Stat implements FS.

func (*MemFS) Walk added in v3.26.0

func (m *MemFS) Walk(root string, fn filepath.WalkFunc) error

Walk implements FS. We iterate over the entries in lexicographic order and synthesize directory callbacks at the boundary between entries so the walkFn observes the same shape it would on disk: paths carry the root as the caller wrote it, so a relative root yields relative paths (which is what lets a caller take them relative to that root again), and SkipDir leaves the rest of that directory unvisited.

type OSFS added in v3.26.0

type OSFS struct{}

OSFS is the host-OS implementation of FS, used by default.

func (OSFS) Exists added in v3.26.0

func (OSFS) Exists(name string) bool

func (OSFS) Glob added in v3.26.0

func (OSFS) Glob(pattern string) ([]string, error)

func (OSFS) GlobRecursive added in v3.26.0

func (OSFS) GlobRecursive(root, pattern string) ([]string, error)

func (OSFS) IsDir added in v3.26.0

func (OSFS) IsDir(name string) bool

func (OSFS) Open added in v3.26.0

func (OSFS) Open(name string) (io.ReadCloser, error)

func (OSFS) ReadFile added in v3.26.0

func (OSFS) ReadFile(name string) ([]byte, error)
func (OSFS) Readlink(name string) (string, error)

func (OSFS) Stat added in v3.26.0

func (OSFS) Stat(name string) (fs.FileInfo, error)

func (OSFS) Walk added in v3.26.0

func (OSFS) Walk(root string, fn filepath.WalkFunc) error

Jump to

Keyboard shortcuts

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