Merge pull request #247 from docker/main_command_cleanup

Main command cleanup
This commit is contained in:
Guillaume Tardif 2020-06-23 10:35:07 +02:00 committed by GitHub
commit 03f11e2953
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 2 deletions

View File

@ -21,6 +21,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/docker/api/cli/cmd/mobyflags"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -43,6 +45,7 @@ func Command() *cobra.Command {
flags.StringP("username", "u", "", "Username") flags.StringP("username", "u", "", "Username")
flags.StringP("password", "p", "", "Password") flags.StringP("password", "p", "", "Password")
flags.BoolP("password-stdin", "", false, "Take the password from stdin") flags.BoolP("password-stdin", "", false, "Take the password from stdin")
mobyflags.AddMobyFlagsForRetrocompatibility(flags)
return cmd return cmd
} }

View File

@ -0,0 +1,24 @@
package mobyflags
import (
"log"
flag "github.com/spf13/pflag"
)
// AddMobyFlagsForRetrocompatibility adds retrocompatibility flags to our commands
func AddMobyFlagsForRetrocompatibility(flags *flag.FlagSet) {
const hostFlag = "host"
flags.StringP(hostFlag, "H", "", "Daemon socket(s) to connect to")
markHidden(flags, hostFlag)
const logLevelFlag = "log-level"
flags.StringP(logLevelFlag, "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
markHidden(flags, logLevelFlag)
}
func markHidden(flags *flag.FlagSet, flagName string) {
err := flags.MarkHidden(flagName)
if err != nil {
log.Fatal(err)
}
}

View File

@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/docker/api/cli/cmd/mobyflags"
"github.com/docker/api/cli/mobycli" "github.com/docker/api/cli/mobycli"
) )
@ -39,6 +40,7 @@ func VersionCommand() *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
flags.String("format", "", "Format the output using the given Go template") flags.String("format", "", "Format the output using the given Go template")
flags.String("kubeconfig", "", "Kubernetes config file") flags.String("kubeconfig", "", "Kubernetes config file")
mobyflags.AddMobyFlagsForRetrocompatibility(flags)
return cmd return cmd
} }

View File

@ -85,7 +85,6 @@ func main() {
var opts cliopts.GlobalOpts var opts cliopts.GlobalOpts
root := &cobra.Command{ root := &cobra.Command{
Use: "docker", Use: "docker",
Long: "docker for the 2020s",
SilenceErrors: true, SilenceErrors: true,
SilenceUsage: true, SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
@ -121,7 +120,7 @@ func main() {
helpFunc(cmd, args) helpFunc(cmd, args)
}) })
root.PersistentFlags().BoolVarP(&opts.Debug, "debug", "d", false, "enable debug output in the logs") root.PersistentFlags().BoolVarP(&opts.Debug, "debug", "D", false, "enable debug output in the logs")
opts.AddConfigFlags(root.PersistentFlags()) opts.AddConfigFlags(root.PersistentFlags())
opts.AddContextFlags(root.PersistentFlags()) opts.AddContextFlags(root.PersistentFlags())

View File

@ -179,6 +179,23 @@ func (s *E2eSuite) TestLeaveLegacyErrorMessagesUnchanged() {
Expect(err).NotTo(BeNil()) Expect(err).NotTo(BeNil())
} }
func (s *E2eSuite) TestPassThroughRootLegacyFlags() {
output, err := s.NewDockerCommand("-H", "tcp://localhost:123", "version").Exec()
Expect(err).To(BeNil())
Expect(output).To(ContainSubstring("Client:"))
Expect(output).To(ContainSubstring("localhost:123"))
output, _ = s.NewDockerCommand("-H", "tcp://localhost:123", "login", "-u", "nouser", "-p", "wrongpasword").Exec()
Expect(output).To(ContainSubstring("WARNING! Using --password via the CLI is insecure"))
output, _ = s.NewDockerCommand("--log-level", "debug", "login", "-u", "nouser", "-p", "wrongpasword").Exec()
Expect(output).To(ContainSubstring("WARNING! Using --password via the CLI is insecure"))
output, _ = s.NewDockerCommand("login", "--help").Exec()
Expect(output).NotTo(ContainSubstring("--host"))
Expect(output).NotTo(ContainSubstring("--log-level"))
}
func (s *E2eSuite) TestDisplayFriendlyErrorMessageForLegacyCommands() { func (s *E2eSuite) TestDisplayFriendlyErrorMessageForLegacyCommands() {
s.NewDockerCommand("context", "create", "example", "test-example").ExecOrDie() s.NewDockerCommand("context", "create", "example", "test-example").ExecOrDie()
output, err := s.NewDockerCommand("--context", "test-example", "images").Exec() output, err := s.NewDockerCommand("--context", "test-example", "images").Exec()