runutil

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 17 Imported by: 3

README

GoDoc

runutil

Various useful tools for running and pipe-ing stuff outside of Go.

This library makes it very easy to execute complex sequences of executables mixing both go native methods like gzip.NewReader and UNIX commands in the way pipes work on bash/etc.

For example it is possible to run the following:

compressed, err := RunPipe(input, "gzip", "-9")
if err != nil {
	t.Errorf("failed to run test: %s", err)
	return
}

res, err = gzip.NewReader(compressed)
if err != nil {
	t.Errorf("failed to run test: %s", err)
	return
}

Reading from res in that example will return the exact same bytes as input, after having been compressed once (into compressed), then decompressed.

If the command fails, the final Read() call will return the failure code, and allows correctly catching any problem (by default, go os/exec will only return the error when calling Wait(), which may result in errors not being catched).

Help

There are a lot of zombie threads

This means Wait() was not called. If using a method returning a pipe, you need to read the pipe to EOF in order for resources to be cleared. Another option is to call defer pipe.Close() in order to ensure resources are freed.

Close() will return quickly and kill the process, however if you want to wait and give the process some time, defer pipe.CloseWait(ctx) can be used. If the context has a deadline the process will be killed as per the deadline.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCommandMissing = errors.New("command is missing")
	ErrNotSupported   = errors.New("operation not supported on this platform")
)

Functions

func PidOf added in v0.1.0

func PidOf(name string) (res []int)

func Reap added in v0.0.4

func Reap() error

func Run

func Run(arg ...string) error

Run is a very simple invokation of command run, with output forwarded to stdout. This will wait for the command to complete.

func RunGet

func RunGet(arg ...string) ([]byte, error)

RunGet executes the command and returns its output as a buffer after it completes.

func RunJson

func RunJson(obj interface{}, arg ...string) error

RunJson executes the command and applies its output to the specified object, parsing json data

func RunWrite

func RunWrite(r io.Reader, arg ...string) error

RunWrite executes the command and passes r as its input, waiting for it to complete.

func Sh

func Sh(cmd string) error

Sh runs a shell command (linux only)

func ShQuote

func ShQuote(s string) string

Types

type Env added in v0.2.3

type Env []string

func NewEnv added in v0.2.3

func NewEnv(home string, vars ...string) Env

NewEnv returns an empty env with only HOME, PATH set

func SysEnv added in v0.2.3

func SysEnv() Env

SysEnv returns a nil Env object that would mean pointing to the OS's environ

func (Env) Contains added in v0.2.3

func (e Env) Contains(k string) bool

Contains checks if the given env contains any value k, and confirm if the value exists or not

func (Env) Dedup added in v0.2.3

func (e Env) Dedup() Env

Dedup returns a copy of e with any duplicate value removed

func (Env) Get added in v0.2.3

func (e Env) Get(k string) string

Get returns the requested value, or empty if none was found

func (Env) Join added in v0.2.3

func (e Env) Join(others ...Env) Env

Join appends in one shot multiple environments together. No check for duplicates is done

func (Env) Run added in v0.2.3

func (e Env) Run(arg ...string) error

Run is a very simple invokation of command run, with output forwarded to stdout. This will wait for the command to complete.

func (Env) RunGet added in v0.2.3

func (e Env) RunGet(arg ...string) ([]byte, error)

RunGet executes the command and returns its output as a buffer after it completes.

func (Env) RunJson added in v0.2.3

func (e Env) RunJson(obj interface{}, arg ...string) error

RunJson executes the command and applies its output to the specified object, parsing json data

func (Env) RunPipe added in v0.2.3

func (e Env) RunPipe(r io.Reader, arg ...string) (Pipe, error)

RunPipe runs a command in background, connecting both ends

func (Env) RunRead added in v0.2.3

func (e Env) RunRead(arg ...string) (Pipe, error)

RunRead executes the command in background and returns its output as a stream. Close the stream to kill the command and release its resources.

func (Env) RunWrite added in v0.2.3

func (e Env) RunWrite(r io.Reader, arg ...string) error

RunWrite executes the command and passes r as its input, waiting for it to complete.

func (*Env) Set added in v0.2.3

func (e *Env) Set(k, v string)

Set sets the given variable in the env

func (*Env) Unset added in v0.2.3

func (e *Env) Unset(k string)

Unset removes any instances of a given value from the env

type LinuxProcState added in v0.2.0

type LinuxProcState struct {
	Pid         int
	Comm        string // "bash", etc
	State       byte   // 'R', 'S', 'D', 'Z', 'T', 't', 'W', etc
	PPid        int    // parent pid
	PGrp        int    // group id
	Session     int    // session
	TtyNr       int    // tty number
	Tpgid       int
	Flags       uint   // PF_* flags
	Minflt      uint64 // faults
	Cminflt     uint64
	Majflt      uint64
	Cmajflt     uint64
	Utime       uint64
	Stime       uint64
	Cutime      uint64
	Cstime      uint64
	Priority    int64
	Nice        int64 // range 19 (low priority) to -20 (high priority).
	NumThreads  int64
	Itrealvalue int64  // The time in jiffies before the next SIGALRM
	StartTime   uint64 // The time the process started after system boot in clock ticks (_SYSTEM_CLK_TCK)
	Vsize       uint64
	RSS         int64
	RSSlim      uint64
}

func LinuxPidState added in v0.2.0

func LinuxPidState(pid uint64) (*LinuxProcState, error)

func (*LinuxProcState) IsRunning added in v0.2.0

func (s *LinuxProcState) IsRunning() bool

func (*LinuxProcState) Started added in v0.2.0

func (s *LinuxProcState) Started() (time.Time, error)

type Pipe added in v0.0.4

type Pipe interface {
	io.ReadCloser
	CopyTo(io.Writer) (int64, error)
	CloseWait(ctx context.Context) error
}

func RunPipe

func RunPipe(r io.Reader, arg ...string) (Pipe, error)

RunPipe runs a command in background, connecting both ends

func RunRead

func RunRead(arg ...string) (Pipe, error)

RunRead executes the command in background and returns its output as a stream. Close the stream to kill the command and release its resources.

type ProcState added in v0.2.0

type ProcState interface {
	IsRunning() bool
	Started() (time.Time, error)
}

func PidState added in v0.2.0

func PidState(pid uint64) (ProcState, error)

Jump to

Keyboard shortcuts

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