瀏覽代碼

Merge pull request #114 from docker/feat-context-list

Feat context list
Djordje Lukic 5 年之前
父節點
當前提交
04e6023d08
共有 3 個文件被更改,包括 19 次插入10 次删除
  1. 7 1
      cli/cmd/context/ls.go
  2. 11 8
      context/store/store.go
  3. 1 1
      tests/e2e/e2e.go

+ 7 - 1
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()

+ 11 - 8
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{},
 		},
 	}
 

+ 1 - 1
tests/e2e/e2e.go

@@ -61,7 +61,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"))