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"
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()

View File

@ -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{},
},
}

View File

@ -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"))