Merge pull request #229 from docker/context_ls_q

added -q flag to `docker context ls`.
This commit is contained in:
Guillaume Tardif 2020-06-17 09:05:35 +02:00 committed by GitHub
commit 505b6a90c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 8 deletions

View File

@ -41,20 +41,26 @@ import (
"github.com/docker/api/context/store" "github.com/docker/api/context/store"
) )
type lsOpts struct {
quiet bool
}
func listCommand() *cobra.Command { func listCommand() *cobra.Command {
var opts lsOpts
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "list", Use: "list",
Short: "List available contexts", Short: "List available contexts",
Aliases: []string{"ls"}, Aliases: []string{"ls"},
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runList(cmd.Context()) return runList(cmd.Context(), opts)
}, },
} }
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
return cmd return cmd
} }
func runList(ctx context.Context) error { func runList(ctx context.Context, opts lsOpts) error {
currentContext := apicontext.CurrentContext(ctx) currentContext := apicontext.CurrentContext(ctx)
s := store.ContextStore(ctx) s := store.ContextStore(ctx)
contexts, err := s.List() contexts, err := s.List()
@ -66,6 +72,13 @@ func runList(ctx context.Context) error {
return strings.Compare(contexts[i].Name, contexts[j].Name) == -1 return strings.Compare(contexts[i].Name, contexts[j].Name) == -1
}) })
if opts.quiet {
for _, c := range contexts {
fmt.Println(c.Name)
}
return nil
}
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
fmt.Fprintln(w, "NAME\tTYPE\tDESCRIPTION\tDOCKER ENDPOINT\tKUBERNETES ENDPOINT\tORCHESTRATOR") fmt.Fprintln(w, "NAME\tTYPE\tDESCRIPTION\tDOCKER ENDPOINT\tKUBERNETES ENDPOINT\tORCHESTRATOR")
format := "%s\t%s\t%s\t%s\t%s\t%s\n" format := "%s\t%s\t%s\t%s\t%s\t%s\n"

View File

@ -9,7 +9,7 @@ import (
"github.com/docker/api/cli/dockerclassic" "github.com/docker/api/cli/dockerclassic"
) )
const cliVersion = "1.0.0-beta" const cliVersion = "0.1.0"
// VersionCommand command to display version // VersionCommand command to display version
func VersionCommand() *cobra.Command { func VersionCommand() *cobra.Command {

View File

@ -1,7 +1,6 @@
package e2e package e2e
import ( import (
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -36,8 +35,7 @@ func (m *LocalBackendTestSuite) TestRun() {
defer func() { defer func() {
m.NewDockerCommand("rm", "-f", "nginx").ExecOrDie() m.NewDockerCommand("rm", "-f", "nginx").ExecOrDie()
}() }()
lines := strings.Split(out, "\n") assert.Contains(m.T(), out, "nginx")
assert.Equal(m.T(), 3, len(lines))
} }
func (m *LocalBackendTestSuite) TestRunWithPorts() { func (m *LocalBackendTestSuite) TestRunWithPorts() {

View File

@ -66,6 +66,14 @@ func (s *E2eSuite) TestCreateDockerContextAndListIt() {
golden.Assert(s.T(), output, GoldenFile("ls-out-test-docker")) golden.Assert(s.T(), output, GoldenFile("ls-out-test-docker"))
} }
func (s *E2eSuite) TestContextListQuiet() {
s.NewDockerCommand("context", "create", "test-docker", "--from", "default").ExecOrDie()
output := s.NewCommand("docker", "context", "ls", "-q").ExecOrDie()
Expect(output).To(Equal(`default
test-docker
`))
}
func (s *E2eSuite) TestInspectDefaultContext() { func (s *E2eSuite) TestInspectDefaultContext() {
output := s.NewDockerCommand("context", "inspect", "default").ExecOrDie() output := s.NewDockerCommand("context", "inspect", "default").ExecOrDie()
Expect(output).To(ContainSubstring(`"Name": "default"`)) Expect(output).To(ContainSubstring(`"Name": "default"`))
@ -158,8 +166,7 @@ func (s *E2eSuite) TestDisplayFriendlyErrorMessageForLegacyCommands() {
func (s *E2eSuite) TestDisplaysAdditionalLineInDockerVersion() { func (s *E2eSuite) TestDisplaysAdditionalLineInDockerVersion() {
output := s.NewDockerCommand("version").ExecOrDie() output := s.NewDockerCommand("version").ExecOrDie()
Expect(output).To(ContainSubstring(`Azure integration 1.0.0-beta Expect(output).To(ContainSubstring("Azure integration"))
Version: `))
} }
func (s *E2eSuite) TestMockBackend() { func (s *E2eSuite) TestMockBackend() {