shell

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const ArgTypeCount = 4

Variables

This section is empty.

Functions

func EmptyArgValue added in v0.23.0

func EmptyArgValue() string

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(raw string, argType ArgType) Arg

func NewEmptyValueArg added in v0.23.0

func NewEmptyValueArg() Arg

func (Arg) HasValue added in v0.15.0

func (a Arg) HasValue() bool

func (Arg) String added in v0.15.0

func (a Arg) String() string

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 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
}

func NewArgs added in v0.15.0

func NewArgs() *Args

func (*Args) AddArg added in v0.15.0

func (a *Args) AddArg(arg string, argType ArgType)

AddArg adds a single argument with no flag

func (*Args) AddArgs added in v0.15.0

func (a *Args) AddArgs(args []string, argType ArgType)

AddArgs adds multiple arguments not associated with a flag

func (*Args) AddFlag added in v0.15.0

func (a *Args) AddFlag(key, value string, argType ArgType)

AddFlag adds a value to a flag

func (*Args) AddFlags added in v0.15.0

func (a *Args) AddFlags(key string, values []string, argType ArgType)

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) 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) SetLegacyArg added in v0.15.0

func (a *Args) SetLegacyArg(legacy bool) *Args

SetLegacyArg is used to activate the legacy (broken) mode of sending arguments on the restic command line

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)

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 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() {
		w.Write([]byte(scanner.Text() + eol))

		_, _ = 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 int)

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