execx

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2019 License: MIT Imports: 10 Imported by: 10

README

execx

CI GoDoc License

Make os/exec testable and graceful

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

cmd := execx.CommandContext(ctx, "sh", "-c", "sleep 5; echo done")
out, err := cmd.Output()

st := err.(*execx.ExitStatus)

fmt.Println(out, err, st.Signaled, st.Killed)

// Output: [] context deadline exceeded true false

Reference

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnimplemented = errors.New("unimplemented")
	ErrNotStarted    = errors.New("command not started")
)
View Source
var (
	DefaultGracePeriod    time.Duration  = 30 * time.Second
	DefaultErrorLog       Logger         = new(nopLogger)
	DefaultNewProcessFunc NewProcessFunc = NewOSProcess
)

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	*exec.Cmd
	*Config
	// contains filtered or unexported fields
}

func Command

func Command(cmd string, args ...string) *Cmd

func CommandContext

func CommandContext(ctx context.Context, cmd string, args ...string) *Cmd
Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/izumin5210/execx"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()

	cmd := execx.CommandContext(ctx, "sh", "-c", "sleep 5; echo done")
	out, err := cmd.Output()

	st := err.(*execx.ExitStatus)

	fmt.Println(out, err, st.Signaled, st.Killed)

}
Output:

[] signal: terminated true false

func (*Cmd) CombinedOutput

func (c *Cmd) CombinedOutput() ([]byte, error)

func (*Cmd) Output

func (c *Cmd) Output() ([]byte, error)

func (*Cmd) Run

func (c *Cmd) Run() error

func (*Cmd) Start

func (c *Cmd) Start() error

func (*Cmd) Wait

func (c *Cmd) Wait() error

type Config

type Config struct {
	GracePeriod time.Duration

	NewProcessFunc NewProcessFunc

	ErrorLog Logger
}

type Executor

type Executor struct {
	Config *Config
}

func New

func New(opts ...Option) *Executor

func (*Executor) Command

func (e *Executor) Command(cmd string, args ...string) *Cmd

func (*Executor) CommandContext

func (e *Executor) CommandContext(ctx context.Context, cmd string, args ...string) *Cmd

type ExitStatus

type ExitStatus struct {
	Code     int
	Signaled bool
	Killed   bool
	Timeout  bool
	Canceled bool
	Err      error
}

ExitStatus stores exit information of the command

func (*ExitStatus) Error

func (es *ExitStatus) Error() string

type FakeProcess

type FakeProcess struct {
	RunFunc func(ctx context.Context, cmd *exec.Cmd) error
	// contains filtered or unexported fields
}

func (*FakeProcess) Kill

func (p *FakeProcess) Kill() error

func (*FakeProcess) Signal

func (p *FakeProcess) Signal() os.Signal

func (*FakeProcess) Start

func (p *FakeProcess) Start() error

func (*FakeProcess) Terminate

func (p *FakeProcess) Terminate() error

func (*FakeProcess) Wait

func (p *FakeProcess) Wait() <-chan *ExitStatus

type Logger

type Logger interface {
	Print(...interface{})
}

type NewProcessFunc

type NewProcessFunc func(*exec.Cmd) Process

func NewFakeNewProcessFunc

func NewFakeNewProcessFunc(f func(ctx context.Context, cmd *exec.Cmd) error) NewProcessFunc

type Option

type Option func(*Config)

func WithErrorLog

func WithErrorLog(l Logger) Option

func WithFakeProcess

func WithFakeProcess(f func(context.Context, *exec.Cmd) error) Option

func WithGracePeriod

func WithGracePeriod(d time.Duration) Option

func WithNewProcessFunc

func WithNewProcessFunc(f NewProcessFunc) Option

type Process

type Process interface {
	Start() error
	Wait() <-chan *ExitStatus
	Terminate() error
	Kill() error
	Signal() os.Signal
}

func NewOSProcess

func NewOSProcess(cmd *exec.Cmd) Process

Jump to

Keyboard shortcuts

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