Procházet zdrojové kódy

Merge pull request #264 from docker/context_ls_format

Allow --format on docker context ls. Fixing VS Code extension shellout
Guillaume Tardif před 5 roky
rodič
revize
5da31da05b
2 změnil soubory, kde provedl 17 přidání a 5 odebrání
  1. 11 5
      cli/cmd/context/ls.go
  2. 6 0
      tests/e2e/e2e_test.go

+ 11 - 5
cli/cmd/context/ls.go

@@ -17,7 +17,6 @@
 package context
 
 import (
-	"context"
 	"errors"
 	"fmt"
 	"os"
@@ -27,14 +26,16 @@ import (
 
 	"github.com/spf13/cobra"
 
+	"github.com/docker/api/cli/mobycli"
 	apicontext "github.com/docker/api/context"
 	"github.com/docker/api/context/store"
 	"github.com/docker/api/formatter"
 )
 
 type lsOpts struct {
-	quiet bool
-	json  bool
+	quiet  bool
+	json   bool
+	format string
 }
 
 func (o lsOpts) validate() error {
@@ -52,21 +53,26 @@ func listCommand() *cobra.Command {
 		Aliases: []string{"ls"},
 		Args:    cobra.NoArgs,
 		RunE: func(cmd *cobra.Command, args []string) error {
-			return runList(cmd.Context(), opts)
+			return runList(cmd, opts)
 		},
 	}
 	cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
 	cmd.Flags().BoolVar(&opts.json, "json", false, "Format output as JSON")
+	cmd.Flags().StringVar(&opts.format, "format", "", "Format output as JSON")
 
 	return cmd
 }
 
-func runList(ctx context.Context, opts lsOpts) error {
+func runList(cmd *cobra.Command, opts lsOpts) error {
 	err := opts.validate()
 	if err != nil {
 		return err
 	}
+	if opts.format != "" {
+		return mobycli.ExecCmd(cmd)
+	}
 
+	ctx := cmd.Context()
 	currentContext := apicontext.CurrentContext(ctx)
 	s := store.ContextStore(ctx)
 	contexts, err := s.List()

+ 6 - 0
tests/e2e/e2e_test.go

@@ -80,6 +80,12 @@ func (s *E2eSuite) TestInspectContextRegardlessCurrentContext() {
 	Expect(output).To(ContainSubstring(`"Name": "localCtx"`))
 }
 
+func (s *E2eSuite) TestContextLsFormat() {
+	output, err := s.NewDockerCommand("context", "ls", "--format", "{{ json . }}").Exec()
+	Expect(err).To(BeNil())
+	Expect(output).To(ContainSubstring(`"Name":"default"`))
+}
+
 func (s *E2eSuite) TestContextCreateParseErrorDoesNotDelegateToLegacy() {
 	It("should dispay new cli error when parsing context create flags", func() {
 		_, err := s.NewDockerCommand("context", "create", "aci", "--subscription-id", "titi").Exec()