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

Feat context list
This commit is contained in:
Djordje Lukic 2020-05-18 08:06:30 -07:00 committed by GitHub
commit 04e6023d08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 10 deletions

View File

@ -35,6 +35,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
apicontext "github.com/docker/api/context"
"github.com/docker/api/context/store" "github.com/docker/api/context/store"
) )
@ -52,6 +53,7 @@ func listCommand() *cobra.Command {
} }
func runList(ctx context.Context) error { func runList(ctx context.Context) error {
currentContext := apicontext.CurrentContext(ctx)
s := store.ContextStore(ctx) s := store.ContextStore(ctx)
contexts, err := s.List() contexts, err := s.List()
if err != nil { if err != nil {
@ -63,7 +65,11 @@ func runList(ctx context.Context) error {
format := "%s\t%s\t%s\n" format := "%s\t%s\t%s\n"
for _, c := range contexts { 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() return w.Flush()

View File

@ -48,6 +48,8 @@ const (
) )
const ( const (
dockerEndpointKey = "docker"
configDir = ".docker"
contextsDir = "contexts" contextsDir = "contexts"
metadataDir = "meta" metadataDir = "meta"
metaFile = "meta.json" metaFile = "meta.json"
@ -103,7 +105,7 @@ func New(opts ...Opt) (Store, error) {
return nil, err return nil, err
} }
s := &store{ s := &store{
root: filepath.Join(home, ".docker"), root: filepath.Join(home, configDir),
} }
if _, err := os.Stat(s.root); os.IsNotExist(err) { if _, err := os.Stat(s.root); os.IsNotExist(err) {
if err = os.Mkdir(s.root, 0755); err != nil { 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 { func (s *store) GetType(meta *Metadata) string {
for k := range meta.Endpoints { for k := range meta.Endpoints {
if k != "docker" { if k != dockerEndpointKey {
return k return k
} }
} }
return "docker"
return dockerEndpointKey
} }
func (s *store) Create(name string, data TypedContext) error { func (s *store) Create(name string, data TypedContext) error {
@ -220,7 +223,7 @@ func (s *store) Create(name string, data TypedContext) error {
Name: name, Name: name,
Metadata: data, Metadata: data,
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"docker": dummyContext{}, (dockerEndpointKey): dummyContext{},
(data.Type): dummyContext{}, (data.Type): dummyContext{},
}, },
} }

View File

@ -61,7 +61,7 @@ func main() {
It("uses the test context", func() { It("uses the test context", func() {
currentContext := NewDockerCommand("context", "use", "test-example").ExecOrDie() currentContext := NewDockerCommand("context", "use", "test-example").ExecOrDie()
Expect(currentContext).To(ContainSubstring("test-example")) Expect(currentContext).To(ContainSubstring("test-example"))
output := NewCommand("docker", "context", "ls").ExecOrDie() output := NewDockerCommand("context", "ls").ExecOrDie()
Expect(output).To(ContainSubstring("test-example *")) Expect(output).To(ContainSubstring("test-example *"))
output = NewDockerCommand("context", "show").ExecOrDie() output = NewDockerCommand("context", "show").ExecOrDie()
Expect(output).To(ContainSubstring("test-example")) Expect(output).To(ContainSubstring("test-example"))