mirror of https://github.com/docker/cli.git
Convert docker root command to use pflag and cobra
Fix the daemon proxy for cobra commands. Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
23dd85befd
commit
08784d7e0e
18
daemon.go
18
daemon.go
|
@ -1,18 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
const daemonBinary = "dockerd"
|
|
||||||
|
|
||||||
// DaemonProxy acts as a cli.Handler to proxy calls to the daemon binary
|
|
||||||
type DaemonProxy struct{}
|
|
||||||
|
|
||||||
// NewDaemonProxy returns a new handler
|
|
||||||
func NewDaemonProxy() DaemonProxy {
|
|
||||||
return DaemonProxy{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns a cli command handler if one exists
|
|
||||||
func (p DaemonProxy) Command(name string) func(...string) error {
|
|
||||||
return map[string]func(...string) error{
|
|
||||||
"daemon": p.CmdDaemon,
|
|
||||||
}[name]
|
|
||||||
}
|
|
|
@ -4,12 +4,22 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdDaemon reports on an error on windows, because there is no exec
|
func newDaemonCommand() *cobra.Command {
|
||||||
func (p DaemonProxy) CmdDaemon(args ...string) error {
|
return &cobra.Command{
|
||||||
|
Use: "daemon",
|
||||||
|
Hidden: true,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return runDaemon()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runDaemon() error {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"`docker daemon` is not supported on %s. Please run `dockerd` directly",
|
"`docker daemon` is not supported on %s. Please run `dockerd` directly",
|
||||||
strings.Title(runtime.GOOS))
|
strings.Title(runtime.GOOS))
|
||||||
|
|
|
@ -3,23 +3,37 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdDaemon execs dockerd with the same flags
|
const daemonBinary = "dockerd"
|
||||||
func (p DaemonProxy) CmdDaemon(args ...string) error {
|
|
||||||
// Special case for handling `docker help daemon`. When pkg/mflag is removed
|
|
||||||
// we can support this on the daemon side, but that is not possible with
|
|
||||||
// pkg/mflag because it uses os.Exit(1) instead of returning an error on
|
|
||||||
// unexpected args.
|
|
||||||
if len(args) == 0 || args[0] != "--help" {
|
|
||||||
// Use os.Args[1:] so that "global" args are passed to dockerd
|
|
||||||
args = stripDaemonArg(os.Args[1:])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func newDaemonCommand() *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "daemon",
|
||||||
|
Hidden: true,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return runDaemon()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.SetHelpFunc(helpFunc)
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// CmdDaemon execs dockerd with the same flags
|
||||||
|
func runDaemon() error {
|
||||||
|
// Use os.Args[1:] so that "global" args are passed to dockerd
|
||||||
|
return execDaemon(stripDaemonArg(os.Args[1:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func execDaemon(args []string) error {
|
||||||
binaryPath, err := findDaemonBinary()
|
binaryPath, err := findDaemonBinary()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -31,6 +45,12 @@ func (p DaemonProxy) CmdDaemon(args ...string) error {
|
||||||
os.Environ())
|
os.Environ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func helpFunc(cmd *cobra.Command, args []string) {
|
||||||
|
if err := execDaemon([]string{"--help"}); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// findDaemonBinary looks for the path to the dockerd binary starting with
|
// findDaemonBinary looks for the path to the dockerd binary starting with
|
||||||
// the directory of the current executable (if one exists) and followed by $PATH
|
// the directory of the current executable (if one exists) and followed by $PATH
|
||||||
func findDaemonBinary() (string, error) {
|
func findDaemonBinary() (string, error) {
|
||||||
|
|
118
docker.go
118
docker.go
|
@ -12,64 +12,52 @@ import (
|
||||||
cliflags "github.com/docker/docker/cli/flags"
|
cliflags "github.com/docker/docker/cli/flags"
|
||||||
"github.com/docker/docker/cliconfig"
|
"github.com/docker/docker/cliconfig"
|
||||||
"github.com/docker/docker/dockerversion"
|
"github.com/docker/docker/dockerversion"
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
|
||||||
"github.com/docker/docker/pkg/term"
|
"github.com/docker/docker/pkg/term"
|
||||||
"github.com/docker/docker/utils"
|
"github.com/docker/docker/utils"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func newDockerCommand(dockerCli *client.DockerCli, opts *cliflags.ClientOptions) *cobra.Command {
|
||||||
commonFlags = cliflags.InitCommonFlags()
|
cmd := &cobra.Command{
|
||||||
clientFlags = initClientFlags(commonFlags)
|
Use: "docker [OPTIONS] COMMAND [arg...]",
|
||||||
flHelp = flag.Bool([]string{"h", "-help"}, false, "Print usage")
|
Short: "A self-sufficient runtime for containers.",
|
||||||
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
|
SilenceUsage: true,
|
||||||
)
|
SilenceErrors: true,
|
||||||
|
Args: cli.NoArgs,
|
||||||
|
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 {
|
||||||
|
dockerPreRun(cmd.Flags(), opts)
|
||||||
|
return dockerCli.Initialize(opts)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cobraadaptor.SetupRootCommand(cmd, dockerCli)
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
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)
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Set terminal emulation based on platform as required.
|
// Set terminal emulation based on platform as required.
|
||||||
stdin, stdout, stderr := term.StdStreams()
|
stdin, stdout, stderr := term.StdStreams()
|
||||||
|
|
||||||
logrus.SetOutput(stderr)
|
logrus.SetOutput(stderr)
|
||||||
|
|
||||||
flag.Merge(flag.CommandLine, clientFlags.FlagSet, commonFlags.FlagSet)
|
opts := cliflags.NewClientOptions()
|
||||||
|
dockerCli := client.NewDockerCli(stdin, stdout, stderr, opts)
|
||||||
|
cmd := newDockerCommand(dockerCli, opts)
|
||||||
|
|
||||||
cobraAdaptor := cobraadaptor.NewCobraAdaptor(clientFlags)
|
if err := cmd.Execute(); err != nil {
|
||||||
|
|
||||||
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"
|
|
||||||
|
|
||||||
dockerCommands := append(cli.DockerCommandUsage, cobraAdaptor.Usage()...)
|
|
||||||
for _, cmd := range sortCommands(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(), cobraAdaptor)
|
|
||||||
if err := c.Run(flag.Args()...); err != nil {
|
|
||||||
if sterr, ok := err.(cli.StatusError); ok {
|
if sterr, ok := err.(cli.StatusError); ok {
|
||||||
if sterr.Status != "" {
|
if sterr.Status != "" {
|
||||||
fmt.Fprintln(stderr, sterr.Status)
|
fmt.Fprintln(stderr, sterr.Status)
|
||||||
|
@ -94,26 +82,22 @@ func showVersion() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initClientFlags(commonFlags *cliflags.CommonFlags) *cliflags.ClientFlags {
|
func dockerPreRun(flags *pflag.FlagSet, opts *cliflags.ClientOptions) {
|
||||||
clientFlags := &cliflags.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags}
|
opts.Common.SetDefaultOptions(flags)
|
||||||
client := clientFlags.FlagSet
|
cliflags.SetDaemonLogLevel(opts.Common.LogLevel)
|
||||||
client.StringVar(&clientFlags.ConfigDir, []string{"-config"}, cliconfig.ConfigDir(), "Location of client config files")
|
|
||||||
|
|
||||||
clientFlags.PostParse = func() {
|
// TODO: remove this, set a default in New, and pass it in opts
|
||||||
clientFlags.Common.PostParse()
|
if opts.ConfigDir != "" {
|
||||||
cliflags.SetDaemonLogLevel(commonOpts.LogLevel)
|
cliconfig.SetConfigDir(opts.ConfigDir)
|
||||||
|
}
|
||||||
if clientFlags.ConfigDir != "" {
|
|
||||||
cliconfig.SetConfigDir(clientFlags.ConfigDir)
|
if opts.Common.TrustKey == "" {
|
||||||
}
|
opts.Common.TrustKey = filepath.Join(
|
||||||
|
cliconfig.ConfigDir(),
|
||||||
if clientFlags.Common.TrustKey == "" {
|
cliflags.DefaultTrustKeyFile)
|
||||||
clientFlags.Common.TrustKey = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
|
}
|
||||||
}
|
|
||||||
|
if opts.Common.Debug {
|
||||||
if clientFlags.Common.Debug {
|
utils.EnableDebug()
|
||||||
utils.EnableDebug()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return clientFlags
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,21 @@ import (
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/docker/utils"
|
"github.com/docker/docker/utils"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/client"
|
||||||
|
cliflags "github.com/docker/docker/cli/flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestClientDebugEnabled(t *testing.T) {
|
func TestClientDebugEnabled(t *testing.T) {
|
||||||
defer utils.DisableDebug()
|
defer utils.DisableDebug()
|
||||||
|
|
||||||
clientFlags.Common.FlagSet.Parse([]string{"-D"})
|
opts := cliflags.NewClientOptions()
|
||||||
clientFlags.PostParse()
|
cmd := newDockerCommand(&client.DockerCli{}, opts)
|
||||||
|
|
||||||
|
opts.Common.Debug = true
|
||||||
|
if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil {
|
||||||
|
t.Fatalf("Unexpected error: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
if os.Getenv("DEBUG") != "1" {
|
if os.Getenv("DEBUG") != "1" {
|
||||||
t.Fatal("expected debug enabled, got false")
|
t.Fatal("expected debug enabled, got false")
|
||||||
|
|
Loading…
Reference in New Issue