Browse Source

Shell out to Moby cli if user uses -H, regardless of context set. Moby cli will refuse both options (context & host) to be set at the same time

Guillaume Tardif 5 years ago
parent
commit
38d4f8d25a
5 changed files with 22 additions and 9 deletions
  1. 0 3
      cli/cmd/mobyflags/mobyflags.go
  2. 5 0
      cli/main.go
  3. 4 3
      cli/mobycli/exec.go
  4. 1 0
      cli/options/options.go
  5. 12 3
      tests/e2e/e2e_test.go

+ 0 - 3
cli/cmd/mobyflags/mobyflags.go

@@ -8,9 +8,6 @@ import (
 
 
 // AddMobyFlagsForRetrocompatibility adds retrocompatibility flags to our commands
 // AddMobyFlagsForRetrocompatibility adds retrocompatibility flags to our commands
 func AddMobyFlagsForRetrocompatibility(flags *flag.FlagSet) {
 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"
 	const logLevelFlag = "log-level"
 	flags.StringP(logLevelFlag, "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
 	flags.StringP(logLevelFlag, "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
 	markHidden(flags, logLevelFlag)
 	markHidden(flags, logLevelFlag)

+ 5 - 0
cli/main.go

@@ -121,6 +121,7 @@ func main() {
 	})
 	})
 
 
 	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")
+	root.PersistentFlags().StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to")
 	opts.AddConfigFlags(root.PersistentFlags())
 	opts.AddConfigFlags(root.PersistentFlags())
 	opts.AddContextFlags(root.PersistentFlags())
 	opts.AddContextFlags(root.PersistentFlags())
 
 
@@ -133,6 +134,10 @@ func main() {
 	ctx, cancel := newSigContext()
 	ctx, cancel := newSigContext()
 	defer cancel()
 	defer cancel()
 
 
+	if opts.Host != "" {
+		mobycli.ExecRegardlessContext(ctx)
+	}
+
 	if opts.Config == "" {
 	if opts.Config == "" {
 		fatal(errors.New("config path cannot be empty"))
 		fatal(errors.New("config path cannot be empty"))
 	}
 	}

+ 4 - 3
cli/mobycli/exec.go

@@ -42,11 +42,12 @@ func ExecIfDefaultCtxType(ctx context.Context) {
 	// Only run original docker command if the current context is not
 	// Only run original docker command if the current context is not
 	// ours.
 	// ours.
 	if err != nil || currentCtx.Type() == store.DefaultContextType {
 	if err != nil || currentCtx.Type() == store.DefaultContextType {
-		shellOut(ctx)
+		ExecRegardlessContext(ctx)
 	}
 	}
 }
 }
 
 
-func shellOut(ctx context.Context) {
+// ExecRegardlessContext delegates to com.docker.cli if on moby context
+func ExecRegardlessContext(ctx context.Context) {
 	cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
 	cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
 	cmd.Stdin = os.Stdin
 	cmd.Stdin = os.Stdin
 	cmd.Stdout = os.Stdout
 	cmd.Stdout = os.Stdout
@@ -63,7 +64,7 @@ func shellOut(ctx context.Context) {
 
 
 // ExecCmd delegates the cli command to com.docker.cli. The error is never returned (process will exit with docker classic exit code), the return type is to make it easier to use with cobra commands
 // ExecCmd delegates the cli command to com.docker.cli. The error is never returned (process will exit with docker classic exit code), the return type is to make it easier to use with cobra commands
 func ExecCmd(command *cobra.Command) error {
 func ExecCmd(command *cobra.Command) error {
-	shellOut(command.Context())
+	ExecRegardlessContext(command.Context())
 	return nil
 	return nil
 }
 }
 
 

+ 1 - 0
cli/options/options.go

@@ -26,4 +26,5 @@ type GlobalOpts struct {
 	apicontext.ContextFlags
 	apicontext.ContextFlags
 	cliconfig.ConfigFlags
 	cliconfig.ConfigFlags
 	Debug bool
 	Debug bool
+	Host  string
 }
 }

+ 12 - 3
tests/e2e/e2e_test.go

@@ -181,18 +181,19 @@ func (s *E2eSuite) TestLeaveLegacyErrorMessagesUnchanged() {
 
 
 func (s *E2eSuite) TestPassThroughRootLegacyFlags() {
 func (s *E2eSuite) TestPassThroughRootLegacyFlags() {
 	output, err := s.NewDockerCommand("-H", "tcp://localhost:123", "version").Exec()
 	output, err := s.NewDockerCommand("-H", "tcp://localhost:123", "version").Exec()
-	Expect(err).To(BeNil())
-	Expect(output).To(ContainSubstring("Client:"))
+	Expect(err).NotTo(BeNil())
+	Expect(output).NotTo(ContainSubstring("unknown shorthand flag"))
 	Expect(output).To(ContainSubstring("localhost:123"))
 	Expect(output).To(ContainSubstring("localhost:123"))
 
 
 	output, _ = s.NewDockerCommand("-H", "tcp://localhost:123", "login", "-u", "nouser", "-p", "wrongpasword").Exec()
 	output, _ = s.NewDockerCommand("-H", "tcp://localhost:123", "login", "-u", "nouser", "-p", "wrongpasword").Exec()
+	Expect(output).NotTo(ContainSubstring("unknown shorthand flag"))
 	Expect(output).To(ContainSubstring("WARNING! Using --password via the CLI is insecure"))
 	Expect(output).To(ContainSubstring("WARNING! Using --password via the CLI is insecure"))
 
 
 	output, _ = s.NewDockerCommand("--log-level", "debug", "login", "-u", "nouser", "-p", "wrongpasword").Exec()
 	output, _ = s.NewDockerCommand("--log-level", "debug", "login", "-u", "nouser", "-p", "wrongpasword").Exec()
+	Expect(output).NotTo(ContainSubstring("unknown shorthand flag"))
 	Expect(output).To(ContainSubstring("WARNING! Using --password via the CLI is insecure"))
 	Expect(output).To(ContainSubstring("WARNING! Using --password via the CLI is insecure"))
 
 
 	output, _ = s.NewDockerCommand("login", "--help").Exec()
 	output, _ = s.NewDockerCommand("login", "--help").Exec()
-	Expect(output).NotTo(ContainSubstring("--host"))
 	Expect(output).NotTo(ContainSubstring("--log-level"))
 	Expect(output).NotTo(ContainSubstring("--log-level"))
 }
 }
 
 
@@ -203,6 +204,14 @@ func (s *E2eSuite) TestDisplayFriendlyErrorMessageForLegacyCommands() {
 	Expect(err).NotTo(BeNil())
 	Expect(err).NotTo(BeNil())
 }
 }
 
 
+func (s *E2eSuite) TestExecMobyIfUsingHostFlag() {
+	s.NewDockerCommand("context", "create", "example", "test-example").ExecOrDie()
+	s.NewDockerCommand("context", "use", "test-example").ExecOrDie()
+	output, err := s.NewDockerCommand("-H", "unix:///var/run/docker.sock", "ps").Exec()
+	Expect(err).To(BeNil())
+	Expect(output).To(ContainSubstring("CONTAINER ID"))
+}
+
 func (s *E2eSuite) TestDisplaysAdditionalLineInDockerVersion() {
 func (s *E2eSuite) TestDisplaysAdditionalLineInDockerVersion() {
 	output := s.NewDockerCommand("version").ExecOrDie()
 	output := s.NewDockerCommand("version").ExecOrDie()
 	Expect(output).To(ContainSubstring("Azure integration"))
 	Expect(output).To(ContainSubstring("Azure integration"))