shutil

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2020 License: MIT Imports: 6 Imported by: 0

README

shutil

GoDoc

go get barney.ci/shutil

A Go package that contains utilities for interacting with shells or reproducing shell functionality.


Copyright © 2020 Arista Networks, Inc.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnterminatedClass = errors.New("unterminated character class")
)

Functions

func GlobMatch

func GlobMatch(pattern, data string) (bool, error)

GlobMatch compiles pattern, and then returns Glob.Match(data).

func GlobMatchInfo

func GlobMatchInfo(pattern string, info os.FileInfo) (bool, error)

GlobMatch compiles pattern, and then returns Glob.MatchInfo(info).

func GlobMatchName

func GlobMatchName(pattern string, namer Namer) (bool, error)

GlobMatch compiles pattern, and then returns Glob.MatchName(namer).

func Quote

func Quote(argv []string) string

Quote returns a bash command approximating what a human would probably type to invoke the given argv array. For example, Quote([]string{"rm", "abc def", "hij"}) returns "rm 'abc def' hij".

func Substitute

func Substitute(s string, vars VariableMap) (string, error)

Substitute expands and substitutes shell variables in s, and returns the fully substituted string. It errors out if s contains variables that do not exist in the specified variable map.

The syntax for variable substitution is a restricted variant to that of a POSIX shell:

  • Variables are denoted with ${variable_name}.
  • All characters except ":" and "}" are accepted in variable names.
  • ${variable:-default} expands to "default" if the variable is not defined in the variable map, or the value of the variable otherwise.
  • ${variable:+alternate} expands to "alternate" if the variable is defined in the variable map, or the empty string otherwise.

Types

type Glob

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

Glob represents a compiled glob pattern. The supported syntax is mostly the same as glob(7), with the following extensions:

  • Curly brace expansion is supported. "{a,b,c}" matches the strings "a", "b", and "c".
  • A double star ("**") is supported to match any pathname component and their children. For instance, "dir/*" matches "dir/file" but not "dir/dir/file", while "dir/**" matches both.
  • If the pattern starts with "!", the whole pattern is negated. If "!" appears later in the pattern, it is treated as a literal "!".

func CompileGlob

func CompileGlob(pattern string) (*Glob, error)

CompileGlob compiles the specified pattern into a Glob object.

See the documentation of the Glob type for more details on the supported syntax.

func MustCompileGlob

func MustCompileGlob(pattern string) *Glob

MustCompileGlob is like CompileGlob, but panics if the function returned an error.

func (*Glob) MarshalText

func (g *Glob) MarshalText() ([]byte, error)

func (*Glob) Match

func (g *Glob) Match(data string) bool

Match returns whether data matches the glob pattern.

func (*Glob) MatchInfo

func (g *Glob) MatchInfo(info os.FileInfo) bool

Match returns whether the specified FileInfo matches the glob pattern.

Generally, the name of the FileInfo is checked against the pattern. If the FileInfo represents a directory, the name followed by "/" is also checked against the pattern.

This behaviour allows for a pattern like "*/" to *only* match directories. If this is not desirable, use MatchName instead.

func (*Glob) MatchName

func (g *Glob) MatchName(namer Namer) bool

Match returns whether the specified Namer matches the glob pattern. It is equivalent to Match(namer.Name()).

This function is there for convenience as both *os.File and os.FileInfo implement Name().

func (*Glob) String

func (g *Glob) String() string

func (*Glob) UnmarshalText

func (g *Glob) UnmarshalText(text []byte) error

type GlobError

type GlobError struct {

	// Pattern is the erroneous glob pattern.
	Pattern string

	// Index is the index where the invalid character resides.
	Index int

	// Err is the concrete underlying error.
	Err error
}

GlobError represents a syntax error for a specific glob pattern.

func (*GlobError) Error

func (err *GlobError) Error() string

func (*GlobError) Unwrap

func (err *GlobError) Unwrap() error

type Namer

type Namer interface {
	Name() string
}

A Namer represents types that have a Name. Notable types that implement this interface are *os.File and os.FileInfo.

type SimpleVariableMap

type SimpleVariableMap map[string]string

SimpleVariableMap is a thin wrapper around map[string]string that implements VariableMap.

func (SimpleVariableMap) Get

func (smap SimpleVariableMap) Get(variable string) (string, bool)

type VariableMap

type VariableMap interface {
	Get(variable string) (value string, present bool)
}

VariableMap is the interface that wraps the Get method.

Get takes a variable name, and returns the value associated to the variable if it has one, along with whether the variable is present in the variable map or not.

Jump to

Keyboard shortcuts

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