github.com/go/docker@v1.12.0-rc2/cmd/docker/docker.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/Sirupsen/logrus"
     9  	"github.com/docker/docker/api/client"
    10  	"github.com/docker/docker/cli"
    11  	"github.com/docker/docker/cli/cobraadaptor"
    12  	cliflags "github.com/docker/docker/cli/flags"
    13  	"github.com/docker/docker/cliconfig"
    14  	"github.com/docker/docker/dockerversion"
    15  	flag "github.com/docker/docker/pkg/mflag"
    16  	"github.com/docker/docker/pkg/term"
    17  	"github.com/docker/docker/utils"
    18  )
    19  
    20  var (
    21  	commonFlags = cliflags.InitCommonFlags()
    22  	clientFlags = initClientFlags(commonFlags)
    23  	flHelp      = flag.Bool([]string{"h", "-help"}, false, "Print usage")
    24  	flVersion   = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
    25  )
    26  
    27  func main() {
    28  	// Set terminal emulation based on platform as required.
    29  	stdin, stdout, stderr := term.StdStreams()
    30  
    31  	logrus.SetOutput(stderr)
    32  
    33  	flag.Merge(flag.CommandLine, clientFlags.FlagSet, commonFlags.FlagSet)
    34  
    35  	cobraAdaptor := cobraadaptor.NewCobraAdaptor(clientFlags)
    36  
    37  	flag.Usage = func() {
    38  		fmt.Fprint(stdout, "Usage: docker [OPTIONS] COMMAND [arg...]\n       docker [ --help | -v | --version ]\n\n")
    39  		fmt.Fprint(stdout, "A self-sufficient runtime for containers.\n\nOptions:\n")
    40  
    41  		flag.CommandLine.SetOutput(stdout)
    42  		flag.PrintDefaults()
    43  
    44  		help := "\nCommands:\n"
    45  
    46  		dockerCommands := append(cli.DockerCommandUsage, cobraAdaptor.Usage()...)
    47  		for _, cmd := range sortCommands(dockerCommands) {
    48  			help += fmt.Sprintf("    %-10.10s%s\n", cmd.Name, cmd.Description)
    49  		}
    50  
    51  		help += "\nRun 'docker COMMAND --help' for more information on a command."
    52  		fmt.Fprintf(stdout, "%s\n", help)
    53  	}
    54  
    55  	flag.Parse()
    56  
    57  	if *flVersion {
    58  		showVersion()
    59  		return
    60  	}
    61  
    62  	if *flHelp {
    63  		// if global flag --help is present, regardless of what other options and commands there are,
    64  		// just print the usage.
    65  		flag.Usage()
    66  		return
    67  	}
    68  
    69  	clientCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags)
    70  
    71  	c := cli.New(clientCli, NewDaemonProxy(), cobraAdaptor)
    72  	if err := c.Run(flag.Args()...); err != nil {
    73  		if sterr, ok := err.(cli.StatusError); ok {
    74  			if sterr.Status != "" {
    75  				fmt.Fprintln(stderr, sterr.Status)
    76  			}
    77  			// StatusError should only be used for errors, and all errors should
    78  			// have a non-zero exit status, so never exit with 0
    79  			if sterr.StatusCode == 0 {
    80  				os.Exit(1)
    81  			}
    82  			os.Exit(sterr.StatusCode)
    83  		}
    84  		fmt.Fprintln(stderr, err)
    85  		os.Exit(1)
    86  	}
    87  }
    88  
    89  func showVersion() {
    90  	if utils.ExperimentalBuild() {
    91  		fmt.Printf("Docker version %s, build %s, experimental\n", dockerversion.Version, dockerversion.GitCommit)
    92  	} else {
    93  		fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
    94  	}
    95  }
    96  
    97  func initClientFlags(commonFlags *cliflags.CommonFlags) *cliflags.ClientFlags {
    98  	clientFlags := &cliflags.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags}
    99  	client := clientFlags.FlagSet
   100  	client.StringVar(&clientFlags.ConfigDir, []string{"-config"}, cliconfig.ConfigDir(), "Location of client config files")
   101  
   102  	clientFlags.PostParse = func() {
   103  		clientFlags.Common.PostParse()
   104  
   105  		if clientFlags.ConfigDir != "" {
   106  			cliconfig.SetConfigDir(clientFlags.ConfigDir)
   107  		}
   108  
   109  		if clientFlags.Common.TrustKey == "" {
   110  			clientFlags.Common.TrustKey = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
   111  		}
   112  
   113  		if clientFlags.Common.Debug {
   114  			utils.EnableDebug()
   115  		}
   116  	}
   117  	return clientFlags
   118  }