github.com/matthiasr/docker@v1.8.2/docker/docker.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "sort" 7 8 "github.com/Sirupsen/logrus" 9 "github.com/docker/docker/api/client" 10 "github.com/docker/docker/autogen/dockerversion" 11 "github.com/docker/docker/cli" 12 flag "github.com/docker/docker/pkg/mflag" 13 "github.com/docker/docker/pkg/reexec" 14 "github.com/docker/docker/pkg/term" 15 "github.com/docker/docker/utils" 16 ) 17 18 func main() { 19 if reexec.Init() { 20 return 21 } 22 23 // Set terminal emulation based on platform as required. 24 stdin, stdout, stderr := term.StdStreams() 25 26 logrus.SetOutput(stderr) 27 28 flag.Merge(flag.CommandLine, clientFlags.FlagSet, commonFlags.FlagSet) 29 30 flag.Usage = func() { 31 fmt.Fprint(os.Stdout, "Usage: docker [OPTIONS] COMMAND [arg...]\n"+daemonUsage+" docker [ --help | -v | --version ]\n\n") 32 fmt.Fprint(os.Stdout, "A self-sufficient runtime for containers.\n\nOptions:\n") 33 34 flag.CommandLine.SetOutput(os.Stdout) 35 flag.PrintDefaults() 36 37 help := "\nCommands:\n" 38 39 // TODO(tiborvass): no need to sort if we ensure dockerCommands is sorted 40 sort.Sort(byName(dockerCommands)) 41 42 for _, cmd := range dockerCommands { 43 help += fmt.Sprintf(" %-10.10s%s\n", cmd.name, cmd.description) 44 } 45 46 help += "\nRun 'docker COMMAND --help' for more information on a command." 47 fmt.Fprintf(os.Stdout, "%s\n", help) 48 } 49 50 flag.Parse() 51 52 if *flVersion { 53 showVersion() 54 return 55 } 56 57 clientCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags) 58 // TODO: remove once `-d` is retired 59 handleGlobalDaemonFlag() 60 61 if *flHelp { 62 // if global flag --help is present, regardless of what other options and commands there are, 63 // just print the usage. 64 flag.Usage() 65 return 66 } 67 68 c := cli.New(clientCli, daemonCli) 69 if err := c.Run(flag.Args()...); err != nil { 70 if sterr, ok := err.(cli.StatusError); ok { 71 if sterr.Status != "" { 72 fmt.Fprintln(os.Stderr, sterr.Status) 73 os.Exit(1) 74 } 75 os.Exit(sterr.StatusCode) 76 } 77 fmt.Fprintln(os.Stderr, err) 78 os.Exit(1) 79 } 80 } 81 82 func showVersion() { 83 if utils.ExperimentalBuild() { 84 fmt.Printf("Docker version %s, build %s, experimental\n", dockerversion.VERSION, dockerversion.GITCOMMIT) 85 } else { 86 fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT) 87 } 88 }