type Cmd struct { // Path is the path of the command to run. // // This is the only field that must be set to a non-zero // value. If Path is relative, it is evaluated relative // to Dir. Path string
// Args holds command line arguments, including the command as Args[0]. // If the Args field is empty or nil, Run uses {Path}. // // In typical use, both Path and Args are set by calling Command. Args []string
// Env specifies the environment of the process. // Each entry is of the form "key=value". // If Env is nil, the new process uses the current process's // environment. // If Env contains duplicate environment keys, only the last // value in the slice for each duplicate key is used. // As a special case on Windows, SYSTEMROOT is always added if // missing and not explicitly set to the empty string. Env []string
// Dir specifies the working directory of the command. // If Dir is the empty string, Run runs the command in the // calling process's current directory. Dir string
// Stdin specifies the process's standard input. // // If Stdin is nil, the process reads from the null device (os.DevNull). // // If Stdin is an *os.File, the process's standard input is connected // directly to that file. // // Otherwise, during the execution of the command a separate // goroutine reads from Stdin and delivers that data to the command // over a pipe. In this case, Wait does not complete until the goroutine // stops copying, either because it has reached the end of Stdin // (EOF or a read error), or because writing to the pipe returned an error, // or because a nonzero WaitDelay was set and expired. Stdin io.Reader
// Stdout and Stderr specify the process's standard output and error. // // If either is nil, Run connects the corresponding file descriptor // to the null device (os.DevNull). // // If either is an *os.File, the corresponding output from the process // is connected directly to that file. // // Otherwise, during the execution of the command a separate goroutine // reads from the process over a pipe and delivers that data to the // corresponding Writer. In this case, Wait does not complete until the // goroutine reaches EOF or encounters an error or a nonzero WaitDelay // expires. // // If Stdout and Stderr are the same writer, and have a type that can // be compared with ==, at most one goroutine at a time will call Write. Stdout io.Writer Stderr io.Writer
// ExtraFiles specifies additional open files to be inherited by the // new process. It does not include standard input, standard output, or // standard error. If non-nil, entry i becomes file descriptor 3+i. // // ExtraFiles is not supported on Windows. ExtraFiles []*os.File
// SysProcAttr holds optional, operating system-specific attributes. // Run passes it to os.StartProcess as the os.ProcAttr's Sys field. SysProcAttr *syscall.SysProcAttr
// Process is the underlying process, once started. Process *os.Process
// ProcessState contains information about an exited process. // If the process was started successfully, Wait or Run will // populate its ProcessState when the command completes. ProcessState *os.ProcessState
Err error// LookPath error, if any.
// If Cancel is non-nil, the command must have been created with // CommandContext and Cancel will be called when the command's // Context is done. By default, CommandContext sets Cancel to // call the Kill method on the command's Process. // // Typically a custom Cancel will send a signal to the command's // Process, but it may instead take other actions to initiate cancellation, // such as closing a stdin or stdout pipe or sending a shutdown request on a // network socket. // // If the command exits with a success status after Cancel is // called, and Cancel does not return an error equivalent to // os.ErrProcessDone, then Wait and similar methods will return a non-nil // error: either an error wrapping the one returned by Cancel, // or the error from the Context. // (If the command exits with a non-success status, or Cancel // returns an error that wraps os.ErrProcessDone, Wait and similar methods // continue to return the command's usual exit status.) // // If Cancel is set to nil, nothing will happen immediately when the command's // Context is done, but a nonzero WaitDelay will still take effect. That may // be useful, for example, to work around deadlocks in commands that do not // support shutdown signals but are expected to always finish quickly. // // Cancel will not be called if Start returns a non-nil error. Cancel func()error
// If WaitDelay is non-zero, it bounds the time spent waiting on two sources // of unexpected delay in Wait: a child process that fails to exit after the // associated Context is canceled, and a child process that exits but leaves // its I/O pipes unclosed. // // The WaitDelay timer starts when either the associated Context is done or a // call to Wait observes that the child process has exited, whichever occurs // first. When the delay has elapsed, the command shuts down the child process // and/or its I/O pipes. // // If the child process has failed to exit — perhaps because it ignored or // failed to receive a shutdown signal from a Cancel function, or because no // Cancel function was set — then it will be terminated using os.Process.Kill. // // Then, if the I/O pipes communicating with the child process are still open, // those pipes are closed in order to unblock any goroutines currently blocked // on Read or Write calls. // // If pipes are closed due to WaitDelay, no Cancel call has occurred, // and the command has otherwise exited with a successful status, Wait and // similar methods will return ErrWaitDelay instead of nil. // // If WaitDelay is zero (the default), I/O pipes will be read until EOF, // which might not occur until orphaned subprocesses of the command have // also closed their descriptors for the pipes. WaitDelay time.Duration // contains filtered or unexported fields }
LookPath 查找可执行文件
1
funcLookPath(file string) (string, error)
其会通过 PATH 环境变量来查找对应文件, 返回的是绝对路径.
Command 设置命令以及 args
1
funcCommand(name string, arg ...string) *Cmd
如果 name 中不包含 path separator, 其会自动调用 LookPath 来查找可执行文件.