Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ func GetDockerContext(storeMetadata store.Metadata) (DockerContext, error) {
if !ok {
return DockerContext{}, errors.New("context metadata is not a valid DockerContext")
}
if storeMetadata.Name == DefaultContextName {
res.Description = "Current DOCKER_HOST based configuration"
}
return res, nil
}
14 changes: 7 additions & 7 deletions cli/command/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
if opts.format == "" {
opts.format = formatter.TableFormatKey
}
curContext := dockerCli.CurrentContext()
contextMap, err := dockerCli.ContextStore().List()
if err != nil {
return err
}
var contexts []*formatter.ClientContext
var (
curContext = dockerCli.CurrentContext()
contexts []*formatter.ClientContext
)
for _, rawMeta := range contextMap {
isCurrent := rawMeta.Name == curContext
meta, err := command.GetDockerContext(rawMeta)
if err != nil {
return err
Expand All @@ -59,12 +62,9 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
if err != nil {
return err
}
if rawMeta.Name == command.DefaultContextName {
meta.Description = "Current DOCKER_HOST based configuration"
}
desc := formatter.ClientContext{
Name: rawMeta.Name,
Current: rawMeta.Name == curContext,
Current: isCurrent,
Description: meta.Description,
DockerEndpoint: dockerEndpoint.Host,
}
Expand All @@ -77,7 +77,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
return err
}
if os.Getenv(client.EnvOverrideHost) != "" {
fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+
_, _ = fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+
"To use a context, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion cli/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func TestDockerContextMetadataKeepAdditionalFields(t *testing.T) {
}
jsonBytes, err := json.Marshal(c)
assert.NilError(t, err)
assert.Equal(t, `{"Description":"test","foo":"bar"}`, string(jsonBytes))
const expected = `{"Description":"test","foo":"bar"}`
assert.Equal(t, string(jsonBytes), expected)

var c2 DockerContext
assert.NilError(t, json.Unmarshal(jsonBytes, &c2))
Expand Down
4 changes: 2 additions & 2 deletions cli/context/store/metadatastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *metadataStore) remove(name string) error {
func (s *metadataStore) list() ([]Metadata, error) {
ctxDirs, err := listRecursivelyMetadataDirs(s.root)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, err
Expand All @@ -110,7 +110,7 @@ func (s *metadataStore) list() ([]Metadata, error) {
for _, dir := range ctxDirs {
c, err := s.getByID(contextdir(dir))
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
continue
}
return nil, errors.Wrap(err, "failed to read metadata")
Expand Down