cmdutils

package
v1.18.0-rc4 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrettyCommand

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

PrettyCommand takes arguments identical to Cmder.Command, it returns a pretty printed command that could be pasted into a shell

func RunCommandOutputToFileFunc

func RunCommandOutputToFileFunc(cmd Cmd, path string) func() error

RunCommandOutputToFileFunc returns a func that runs the given Cmd pipes its stdout to the file specified. If the file does not exist on the host, it will be created

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
}

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 added in v1.18.0

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) 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 added in v1.18.0

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 added in v1.18.0

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