Merge pull request #3877 from thaJeztah/minor_context_cleanups

context: various minor cleanups and fixes
This commit is contained in:
Sebastiaan van Stijn 2022-11-22 13:34:35 +01:00 committed by GitHub
commit de52868abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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