exec

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 15 Imported by: 0

README

exec

Overview

The exec package provides wraps the os/exec package to provide additional convienience methods for executing commands in Go, including capturing output, logging, and piping commands together.

Usage

To use exec, import the module in your Go project:

import "github.com/frostyeti/go/exec"

func main() {
    cmd := exec.New("ls", "-l")
    result, err := cmd.Run()
    if err != nil {
        panic(err)
    }
    println("Command output:", string(result.Stdout))

    o, err := exec.Run("git commit -m 'test'")
    if err != nil {
        panic(err)
    }
    if !o.IsOk() {
        err := o.ToError()
        panic(err)
    }

    o2, err := exec.Output("ls -l")
    if o.IsOk() {
        for _, line := range o2.Lines() {
            println(line)
        }
    }

    o3, err := exec.Command("echo 'Hello World'").PipeCommand("grep Hello").Output()
    if err != nil {
        panic(err)
    }
    println("Piped command output:", string(o3.Stdout))
}

Documentation

Index

Constants

View Source
const (
	STDIO_INHERIT = 0
	STDIO_PIPED   = 1
	STDIO_NULL    = 2
)
View Source
const (
	EOL = "\n" // POSIX line endings
)

Variables

View Source
var Registry = &ExecutableRegistry{data: make(map[string]Executable)}

Functions

func Find

func Find(name string, options *WhichOptions) (string, error)

func Register

func Register(name string, exe *Executable)

func SetEnvLike

func SetEnvLike(e EnvLike)

func SetLogger

func SetLogger(f func(cmd *Cmd))

func Which

func Which(command string) (string, bool)

func WhichFirst

func WhichFirst(command string, options *WhichOptions) (string, bool)

Types

type Cmd

type Cmd struct {
	*exec.Cmd

	TempFile *string
	// contains filtered or unexported fields
}

func Command

func Command(command string) *Cmd

Command parses the command and arguments and returns a new Cmd with the parsed command and arguments Example:

Command("echo hello world")
Command("echo 'hello world'")

func CommandContext

func CommandContext(ctx context.Context, command string) *Cmd

func New

func New(name string, args ...string) *Cmd

func NewContext

func NewContext(ctx context.Context, name string, args ...string) *Cmd

func (*Cmd) AppendArgs

func (c *Cmd) AppendArgs(args ...string) *Cmd

func (*Cmd) AppendEnv

func (c *Cmd) AppendEnv(env ...string) *Cmd

func (*Cmd) DisableLogger

func (c *Cmd) DisableLogger()

func (*Cmd) Output

func (c *Cmd) Output() (*Result, error)

Runs the command and captures the PsOutput PsOutputs are captured from the current process and are not inherited

func (*Cmd) Pipe

func (c *Cmd) Pipe(subcommands ...*Cmd) *Pipeline

func (*Cmd) PipeCommand

func (c *Cmd) PipeCommand(subcommands ...string) *Pipeline

func (*Cmd) PrependArgs

func (c *Cmd) PrependArgs(args ...string) *Cmd

func (*Cmd) PrependEnv

func (c *Cmd) PrependEnv(env ...string) *Cmd

func (*Cmd) Quiet

func (c *Cmd) Quiet() (*Result, error)

Runs the command quietly, without any PsOutput

func (*Cmd) Run

func (c *Cmd) Run() (*Result, error)

Runs the command and waits for it to finish PsOutputs are inherited from the current process and are not captured

func (*Cmd) SetLogger

func (c *Cmd) SetLogger(f func(cmd *Cmd))

func (*Cmd) Start

func (c *Cmd) Start() error

func (*Cmd) Wait

func (c *Cmd) Wait() error

func (*Cmd) WithArgs

func (c *Cmd) WithArgs(args ...string) *Cmd

func (*Cmd) WithCwd

func (c *Cmd) WithCwd(dir string) *Cmd

func (*Cmd) WithEnv

func (c *Cmd) WithEnv(env ...string) *Cmd

func (*Cmd) WithEnvMap

func (c *Cmd) WithEnvMap(env map[string]string) *Cmd

func (*Cmd) WithStderr

func (c *Cmd) WithStderr(stderr io.Writer) *Cmd

func (*Cmd) WithStdin

func (c *Cmd) WithStdin(stdin io.Reader) *Cmd

func (*Cmd) WithStdio

func (c *Cmd) WithStdio(stdin, stdout, stderr int) *Cmd

func (*Cmd) WithStdout

func (c *Cmd) WithStdout(stdout io.Writer) *Cmd

type EnvLike

type EnvLike interface {
	Get(key string) string
	Expand(s string) (string, error)
	Set(key, value string)
	SplitPath() []string
}

func GetEnvLike

func GetEnvLike() EnvLike

type Executable

type Executable struct {
	Name     string
	Path     string
	Variable string
	Windows  []string
	Linux    []string
	Darwin   []string
}

type ExecutableRegistry

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

func (*ExecutableRegistry) Find

func (r *ExecutableRegistry) Find(name string, options *WhichOptions) (string, error)

func (*ExecutableRegistry) Get

func (r *ExecutableRegistry) Get(name string) (*Executable, bool)

func (*ExecutableRegistry) Has

func (r *ExecutableRegistry) Has(name string) bool

func (*ExecutableRegistry) Register

func (r *ExecutableRegistry) Register(name string, exe *Executable)

func (*ExecutableRegistry) Set

func (r *ExecutableRegistry) Set(name string, exe *Executable)

type Pipeline

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

func (*Pipeline) Output

func (p *Pipeline) Output() (*Result, error)

func (*Pipeline) Pipe

func (p *Pipeline) Pipe(subcommands ...*Cmd) *Pipeline

func (*Pipeline) PipeCommand

func (p *Pipeline) PipeCommand(subcommands ...string) *Pipeline

func (*Pipeline) Run

func (p *Pipeline) Run() (*Result, error)

type Result

type Result struct {
	Stdout    []byte
	Stderr    []byte
	Code      int
	FileName  string
	Args      []string
	StartedAt time.Time
	EndedAt   time.Time
	TempFile  *string
}

func Output

func Output(command string) (*Result, error)

func Run

func Run(command string) (*Result, error)

func (*Result) ErrorJson

func (o *Result) ErrorJson() (interface{}, error)

func (*Result) ErrorLines

func (o *Result) ErrorLines() []string

func (*Result) ErrorText

func (o *Result) ErrorText() string

func (*Result) IsOk

func (o *Result) IsOk() bool

func (*Result) Json

func (o *Result) Json() (interface{}, error)

func (*Result) Lines

func (o *Result) Lines() []string

func (*Result) Text

func (o *Result) Text() string

func (*Result) ToError

func (o *Result) ToError() error

func (*Result) ToErrorIf

func (o *Result) ToErrorIf(f func(o *Result) bool) error

func (*Result) Validate

func (o *Result) Validate() (bool, error)

func (*Result) ValidateWith

func (o *Result) ValidateWith(cb func(o *Result) (bool, error)) (bool, error)

type WhichOptions

type WhichOptions struct {
	UseCache     bool
	PrependPaths []string
}

Jump to

Keyboard shortcuts

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