diff --git a/cli/cmd/context/ls.go b/cli/cmd/context/ls.go index 2a5de0150..20c82ba73 100644 --- a/cli/cmd/context/ls.go +++ b/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() diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 5552d4ecb..d8442c505 100644 --- a/tests/e2e/e2e_test.go +++ b/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()