bashexec

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package bashexec provides helpers to execute bash code. What this primarily offers over directly writing e.g. `exec.Command("bash")` is:

  • By default, all fragments are executed in "bash strict mode": http://redsymbol.net/articles/unofficial-bash-strict-mode/
  • The code encourages adding a "name" for in-memory scripts, similar to e.g. Ansible tasks as well as many CI systems like Github actions
  • The code to execute is piped to stdin instead of passed via `-c` which avoids argument length limits and makes the output of e.g. `ps` readable.
  • Scripts are assumed synchronous, and stdin/stdout/stderr are passed directly instead of piped.
  • We use prctl(PR_SET_PDEATHSIG) (assuming Linux) to lifecycle bind the script to the caller

Index

Constants

View Source
const StrictMode = "set -euo pipefail"

StrictMode enables http://redsymbol.net/articles/unofficial-bash-strict-mode/

Variables

This section is empty.

Functions

func Run

func Run(name, cmd string) error

Run spawns a named script (without any arguments), gathering stdout/stderr into a buffer that is displayed only on error.

func RunA

func RunA(cmd string) error

RunA spawns an anonymous script, and is otherwise the same as `Run`.

Types

type BashRunner

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

BashRunner is a wrapper for executing in-memory bash scripts

func NewBashRunner

func NewBashRunner(name, src string, args ...string) (*BashRunner, error)

NewBashRunner creates a bash executor from in-memory shell script.

func (*BashRunner) Exec

func (r *BashRunner) Exec() error

Exec synchronously spawns the child process, passing stdin/stdout/stderr directly.

func (*BashRunner) Run

func (r *BashRunner) Run() error

Run spawns the script, gathering stdout/stderr into a buffer that is displayed only on error.

Jump to

Keyboard shortcuts

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