Merge pull request #264 from docker/context_ls_format

Allow --format on docker context ls. Fixing VS Code extension shellout
This commit is contained in:
Guillaume Tardif 2020-06-23 14:47:15 +02:00 committed by GitHub
commit 5da31da05b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -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()

View File

@ -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()