2016-02-19 17:42:51 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-16 19:38:28 -05:00
|
|
|
"errors"
|
2016-02-19 17:42:51 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-11-02 20:43:32 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-02-19 17:42:51 -05:00
|
|
|
"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-11-04 22:45:15 -04:00
|
|
|
Use: "docker [OPTIONS] COMMAND [ARG...]",
|
2016-10-31 23:07:31 -04:00
|
|
|
Short: "A self-sufficient runtime for containers",
|
2016-06-22 18:36:51 -04:00
|
|
|
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
|
|
|
|
}
|
2016-11-17 13:54:10 -05:00
|
|
|
return dockerCli.ShowHelp(cmd, args)
|
2016-06-22 13:08:04 -04:00
|
|
|
},
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2016-11-16 16:19:45 -05:00
|
|
|
// daemon command is special, we redirect directly to another binary
|
|
|
|
if cmd.Name() == "daemon" {
|
|
|
|
return nil
|
|
|
|
}
|
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-11-16 19:38:28 -05:00
|
|
|
if err := dockerCli.Initialize(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return isSupported(cmd, dockerCli.Client().ClientVersion(), dockerCli.HasExperimental())
|
2016-06-22 13:08:04 -04:00
|
|
|
},
|
|
|
|
}
|
2016-06-23 11:25:51 -04:00
|
|
|
cli.SetupRootCommand(cmd)
|
2016-06-22 13:08:04 -04:00
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
cmd.SetHelpFunc(func(ccmd *cobra.Command, args []string) {
|
2016-11-16 19:38:28 -05:00
|
|
|
if dockerCli.Client() == nil { // when using --help, PersistenPreRun is not called, so initialization is needed.
|
2016-11-02 20:43:32 -04:00
|
|
|
// flags must be the top-level command flags, not cmd.Flags()
|
|
|
|
opts.Common.SetDefaultOptions(flags)
|
|
|
|
dockerPreRun(opts)
|
2016-11-02 20:43:32 -04:00
|
|
|
dockerCli.Initialize(opts)
|
2016-11-02 20:43:32 -04:00
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
|
2016-11-16 19:38:28 -05:00
|
|
|
if err := isSupported(ccmd, dockerCli.Client().ClientVersion(), dockerCli.HasExperimental()); err != nil {
|
|
|
|
ccmd.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
hideUnsupportedFeatures(ccmd, dockerCli.Client().ClientVersion(), dockerCli.HasExperimental())
|
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
if err := ccmd.Help(); err != nil {
|
|
|
|
ccmd.Println(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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(
|
2016-11-18 18:57:11 -05:00
|
|
|
"docker: '%s' is not a docker command.\nSee 'docker --help'", args[0])
|
2016-08-03 12:20:46 -04:00
|
|
|
}
|
|
|
|
|
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() {
|
2016-10-06 10:09:54 -04:00
|
|
|
fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
|
2016-02-19 17:42:51 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
func hideUnsupportedFeatures(cmd *cobra.Command, clientVersion string, hasExperimental bool) {
|
2016-11-02 20:43:32 -04:00
|
|
|
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
2016-11-02 20:43:32 -04:00
|
|
|
// hide experimental flags
|
2016-11-11 20:43:06 -05:00
|
|
|
if !hasExperimental {
|
|
|
|
if _, ok := f.Annotations["experimental"]; ok {
|
|
|
|
f.Hidden = true
|
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
|
|
|
|
// hide flags not supported by the server
|
|
|
|
if flagVersion, ok := f.Annotations["version"]; ok && len(flagVersion) == 1 && versions.LessThan(clientVersion, flagVersion[0]) {
|
|
|
|
f.Hidden = true
|
|
|
|
}
|
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
for _, subcmd := range cmd.Commands() {
|
2016-11-02 20:43:32 -04:00
|
|
|
// hide experimental subcommands
|
2016-11-11 20:43:06 -05:00
|
|
|
if !hasExperimental {
|
|
|
|
if _, ok := subcmd.Tags["experimental"]; ok {
|
|
|
|
subcmd.Hidden = true
|
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
|
|
|
|
// hide subcommands not supported by the server
|
|
|
|
if subcmdVersion, ok := subcmd.Tags["version"]; ok && versions.LessThan(clientVersion, subcmdVersion) {
|
|
|
|
subcmd.Hidden = true
|
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-16 19:38:28 -05:00
|
|
|
|
|
|
|
func isSupported(cmd *cobra.Command, clientVersion string, hasExperimental bool) error {
|
|
|
|
if !hasExperimental {
|
|
|
|
if _, ok := cmd.Tags["experimental"]; ok {
|
|
|
|
return errors.New("only supported with experimental daemon")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmdVersion, ok := cmd.Tags["version"]; ok && versions.LessThan(clientVersion, cmdVersion) {
|
|
|
|
return fmt.Errorf("only supported with daemon version >= %s", cmdVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|