shell

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const ArgTypeCount = 4

Variables

This section is empty.

Functions

This section is empty.

Types

type Arg added in v0.15.0

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

func NewArg added in v0.15.0

func NewArg(value string, argType ArgType, options ...ArgOption) Arg

func NewArgsSlice added in v0.28.0

func NewArgsSlice(args []string, argType ArgType, options ...ArgOption) []Arg

func NewEmptyValueArg added in v0.23.0

func NewEmptyValueArg() Arg

func (Arg) Clone added in v0.28.0

func (a Arg) Clone() Arg

func (Arg) GetConfidentialValue added in v0.28.0

func (a Arg) GetConfidentialValue() string

func (Arg) HasConfidentialFilter added in v0.28.0

func (a Arg) HasConfidentialFilter() bool

IsConfidential returns true if the argument may contain credentials.

func (Arg) HasValue added in v0.15.0

func (a Arg) HasValue() bool

HasValue returns true if the argument has a value (e.g. --flag=value). The value could be empty (e.g. --flag=""). If false, the argument is a simple flag (e.g. --verbose).

func (Arg) IsEmptyValue added in v0.28.0

func (a Arg) IsEmptyValue() bool

IsEmpty means the flag is specifically empty, not just a flag without a value (e.g. --flag="")

func (Arg) String added in v0.15.0

func (a Arg) String() string

String returns an escaped value to send to the command line

func (Arg) Type added in v0.15.0

func (a Arg) Type() ArgType

func (Arg) Value added in v0.15.0

func (a Arg) Value() string

type ArgModifier added in v0.28.0

type ArgModifier interface {
	// Arg returns either the same of a new Arg if the value was changed.
	// A boolean also indicates if the value was changed.
	Arg(name string, arg *Arg) (*Arg, bool)
}

type ArgOption added in v0.28.0

type ArgOption interface {
	// contains filtered or unexported methods
}

type ArgType added in v0.15.0

type ArgType int
const (
	ArgConfigEscape        ArgType = iota // escape each special character but don't add quotes
	ArgConfigKeepGlobQuote                // use double quotes around argument when needed so the shell doesn't resolve glob patterns
	ArgCommandLineEscape                  // same as ArgConfigEscape but argument is coming from the command line
	ArgConfigBackupSource                 // same as ArgConfigEscape but represents the folders to add at the end of a backup command
	ArgLegacyEscape
	ArgLegacyKeepGlobQuote
	ArgLegacyCommandLineEscape
	ArgLegacyConfigBackupSource
)

type Args added in v0.15.0

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

Args is a collection of arguments that can be used to build a command line.

func NewArgs added in v0.15.0

func NewArgs() *Args

NewArgs creates a new Args instance

func (*Args) AddArg added in v0.15.0

func (a *Args) AddArg(arg Arg)

AddArg adds a single argument with no flag

func (*Args) AddArgs added in v0.15.0

func (a *Args) AddArgs(args []Arg)

AddArgs adds multiple arguments not associated with a flag

func (*Args) AddFlag added in v0.15.0

func (a *Args) AddFlag(key string, arg Arg)

AddFlag adds a value to a flag

func (*Args) AddFlags added in v0.15.0

func (a *Args) AddFlags(key string, args []Arg)

AddFlags adds a slice of values for the same flag

func (*Args) Clone added in v0.15.0

func (a *Args) Clone() *Args

func (*Args) Get added in v0.15.0

func (a *Args) Get(name string) ([]Arg, bool)

func (*Args) GetAll added in v0.15.0

func (a *Args) GetAll() []string

GetAll return a clean list of arguments to send on the command line

func (*Args) Modify added in v0.28.0

func (a *Args) Modify(modifier ArgModifier) *Args

Modify returns a new Args with all arguments modified by the provided modifier

func (*Args) Remove added in v0.21.0

func (a *Args) Remove(name string) ([]Arg, bool)

func (*Args) RemoveArg added in v0.21.0

func (a *Args) RemoveArg(name string) (removed []Arg)

func (*Args) Rename added in v0.21.0

func (a *Args) Rename(oldName, newName string) bool

func (*Args) ToMap added in v0.15.0

func (a *Args) ToMap() map[string][]string

ToMap converts the arguments to a map. It is only used by unit tests.

func (*Args) Walk added in v0.15.0

func (a *Args) Walk(callback func(name string, arg *Arg) *Arg)

type Command

type Command struct {
	Command    string
	Arguments  []string
	Environ    []string
	Shell      []string
	Dir        string
	Stdin      io.Reader
	Stdout     io.Writer
	Stderr     io.Writer
	SetPID     SetPID
	ScanStdout ScanOutput
	// contains filtered or unexported fields
}

Command holds the configuration to run a shell command

func NewCommand

func NewCommand(command string, args []string) *Command

NewCommand instantiate a default Command without receiving OS signals (SIGTERM, etc.)

func NewSignalledCommand added in v0.6.1

func NewSignalledCommand(command string, args []string, c chan os.Signal) *Command

NewSignalledCommand instantiate a default Command receiving OS signals (SIGTERM, etc.)

func (*Command) GetShellCommand added in v0.18.0

func (c *Command) GetShellCommand() (shell string, arguments []string, err error)

GetShellCommand transforms the command line and arguments to be launched via a shell (sh or cmd.exe). This method doesn't escape any argument containing spaces, it should have been dealt with before.

func (*Command) OnErrorCallback added in v0.17.0

func (c *Command) OnErrorCallback(name, pattern string, minCount, maxCalls int, callback func(line string) error) error

OnErrorCallback registers a custom callback that is invoked when pattern (regex) matches a line in stderr.

func (*Command) Run

func (c *Command) Run() (monitor.Summary, string, error)

Run the command

type ConfidentialArgModifier added in v0.28.0

type ConfidentialArgModifier struct {
}

ConfidentialArgModifier masks confidential information in arguments.

func NewConfidentialArgModifier added in v0.28.0

func NewConfidentialArgModifier() *ConfidentialArgModifier

func (ConfidentialArgModifier) Arg added in v0.28.0

func (m ConfidentialArgModifier) Arg(name string, arg *Arg) (*Arg, bool)

Arg returns either the same of a new argument if the value has changed. A boolean value indicates if Arg has changed.

type ConfidentialArgOption added in v0.28.0

type ConfidentialArgOption struct {
	ConfidentialFilter filterFunc
}

func NewConfidentialArgOption added in v0.28.0

func NewConfidentialArgOption(confidential bool) *ConfidentialArgOption

type EmptyArgOption added in v0.28.0

type EmptyArgOption struct{}

EmptyArgOption is an option to create an specifically empty argument (e.g. --flag="")

type ExpandEnvModifier added in v0.28.0

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

func NewExpandEnvModifier added in v0.28.0

func NewExpandEnvModifier(environment []string) *ExpandEnvModifier

func (ExpandEnvModifier) Arg added in v0.28.0

func (m ExpandEnvModifier) Arg(name string, arg *Arg) (*Arg, bool)

Arg returns either the same of a new argument if the value was expanded. A boolean value indicates if the argument was expanded.

type LegacyArgModifier added in v0.28.0

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

LegacyArgModifier changes the type of arguments to a legacy type.

func NewLegacyArgModifier added in v0.28.0

func NewLegacyArgModifier(legacy bool) *LegacyArgModifier

func (LegacyArgModifier) Arg added in v0.28.0

func (m LegacyArgModifier) Arg(name string, arg *Arg) (*Arg, bool)

Arg returns either the same of a new argument if the type has changed. A boolean value indicates if Arg has changed.

type OutputAnalyser added in v0.14.0

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

func NewOutputAnalyser added in v0.14.0

func NewOutputAnalyser() *OutputAnalyser

func (*OutputAnalyser) AnalyseLines added in v0.14.0

func (a *OutputAnalyser) AnalyseLines(output io.Reader) (err error)

func (*OutputAnalyser) AnalyseStringLines added in v0.14.0

func (a *OutputAnalyser) AnalyseStringLines(output string) error

func (*OutputAnalyser) ContainsRemoteLockFailure added in v0.14.0

func (a *OutputAnalyser) ContainsRemoteLockFailure() bool

func (*OutputAnalyser) GetRemoteLockedBy added in v0.14.0

func (a *OutputAnalyser) GetRemoteLockedBy() (string, bool)

func (*OutputAnalyser) GetRemoteLockedMaxWait added in v0.23.0

func (a *OutputAnalyser) GetRemoteLockedMaxWait() (time.Duration, bool)

func (*OutputAnalyser) GetRemoteLockedSince added in v0.14.0

func (a *OutputAnalyser) GetRemoteLockedSince() (time.Duration, bool)

func (*OutputAnalyser) Reset added in v0.17.0

func (a *OutputAnalyser) Reset() *OutputAnalyser

func (*OutputAnalyser) SetCallback added in v0.17.0

func (a *OutputAnalyser) SetCallback(name, pattern string, minCount, maxCalls int, stopOnError bool, callback func(line string) error) error

SetCallback registers a custom callback that is invoked when pattern (regex) matches a line.

type ResticJsonSummary added in v0.12.0

type ResticJsonSummary struct {
	MessageType         string  `json:"message_type"`
	FilesNew            int     `json:"files_new"`
	FilesChanged        int     `json:"files_changed"`
	FilesUnmodified     int     `json:"files_unmodified"`
	DirsNew             int     `json:"dirs_new"`
	DirsChanged         int     `json:"dirs_changed"`
	DirsUnmodified      int     `json:"dirs_unmodified"`
	DataBlobs           int     `json:"data_blobs"`
	TreeBlobs           int     `json:"tree_blobs"`
	DataAdded           uint64  `json:"data_added"`
	TotalFilesProcessed int     `json:"total_files_processed"`
	TotalBytesProcessed uint64  `json:"total_bytes_processed"`
	TotalDuration       float64 `json:"total_duration"`
	SnapshotID          string  `json:"snapshot_id"`
}

type ScanOutput added in v0.12.0

type ScanOutput func(r io.Reader, summary *monitor.Summary, w io.Writer) error

ScanOutput is a callback to scan the default output of the command The implementation is expected to send everything read from the reader back to the writer

var ScanBackupJson ScanOutput = func(r io.Reader, summary *monitor.Summary, w io.Writer) error {
	bogusPrefix := []byte("\r\x1b[2K")
	jsonPrefix := []byte(`{"message_type":"`)
	summaryPrefix := []byte(`{"message_type":"summary",`)
	jsonSuffix := []byte("}")
	eol := "\n"
	if runtime.GOOS == "windows" {
		eol = "\r\n"
	}
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		line := scanner.Bytes()
		line = bytes.TrimPrefix(line, bogusPrefix)
		if bytes.HasPrefix(line, jsonPrefix) && bytes.HasSuffix(line, jsonSuffix) {
			if bytes.HasPrefix(line, summaryPrefix) {
				jsonSummary := ResticJsonSummary{}
				err := json.Unmarshal(line, &jsonSummary)
				if err != nil {
					continue
				}
				summary.FilesNew = jsonSummary.FilesNew
				summary.FilesChanged = jsonSummary.FilesChanged
				summary.FilesUnmodified = jsonSummary.FilesUnmodified
				summary.DirsNew = jsonSummary.DirsNew
				summary.DirsChanged = jsonSummary.DirsChanged
				summary.DirsUnmodified = jsonSummary.DirsUnmodified
				summary.FilesTotal = jsonSummary.TotalFilesProcessed
				summary.BytesAdded = jsonSummary.DataAdded
				summary.BytesTotal = jsonSummary.TotalBytesProcessed
			}
			continue
		}

		_, _ = w.Write(line)
		_, _ = w.Write([]byte(eol))
	}

	if err := scanner.Err(); err != nil {
		return err
	}
	return nil
}

ScanBackupJson should populate the backup summary values from the output of the --json flag

var ScanBackupPlain ScanOutput = func(r io.Reader, summary *monitor.Summary, w io.Writer) error {
	eol := "\n"
	if runtime.GOOS == "windows" {
		eol = "\r\n"
	}
	rawBytes, unit, duration := 0.0, "", ""
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		_, err := w.Write([]byte(scanner.Text() + eol))
		if err != nil {
			return err
		}

		_, _ = fmt.Sscanf(scanner.Text(), "Files: %d new, %d changed, %d unmodified", &summary.FilesNew, &summary.FilesChanged, &summary.FilesUnmodified)
		_, _ = fmt.Sscanf(scanner.Text(), "Dirs: %d new, %d changed, %d unmodified", &summary.DirsNew, &summary.DirsChanged, &summary.DirsUnmodified)

		n, err := fmt.Sscanf(scanner.Text(), "Added to the repo: %f %3s", &rawBytes, &unit)
		if n == 2 && err == nil {
			summary.BytesAdded = unformatBytes(rawBytes, unit)
		}

		n, err = fmt.Sscanf(scanner.Text(), "processed %d files, %f %3s in %s", &summary.FilesTotal, &rawBytes, &unit, &duration)
		if n == 4 && err == nil {
			summary.BytesTotal = unformatBytes(rawBytes, unit)
		}
	}

	if err := scanner.Err(); err != nil {
		return err
	}
	return nil
}

ScanBackupPlain should populate the backup summary values from the standard output

type SetPID added in v0.9.2

type SetPID func(pid int32)

SetPID is a callback to send the PID of the current child process

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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