Documentation
¶
Index ¶
- func KillProcessGroup(p *exec.Cmd) error
- func MergeArgs(sets ...[]string) []string
- func SetProcessGroup(p *exec.Cmd)
- func TerminateProcessGroup(p *exec.Cmd) error
- func UseProcessGroup(p *exec.Cmd)
- type Command
- type Option
- func WithCancel(fn func(*os.Process) error) Option
- func WithContext(ctx context.Context) Option
- func WithDir(dir string) Option
- func WithEnv(envs ...string) Option
- func WithInput(inputs ...io.Reader) Option
- func WithMaxBuffer(n int) Option
- func WithProcessGroup() Option
- func WithStderr(w io.Writer) Option
- func WithStdout(w io.Writer) Option
- func WithWaitDelay(d time.Duration) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func KillProcessGroup ¶ added in v0.8.0
A group already gone is not an error.
func MergeArgs ¶ added in v0.4.13
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 TerminateProcessGroup ¶ added in v0.8.0
A group already gone is not an error.
func UseProcessGroup ¶ added in v0.7.0
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
}
type Option ¶
type Option func(*cmd)
func WithContext ¶
func WithInput ¶
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 WithProcessGroup ¶ added in v0.7.0
func WithProcessGroup() Option