cmdutil

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KillProcessGroup added in v0.8.0

func KillProcessGroup(p *exec.Cmd) error

A group already gone is not an error.

func MergeArgs added in v0.4.13

func MergeArgs(sets ...[]string) []string

This function merges argument sets left to right, so a later set overrides an earlier one. A token longer than one character and starting with "-" is a flag; following non-flag tokens are its values. A repeated flag keeps its first-seen position and takes its last-seen value list, so MergeArgs([]string{"-a", "-b", "2", "-b", "3"}) returns ["-a", "-b", "3"].

The --flag=value form splits on the first "=" and merges with the space form of the same flag; the emitted form follows the last-seen occurrence. Bare "-" and tokens with no flag open are positionals, passed through in place. A flag does not stay open across a set boundary.

The result is newly allocated and never aliases an input. It has a time complexity of O(n) in the total number of tokens.

This function infers flag arity positionally, because an argument list does not say which flags take a value. Two consequences:

  • Repeatable flags collapse: ["-v", "-v"] returns ["-v"], and ["--header", "A", "--header", "B"] returns ["--header", "B"]. Do not use MergeArgs for tools whose flags accumulate.
  • A trailing positional is absorbed as the preceding flag's value: ["--verbose", "file.txt"] parses file.txt as --verbose's value. There is no "--" terminator to guard against this.

func SetProcessGroup added in v0.8.0

func SetProcessGroup(p *exec.Cmd)

func TerminateProcessGroup added in v0.8.0

func TerminateProcessGroup(p *exec.Cmd) error

A group already gone is not an error.

func UseProcessGroup added in v0.7.0

func UseProcessGroup(p *exec.Cmd)

UseProcessGroup is for a command made with exec.CommandContext, as exec refuses its Cancel hook otherwise: canceling the context sends the whole group a SIGTERM.

Types

type Command

type Command interface {
	// Start starts the command and returns without waiting for it to end.
	Start() error
	// Wait waits for the started command to end.
	Wait() error
	// Run starts the command and waits for it to end.
	Run() error
	// Terminate sends the command a SIGTERM, to its whole process group with
	// WithProcessGroup; where there are no signals it kills it. A command that already ended
	// is not an error.
	Terminate() error
	// Kill sends the command, or its process group, a SIGKILL.
	Kill() error
	// Output and Error hold what the command wrote to stdout and stderr, for a stream given
	// no writer; WithMaxBuffer bounds them.
	Output() string
	Error() string
	ExitCode() int
	String() string
}

func New

func New(bin string, args []string, opts ...Option) Command

type Option

type Option func(*cmd)

func WithCancel added in v0.3.13

func WithCancel(fn func(*os.Process) error) Option

func WithContext

func WithContext(ctx context.Context) Option

func WithDir added in v0.3.13

func WithDir(dir string) Option

func WithEnv

func WithEnv(envs ...string) Option

func WithInput

func WithInput(inputs ...io.Reader) Option

WithInput feeds the command, and calling it with no readers hands over the parent's stdin. That substitution stays here rather than in setDefaults - the exception to the rule - because it turns on the *call* having happened, which a zero field cannot express: never calling WithInput leaves in nil, and exec reads such a command from the null device.

func WithMaxBuffer added in v0.3.13

func WithMaxBuffer(n int) Option

func WithProcessGroup added in v0.7.0

func WithProcessGroup() Option

func WithStderr added in v0.3.13

func WithStderr(w io.Writer) Option

func WithStdout added in v0.3.13

func WithStdout(w io.Writer) Option

func WithWaitDelay added in v0.3.13

func WithWaitDelay(d time.Duration) Option

Jump to

Keyboard shortcuts

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