shell

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: GPL-3.0 Imports: 12 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 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 {
	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()
		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
}

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