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

Commit 04e6023

Browse files
authored
Merge pull request #114 from docker/feat-context-list
Feat context list
2 parents 3e2580c + 3891c8c commit 04e6023

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

cli/cmd/context/ls.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"github.com/spf13/cobra"
3737

38+
apicontext "github.com/docker/api/context"
3839
"github.com/docker/api/context/store"
3940
)
4041

@@ -52,6 +53,7 @@ func listCommand() *cobra.Command {
5253
}
5354

5455
func runList(ctx context.Context) error {
56+
currentContext := apicontext.CurrentContext(ctx)
5557
s := store.ContextStore(ctx)
5658
contexts, err := s.List()
5759
if err != nil {
@@ -63,7 +65,11 @@ func runList(ctx context.Context) error {
6365
format := "%s\t%s\t%s\n"
6466

6567
for _, c := range contexts {
66-
fmt.Fprintf(w, format, c.Name, c.Metadata.Description, c.Metadata.Type)
68+
contextName := c.Name
69+
if c.Name == currentContext {
70+
contextName += " *"
71+
}
72+
fmt.Fprintf(w, format, contextName, c.Metadata.Description, c.Metadata.Type)
6773
}
6874

6975
return w.Flush()

context/store/store.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ const (
4848
)
4949

5050
const (
51-
contextsDir = "contexts"
52-
metadataDir = "meta"
53-
metaFile = "meta.json"
51+
dockerEndpointKey = "docker"
52+
configDir = ".docker"
53+
contextsDir = "contexts"
54+
metadataDir = "meta"
55+
metaFile = "meta.json"
5456
)
5557

5658
type contextStoreKey struct{}
@@ -103,7 +105,7 @@ func New(opts ...Opt) (Store, error) {
103105
return nil, err
104106
}
105107
s := &store{
106-
root: filepath.Join(home, ".docker"),
108+
root: filepath.Join(home, configDir),
107109
}
108110
if _, err := os.Stat(s.root); os.IsNotExist(err) {
109111
if err = os.Mkdir(s.root, 0755); err != nil {
@@ -190,11 +192,12 @@ func parse(payload []byte, getter func() interface{}) (interface{}, error) {
190192

191193
func (s *store) GetType(meta *Metadata) string {
192194
for k := range meta.Endpoints {
193-
if k != "docker" {
195+
if k != dockerEndpointKey {
194196
return k
195197
}
196198
}
197-
return "docker"
199+
200+
return dockerEndpointKey
198201
}
199202

200203
func (s *store) Create(name string, data TypedContext) error {
@@ -220,8 +223,8 @@ func (s *store) Create(name string, data TypedContext) error {
220223
Name: name,
221224
Metadata: data,
222225
Endpoints: map[string]interface{}{
223-
"docker": dummyContext{},
224-
(data.Type): dummyContext{},
226+
(dockerEndpointKey): dummyContext{},
227+
(data.Type): dummyContext{},
225228
},
226229
}
227230

tests/e2e/e2e.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
It("uses the test context", func() {
6262
currentContext := NewDockerCommand("context", "use", "test-example").ExecOrDie()
6363
Expect(currentContext).To(ContainSubstring("test-example"))
64-
output := NewCommand("docker", "context", "ls").ExecOrDie()
64+
output := NewDockerCommand("context", "ls").ExecOrDie()
6565
Expect(output).To(ContainSubstring("test-example *"))
6666
output = NewDockerCommand("context", "show").ExecOrDie()
6767
Expect(output).To(ContainSubstring("test-example"))

0 commit comments

Comments
 (0)