shell

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Command       string
	Arguments     []string
	Environ       []string
	Dir           string
	Stdin         io.Reader
	Stdout        io.Writer
	Stderr        io.Writer
	SetPID        SetPID
	ScanStdout    ScanOutput
	UsePowershell bool
	// 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) Run

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

Run the command

func (*Command) ScanStderr added in v0.13.0

func (c *Command) ScanStderr(r io.Reader, w1, w2 io.Writer) error

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) OutputAnalysis

func (OutputAnalyser) AnalyseStringLines added in v0.14.0

func (a OutputAnalyser) AnalyseStringLines(output string) OutputAnalysis

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) GetRemoteLockedSince added in v0.14.0

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

type OutputAnalysis added in v0.14.0

type OutputAnalysis interface {
	// ContainsRemoteLockFailure returns true if the output indicates that remote locking failed.
	ContainsRemoteLockFailure() bool

	// GetRemoteLockedSince returns the time duration since the remote lock was created.
	// If no remote lock is held or the time cannot be determined, the second parameter is false.
	GetRemoteLockedSince() (time.Duration, bool)

	// GetRemoteLockedBy returns who locked the remote lock, if available.
	GetRemoteLockedBy() (string, bool)
}

OutputAnalysis of the profile run

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

type Summary added in v0.12.0

type Summary struct {
	Duration        time.Duration
	FilesNew        int
	FilesChanged    int
	FilesUnmodified int
	DirsNew         int
	DirsChanged     int
	DirsUnmodified  int
	FilesTotal      int
	BytesAdded      uint64
	BytesTotal      uint64
	OutputAnalysis  OutputAnalysis
}

Summary of the profile run

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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