type Error … func (e *Error) Error() string { … } func (e *Error) Unwrap() error { … } var ErrWaitDelay … type wrappedError … func (w wrappedError) Error() string { … } func (w wrappedError) Unwrap() error { … } type Cmd … type ctxResult … var execwait … var execerrdot … // Command returns the [Cmd] struct to execute the named program with // the given arguments. // // It sets only the Path and Args in the returned structure. // // If name contains no path separators, Command uses [LookPath] to // resolve name to a complete path if possible. Otherwise it uses name // directly as Path. // // The returned Cmd's Args field is constructed from the command name // followed by the elements of arg, so arg should not include the // command name itself. For example, Command("echo", "hello"). // Args[0] is always name, not the possibly resolved Path. // // On Windows, processes receive the whole command line as a single string // and do their own parsing. Command combines and quotes Args into a command // line string with an algorithm compatible with applications using // CommandLineToArgvW (which is the most common way). Notable exceptions are // msiexec.exe and cmd.exe (and thus, all batch files), which have a different // unquoting algorithm. In these or other similar cases, you can do the // quoting yourself and provide the full command line in SysProcAttr.CmdLine, // leaving Args empty. func Command(name string, arg ...string) *Cmd { … } // CommandContext is like [Command] but includes a context. // // The provided context is used to interrupt the process // (by calling cmd.Cancel or [os.Process.Kill]) // if the context becomes done before the command completes on its own. // // CommandContext sets the command's Cancel function to invoke the Kill method // on its Process, and leaves its WaitDelay unset. The caller may change the // cancellation behavior by modifying those fields before starting the command. func CommandContext(ctx context.Context, name string, arg ...string) *Cmd { … } // String returns a human-readable description of c. // It is intended only for debugging. // In particular, it is not suitable for use as input to a shell. // The output of String may vary across Go releases. func (c *Cmd) String() string { … } // interfaceEqual protects against panics from doing equality tests on // two interfaces with non-comparable underlying types. func interfaceEqual(a, b any) bool { … } func (c *Cmd) argv() []string { … } func (c *Cmd) childStdin() (*os.File, error) { … } func (c *Cmd) childStdout() (*os.File, error) { … } func (c *Cmd) childStderr(childStdout *os.File) (*os.File, error) { … } // writerDescriptor returns an os.File to which the child process // can write to send data to w. // // If w is nil, writerDescriptor returns a File that writes to os.DevNull. func (c *Cmd) writerDescriptor(w io.Writer) (*os.File, error) { … } func closeDescriptors(closers []io.Closer) { … } // Run starts the specified command and waits for it to complete. // // The returned error is nil if the command runs, has no problems // copying stdin, stdout, and stderr, and exits with a zero exit // status. // // If the command starts but does not complete successfully, the error is of // type [*ExitError]. Other error types may be returned for other situations. // // If the calling goroutine has locked the operating system thread // with [runtime.LockOSThread] and modified any inheritable OS-level // thread state (for example, Linux or Plan 9 name spaces), the new // process will inherit the caller's thread state. func (c *Cmd) Run() error { … } // Start starts the specified command but does not wait for it to complete. // // If Start returns successfully, the c.Process field will be set. // // After a successful call to Start the [Cmd.Wait] method must be called in // order to release associated system resources. func (c *Cmd) Start() error { … } // watchCtx watches c.ctx until it is able to send a result to resultc. // // If c.ctx is done before a result can be sent, watchCtx calls c.Cancel, // and/or kills cmd.Process it after c.WaitDelay has elapsed. // // watchCtx manipulates c.goroutineErr, so its result must be received before // c.awaitGoroutines is called. func (c *Cmd) watchCtx(resultc chan<- ctxResult) { … } type ExitError … func (e *ExitError) Error() string { … } // Wait waits for the command to exit and waits for any copying to // stdin or copying from stdout or stderr to complete. // // The command must have been started by [Cmd.Start]. // // The returned error is nil if the command runs, has no problems // copying stdin, stdout, and stderr, and exits with a zero exit // status. // // If the command fails to run or doesn't complete successfully, the // error is of type [*ExitError]. Other error types may be // returned for I/O problems. // // If any of c.Stdin, c.Stdout or c.Stderr are not an [*os.File], Wait also waits // for the respective I/O loop copying to or from the process to complete. // // Wait releases any resources associated with the [Cmd]. func (c *Cmd) Wait() error { … } // awaitGoroutines waits for the results of the goroutines copying data to or // from the command's I/O pipes. // // If c.WaitDelay elapses before the goroutines complete, awaitGoroutines // forcibly closes their pipes and returns ErrWaitDelay. // // If timer is non-nil, it must send to timer.C at the end of c.WaitDelay. func (c *Cmd) awaitGoroutines(timer *time.Timer) error { … } // Output runs the command and returns its standard output. // Any returned error will usually be of type [*ExitError]. // If c.Stderr was nil, Output populates [ExitError.Stderr]. func (c *Cmd) Output() ([]byte, error) { … } // CombinedOutput runs the command and returns its combined standard // output and standard error. func (c *Cmd) CombinedOutput() ([]byte, error) { … } // StdinPipe returns a pipe that will be connected to the command's // standard input when the command starts. // The pipe will be closed automatically after [Cmd.Wait] sees the command exit. // A caller need only call Close to force the pipe to close sooner. // For example, if the command being run will not exit until standard input // is closed, the caller must close the pipe. func (c *Cmd) StdinPipe() (io.WriteCloser, error) { … } // StdoutPipe returns a pipe that will be connected to the command's // standard output when the command starts. // // [Cmd.Wait] will close the pipe after seeing the command exit, so most callers // need not close the pipe themselves. It is thus incorrect to call Wait // before all reads from the pipe have completed. // For the same reason, it is incorrect to call [Cmd.Run] when using StdoutPipe. // See the example for idiomatic usage. func (c *Cmd) StdoutPipe() (io.ReadCloser, error) { … } // StderrPipe returns a pipe that will be connected to the command's // standard error when the command starts. // // [Cmd.Wait] will close the pipe after seeing the command exit, so most callers // need not close the pipe themselves. It is thus incorrect to call Wait // before all reads from the pipe have completed. // For the same reason, it is incorrect to use [Cmd.Run] when using StderrPipe. // See the StdoutPipe example for idiomatic usage. func (c *Cmd) StderrPipe() (io.ReadCloser, error) { … } type prefixSuffixSaver … func (w *prefixSuffixSaver) Write(p []byte) (n int, err error) { … } // fill appends up to len(p) bytes of p to *dst, such that *dst does not // grow larger than w.N. It returns the un-appended suffix of p. func (w *prefixSuffixSaver) fill(dst *[]byte, p []byte) (pRemain []byte) { … } func (w *prefixSuffixSaver) Bytes() []byte { … } // environ returns a best-effort copy of the environment in which the command // would be run as it is currently configured. If an error occurs in computing // the environment, it is returned alongside the best-effort copy. func (c *Cmd) environ() ([]string, error) { … } // Environ returns a copy of the environment in which the command would be run // as it is currently configured. func (c *Cmd) Environ() []string { … } // dedupEnv returns a copy of env with any duplicates removed, in favor of // later values. // Items not of the normal environment "key=value" form are preserved unchanged. // Except on Plan 9, items containing NUL characters are removed, and // an error is returned along with the remaining values. func dedupEnv(env []string) ([]string, error) { … } // dedupEnvCase is dedupEnv with a case option for testing. // If caseInsensitive is true, the case of keys is ignored. // If nulOK is false, items containing NUL characters are allowed. func dedupEnvCase(caseInsensitive, nulOK bool, env []string) ([]string, error) { … } // addCriticalEnv adds any critical environment variables that are required // (or at least almost always required) on the operating system. // Currently this is only used for Windows. func addCriticalEnv(env []string) []string { … } var ErrDot …