1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/runconfig/exec.go
Dan Walsh a2b529ead2 --help option and help command should print to stdout not stderr
--help and help are successful commands so output should not go to error.

    QE teams have requested this change, also users doing docker help | less
    or docker run --help | less would expect this to work.

    Usage statement should only be printed when the user asks for it.
    Errors should print error message and then suggest the docker COMMAND --help
    command to see usage information.

    The current behaviour causes the user to have to search for the error message
    and sometimes scrolls right off the screen.  For example a error on a
    "docker run" command is very difficult to diagnose.

    Finally erros should always exit with a non 0 exit code, if the user
    makes a CLI error.

Docker-DCO-1.1-Signed-off-by: Dan Walsh <dwalsh@redhat.com> (github: rhatdan)
2015-01-06 13:40:14 +01:00

88 lines
2.1 KiB
Go

package runconfig
import (
"fmt"
"github.com/docker/docker/engine"
flag "github.com/docker/docker/pkg/mflag"
"os"
)
type ExecConfig struct {
User string
Privileged bool
Tty bool
Container string
AttachStdin bool
AttachStderr bool
AttachStdout bool
Detach bool
Cmd []string
}
func ExecConfigFromJob(job *engine.Job) (*ExecConfig, error) {
execConfig := &ExecConfig{
// TODO(vishh): Expose 'User' once it is supported.
//User: job.Getenv("User"),
// TODO(vishh): Expose 'Privileged' once it is supported.
//Privileged: job.GetenvBool("Privileged"),
Tty: job.GetenvBool("Tty"),
AttachStdin: job.GetenvBool("AttachStdin"),
AttachStderr: job.GetenvBool("AttachStderr"),
AttachStdout: job.GetenvBool("AttachStdout"),
}
cmd := job.GetenvList("Cmd")
if len(cmd) == 0 {
return nil, fmt.Errorf("No exec command specified")
}
execConfig.Cmd = cmd
return execConfig, nil
}
func ParseExec(cmd *flag.FlagSet, args []string) (*ExecConfig, error) {
var (
flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
help = cmd.Bool([]string{"#help", "-help"}, false, "Print usage")
execCmd []string
container string
)
if err := cmd.Parse(args); err != nil {
return nil, err
}
if *help {
cmd.Usage()
return nil, nil
}
if cmd.BadArgs(flag.Min, 2) {
os.Exit(1)
}
container = cmd.Arg(0)
parsedArgs := cmd.Args()
execCmd = parsedArgs[1:]
execConfig := &ExecConfig{
// TODO(vishh): Expose '-u' flag once it is supported.
User: "",
// TODO(vishh): Expose '-p' flag once it is supported.
Privileged: false,
Tty: *flTty,
Cmd: execCmd,
Container: container,
Detach: *flDetach,
}
// If -d is not set, attach to everything by default
if !*flDetach {
execConfig.AttachStdout = true
execConfig.AttachStderr = true
if *flStdin {
execConfig.AttachStdin = true
}
}
return execConfig, nil
}