2016-02-19 17:42:51 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/commands"
|
2016-04-25 12:05:42 -04:00
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
|
|
|
"github.com/docker/docker/cliconfig"
|
2016-02-19 17:42:51 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/docker/docker/utils"
|
2016-06-22 13:08:04 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
2016-02-19 17:42:51 -05:00
|
|
|
)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := cliflags.NewClientOptions()
|
2016-08-03 12:20:46 -04:00
|
|
|
var flags *pflag.FlagSet
|
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
cmd := &cobra.Command{
|
2016-06-22 18:36:51 -04:00
|
|
|
Use: "docker [OPTIONS] COMMAND [arg...]",
|
|
|
|
Short: "A self-sufficient runtime for containers.",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
TraverseChildren: true,
|
2016-08-03 12:20:46 -04:00
|
|
|
Args: noArgs,
|
2016-06-22 13:08:04 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if opts.Version {
|
|
|
|
showVersion()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2016-08-03 12:20:46 -04:00
|
|
|
// flags must be the top-level command flags, not cmd.Flags()
|
|
|
|
opts.Common.SetDefaultOptions(flags)
|
|
|
|
dockerPreRun(opts)
|
2016-06-22 13:08:04 -04:00
|
|
|
return dockerCli.Initialize(opts)
|
|
|
|
},
|
|
|
|
}
|
2016-06-23 11:25:51 -04:00
|
|
|
cli.SetupRootCommand(cmd)
|
2016-06-22 13:08:04 -04:00
|
|
|
|
2016-08-03 12:20:46 -04:00
|
|
|
flags = cmd.Flags()
|
2016-06-22 13:08:04 -04:00
|
|
|
flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
|
|
|
|
flags.StringVar(&opts.ConfigDir, "config", cliconfig.ConfigDir(), "Location of client config files")
|
|
|
|
opts.Common.InstallFlags(flags)
|
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
cmd.SetOutput(dockerCli.Out())
|
2016-06-23 11:25:51 -04:00
|
|
|
cmd.AddCommand(newDaemonCommand())
|
2016-09-08 13:11:39 -04:00
|
|
|
commands.AddCommands(cmd, dockerCli)
|
2016-06-22 18:36:51 -04:00
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
return cmd
|
|
|
|
}
|
2016-04-25 12:05:42 -04:00
|
|
|
|
2016-08-03 12:20:46 -04:00
|
|
|
func noArgs(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf(
|
|
|
|
"docker: '%s' is not a docker command.\nSee 'docker --help'%s", args[0], ".")
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
func main() {
|
|
|
|
// Set terminal emulation based on platform as required.
|
|
|
|
stdin, stdout, stderr := term.StdStreams()
|
|
|
|
logrus.SetOutput(stderr)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
dockerCli := command.NewDockerCli(stdin, stdout, stderr)
|
2016-06-22 18:36:51 -04:00
|
|
|
cmd := newDockerCommand(dockerCli)
|
2016-02-19 17:42:51 -05:00
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
if err := cmd.Execute(); err != nil {
|
2016-02-19 17:42:51 -05:00
|
|
|
if sterr, ok := err.(cli.StatusError); ok {
|
|
|
|
if sterr.Status != "" {
|
|
|
|
fmt.Fprintln(stderr, sterr.Status)
|
2016-06-01 01:19:13 -04:00
|
|
|
}
|
|
|
|
// StatusError should only be used for errors, and all errors should
|
|
|
|
// have a non-zero exit status, so never exit with 0
|
|
|
|
if sterr.StatusCode == 0 {
|
2016-02-19 17:42:51 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(sterr.StatusCode)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showVersion() {
|
|
|
|
if utils.ExperimentalBuild() {
|
|
|
|
fmt.Printf("Docker version %s, build %s, experimental\n", dockerversion.Version, dockerversion.GitCommit)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 12:05:42 -04:00
|
|
|
|
2016-08-03 12:20:46 -04:00
|
|
|
func dockerPreRun(opts *cliflags.ClientOptions) {
|
2016-10-11 07:35:12 -04:00
|
|
|
cliflags.SetLogLevel(opts.Common.LogLevel)
|
2016-04-25 12:05:42 -04:00
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
if opts.ConfigDir != "" {
|
|
|
|
cliconfig.SetConfigDir(opts.ConfigDir)
|
|
|
|
}
|
2016-04-25 12:05:42 -04:00
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
if opts.Common.Debug {
|
|
|
|
utils.EnableDebug()
|
2016-04-25 12:05:42 -04:00
|
|
|
}
|
|
|
|
}
|