From 0bd18986ddb4f685580a5a96879a8572a4c3fb27 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Mon, 18 May 2020 15:16:05 +0200 Subject: [PATCH 1/2] Add "*" for the current context --- cli/cmd/context/ls.go | 8 +++++++- tests/e2e/e2e.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cli/cmd/context/ls.go b/cli/cmd/context/ls.go index 192a3e432..569fce52c 100644 --- a/cli/cmd/context/ls.go +++ b/cli/cmd/context/ls.go @@ -35,6 +35,7 @@ import ( "github.com/spf13/cobra" + apicontext "github.com/docker/api/context" "github.com/docker/api/context/store" ) @@ -52,6 +53,7 @@ func listCommand() *cobra.Command { } func runList(ctx context.Context) error { + currentContext := apicontext.CurrentContext(ctx) s := store.ContextStore(ctx) contexts, err := s.List() if err != nil { @@ -63,7 +65,11 @@ func runList(ctx context.Context) error { format := "%s\t%s\t%s\n" for _, c := range contexts { - fmt.Fprintf(w, format, c.Name, c.Metadata.Description, c.Metadata.Type) + contextName := c.Name + if c.Name == currentContext { + contextName += " *" + } + fmt.Fprintf(w, format, contextName, c.Metadata.Description, c.Metadata.Type) } return w.Flush() diff --git a/tests/e2e/e2e.go b/tests/e2e/e2e.go index 1b577dd51..a5e7f8d4d 100644 --- a/tests/e2e/e2e.go +++ b/tests/e2e/e2e.go @@ -59,7 +59,7 @@ func main() { It("uses the test context", func() { currentContext := NewDockerCommand("context", "use", "test-example").ExecOrDie() Expect(currentContext).To(ContainSubstring("test-example")) - output := NewCommand("docker", "context", "ls").ExecOrDie() + output := NewDockerCommand("context", "ls").ExecOrDie() Expect(output).To(ContainSubstring("test-example *")) output = NewDockerCommand("context", "show").ExecOrDie() Expect(output).To(ContainSubstring("test-example")) From 3891c8c414751ad8fe3caaf82c2639e8c11f9b7b Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Mon, 18 May 2020 15:32:35 +0200 Subject: [PATCH 2/2] Put all magic strings in variables in context store --- context/store/store.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/context/store/store.go b/context/store/store.go index 80297e0be..2675ca509 100644 --- a/context/store/store.go +++ b/context/store/store.go @@ -48,9 +48,11 @@ const ( ) const ( - contextsDir = "contexts" - metadataDir = "meta" - metaFile = "meta.json" + dockerEndpointKey = "docker" + configDir = ".docker" + contextsDir = "contexts" + metadataDir = "meta" + metaFile = "meta.json" ) type contextStoreKey struct{} @@ -103,7 +105,7 @@ func New(opts ...Opt) (Store, error) { return nil, err } s := &store{ - root: filepath.Join(home, ".docker"), + root: filepath.Join(home, configDir), } if _, err := os.Stat(s.root); os.IsNotExist(err) { if err = os.Mkdir(s.root, 0755); err != nil { @@ -190,11 +192,12 @@ func parse(payload []byte, getter func() interface{}) (interface{}, error) { func (s *store) GetType(meta *Metadata) string { for k := range meta.Endpoints { - if k != "docker" { + if k != dockerEndpointKey { return k } } - return "docker" + + return dockerEndpointKey } func (s *store) Create(name string, data TypedContext) error { @@ -220,8 +223,8 @@ func (s *store) Create(name string, data TypedContext) error { Name: name, Metadata: data, Endpoints: map[string]interface{}{ - "docker": dummyContext{}, - (data.Type): dummyContext{}, + (dockerEndpointKey): dummyContext{}, + (data.Type): dummyContext{}, }, }