cmdutils

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrettyCommand

func PrettyCommand(quoteAll bool, name string, args ...string) string

PrettyCommand takes arguments identical to Cmder.Command, with a leading quote flag. Behavior: - quoteAll == true: quote all tokens (command and all args) - quoteAll == false: print tokens unquoted, but quote any arg that contains whitespace It returns a pretty printed command that could be pasted into a shell.

Types

type Cmd

type Cmd interface {
	// Run executes the command (like os/exec.Cmd.Run)
	// It returns a *RunError if there is any error, nil otherwise
	Run() *RunError

	// Start starts the command but doesn't block
	// If the returned error is non-nil, it should be of type *RunError
	Start() *RunError

	// Wait waits for the command to finish
	// If the returned error is non-nil, it should be of type *RunError
	Wait() *RunError

	// Output returns the output of the executed command
	Output() []byte

	// WithEnv sets the Env variables for the Cmd
	// Each entry should be of the form "key=value"
	WithEnv(...string) Cmd

	// WithStdin sets the io.Reader used for stdin
	WithStdin(reader io.Reader) Cmd

	// WithStdout sets the io.Writer used for stdout
	WithStdout(io.Writer) Cmd

	// WithStderr sets the io.Reader used for stderr
	WithStderr(io.Writer) Cmd

	PrettyCommand() string
}

Cmd abstracts over running a command somewhere

func Command

func Command(ctx context.Context, command string, args ...string) Cmd

Command is a convenience wrapper over defaultCmder.Command

type Cmder

type Cmder interface {
	Command(ctx context.Context, name string, args ...string) Cmd
}

Cmder abstracts over creating commands

type LocalCmd

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

LocalCmd wraps os/exec.Cmd, implementing the cmdutils.Cmd interface

func (*LocalCmd) Output

func (cmd *LocalCmd) Output() []byte

Output returns the output of the command If the returned error is non-nil, it should be of type *RunError

func (*LocalCmd) PrettyCommand

func (cmd *LocalCmd) PrettyCommand() string

func (*LocalCmd) Run

func (cmd *LocalCmd) Run() *RunError

Run runs the command If the returned error is non-nil, it should be of type *RunError

func (*LocalCmd) Start

func (cmd *LocalCmd) Start() *RunError

Start starts the command but doesn't block If the returned error is non-nil, it should be of type *RunError

func (*LocalCmd) Wait

func (cmd *LocalCmd) Wait() *RunError

Wait waits for the command to finish If the returned error is non-nil, it should be of type *RunError

func (*LocalCmd) WithEnv

func (cmd *LocalCmd) WithEnv(env ...string) Cmd

WithEnv sets env

func (*LocalCmd) WithStderr

func (cmd *LocalCmd) WithStderr(w io.Writer) Cmd

WithStderr sets stderr

func (*LocalCmd) WithStdin

func (cmd *LocalCmd) WithStdin(r io.Reader) Cmd

WithStdin sets stdin

func (*LocalCmd) WithStdout

func (cmd *LocalCmd) WithStdout(w io.Writer) Cmd

WithStdout set stdout

type LocalCmder

type LocalCmder struct{}

LocalCmder is a factory for LocalCmd, implementing Cmder

func (*LocalCmder) Command

func (c *LocalCmder) Command(ctx context.Context, name string, arg ...string) Cmd

Command returns a Cmd which includes the running process's `Environment`

type RunError

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

RunError represents an error running a Cmd

func (*RunError) Cause

func (e *RunError) Cause() error

Cause mimics github.com/pkg/errors's Cause pattern for errors

func (*RunError) Error

func (e *RunError) Error() string

func (*RunError) OutputString

func (e *RunError) OutputString() string

func (*RunError) PrettyCommand

func (e *RunError) PrettyCommand() string

PrettyCommand pretty prints the command in a way that could be pasted into a shell

Jump to

Keyboard shortcuts

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