2016-02-19 17:42:51 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-04-25 12:05:42 -04:00
|
|
|
"path/filepath"
|
2016-02-19 17:42:51 -05:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/cli"
|
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"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2016-04-25 12:05:42 -04:00
|
|
|
var (
|
|
|
|
commonFlags = cliflags.InitCommonFlags()
|
|
|
|
clientFlags = initClientFlags(commonFlags)
|
|
|
|
flHelp = flag.Bool([]string{"h", "-help"}, false, "Print usage")
|
|
|
|
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
flag.Merge(flag.CommandLine, clientFlags.FlagSet, commonFlags.FlagSet)
|
|
|
|
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprint(stdout, "Usage: docker [OPTIONS] COMMAND [arg...]\n docker [ --help | -v | --version ]\n\n")
|
|
|
|
fmt.Fprint(stdout, "A self-sufficient runtime for containers.\n\nOptions:\n")
|
|
|
|
|
|
|
|
flag.CommandLine.SetOutput(stdout)
|
|
|
|
flag.PrintDefaults()
|
|
|
|
|
|
|
|
help := "\nCommands:\n"
|
|
|
|
|
2016-04-25 12:05:42 -04:00
|
|
|
dockerCommands := sortCommands(cli.DockerCommandUsage)
|
2016-02-19 17:42:51 -05:00
|
|
|
for _, cmd := range dockerCommands {
|
|
|
|
help += fmt.Sprintf(" %-10.10s%s\n", cmd.Name, cmd.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
help += "\nRun 'docker COMMAND --help' for more information on a command."
|
|
|
|
fmt.Fprintf(stdout, "%s\n", help)
|
|
|
|
}
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *flVersion {
|
|
|
|
showVersion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if *flHelp {
|
|
|
|
// if global flag --help is present, regardless of what other options and commands there are,
|
|
|
|
// just print the usage.
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags)
|
|
|
|
|
|
|
|
c := cli.New(clientCli, NewDaemonProxy())
|
|
|
|
if err := c.Run(flag.Args()...); err != nil {
|
|
|
|
if sterr, ok := err.(cli.StatusError); ok {
|
|
|
|
if sterr.Status != "" {
|
|
|
|
fmt.Fprintln(stderr, sterr.Status)
|
|
|
|
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
|
|
|
|
|
|
|
func initClientFlags(commonFlags *cliflags.CommonFlags) *cliflags.ClientFlags {
|
|
|
|
clientFlags := &cliflags.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags}
|
|
|
|
client := clientFlags.FlagSet
|
|
|
|
client.StringVar(&clientFlags.ConfigDir, []string{"-config"}, cliconfig.ConfigDir(), "Location of client config files")
|
|
|
|
|
|
|
|
clientFlags.PostParse = func() {
|
|
|
|
clientFlags.Common.PostParse()
|
|
|
|
|
|
|
|
if clientFlags.ConfigDir != "" {
|
|
|
|
cliconfig.SetConfigDir(clientFlags.ConfigDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if clientFlags.Common.TrustKey == "" {
|
|
|
|
clientFlags.Common.TrustKey = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if clientFlags.Common.Debug {
|
|
|
|
utils.EnableDebug()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clientFlags
|
|
|
|
}
|