Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 505b6a9

Browse files
authored
Merge pull request #229 from docker/context_ls_q
added -q flag to `docker context ls`.
2 parents 8f6cd7c + 34d0101 commit 505b6a9

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

cli/cmd/context/ls.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,26 @@ import (
4141
"github.com/docker/api/context/store"
4242
)
4343

44+
type lsOpts struct {
45+
quiet bool
46+
}
47+
4448
func listCommand() *cobra.Command {
49+
var opts lsOpts
4550
cmd := &cobra.Command{
4651
Use: "list",
4752
Short: "List available contexts",
4853
Aliases: []string{"ls"},
4954
Args: cobra.NoArgs,
5055
RunE: func(cmd *cobra.Command, args []string) error {
51-
return runList(cmd.Context())
56+
return runList(cmd.Context(), opts)
5257
},
5358
}
59+
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
5460
return cmd
5561
}
5662

57-
func runList(ctx context.Context) error {
63+
func runList(ctx context.Context, opts lsOpts) error {
5864
currentContext := apicontext.CurrentContext(ctx)
5965
s := store.ContextStore(ctx)
6066
contexts, err := s.List()
@@ -66,6 +72,13 @@ func runList(ctx context.Context) error {
6672
return strings.Compare(contexts[i].Name, contexts[j].Name) == -1
6773
})
6874

75+
if opts.quiet {
76+
for _, c := range contexts {
77+
fmt.Println(c.Name)
78+
}
79+
return nil
80+
}
81+
6982
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
7083
fmt.Fprintln(w, "NAME\tTYPE\tDESCRIPTION\tDOCKER ENDPOINT\tKUBERNETES ENDPOINT\tORCHESTRATOR")
7184
format := "%s\t%s\t%s\t%s\t%s\t%s\n"

cli/cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/docker/api/cli/dockerclassic"
1010
)
1111

12-
const cliVersion = "1.0.0-beta"
12+
const cliVersion = "0.1.0"
1313

1414
// VersionCommand command to display version
1515
func VersionCommand() *cobra.Command {

local/e2e/backend_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package e2e
22

33
import (
4-
"strings"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
@@ -36,8 +35,7 @@ func (m *LocalBackendTestSuite) TestRun() {
3635
defer func() {
3736
m.NewDockerCommand("rm", "-f", "nginx").ExecOrDie()
3837
}()
39-
lines := strings.Split(out, "\n")
40-
assert.Equal(m.T(), 3, len(lines))
38+
assert.Contains(m.T(), out, "nginx")
4139
}
4240

4341
func (m *LocalBackendTestSuite) TestRunWithPorts() {

tests/e2e/e2e_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ func (s *E2eSuite) TestCreateDockerContextAndListIt() {
6666
golden.Assert(s.T(), output, GoldenFile("ls-out-test-docker"))
6767
}
6868

69+
func (s *E2eSuite) TestContextListQuiet() {
70+
s.NewDockerCommand("context", "create", "test-docker", "--from", "default").ExecOrDie()
71+
output := s.NewCommand("docker", "context", "ls", "-q").ExecOrDie()
72+
Expect(output).To(Equal(`default
73+
test-docker
74+
`))
75+
}
76+
6977
func (s *E2eSuite) TestInspectDefaultContext() {
7078
output := s.NewDockerCommand("context", "inspect", "default").ExecOrDie()
7179
Expect(output).To(ContainSubstring(`"Name": "default"`))
@@ -158,8 +166,7 @@ func (s *E2eSuite) TestDisplayFriendlyErrorMessageForLegacyCommands() {
158166

159167
func (s *E2eSuite) TestDisplaysAdditionalLineInDockerVersion() {
160168
output := s.NewDockerCommand("version").ExecOrDie()
161-
Expect(output).To(ContainSubstring(`Azure integration 1.0.0-beta
162-
Version: `))
169+
Expect(output).To(ContainSubstring("Azure integration"))
163170
}
164171

165172
func (s *E2eSuite) TestMockBackend() {

0 commit comments

Comments
 (0)