command: print appropriate warning messages on 'context list'/'contex… by nicks · Pull Request #3668 · docker/cli (original) (raw)

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation14 Commits2 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

nicks

@codecov-commenter

Codecov Report

Merging #3668 (f31397f) into master (c59773f) will increase coverage by 0.02%.
The diff coverage is 80.00%.

❗ Current head f31397f differs from pull request most recent head 49f4db9. Consider uploading reports for the commit 49f4db9 to get more accurate results

@@ Coverage Diff @@ ## master #3668 +/- ##

thaJeztah

if os.Getenv(client.EnvOverrideHost) != "" {
if _, present := os.LookupEnv(client.EnvOverrideHost); present {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked closely at the other places where this is checked, but wondering if instead of checking if the env-var is found, we should just take the "simple" approach and treat "empty" and "unset" as equivalent everywhere.

When scripting, it's not uncommon to have a variable set to an empty string (to "reset it"), and I think it would be ok for us to ignore in that case.

@nicks

@thaJeztah I updated the PR so that it treats DOCKER_HOST="" and DOCKER_HOST unset the same way.

nicks

@@ -456,7 +456,7 @@ func resolveContextName(opts *cliflags.CommonOptions, config *configfile.ConfigF
if len(opts.Hosts) > 0 {
return DefaultContextName, nil
}
if _, present := os.LookupEnv(client.EnvOverrideHost); present {
if os.Getenv(client.EnvOverrideHost) != "" {
return DefaultContextName, nil
}
if ctxName, ok := os.LookupEnv("DOCKER_CONTEXT"); ok {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also do the same thing with DOCKER_CONTEXT, for consistency?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, missed your comment; yes, I think we should do the same (Or at least, I don't see a good reason to consider an empty env-var to be used 🤔)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

thaJeztah

Comment on lines 46 to 48

host := os.Getenv(client.EnvOverrideHost)
isIneffective := host != "" && configValue != command.DefaultContextName
if isIneffective {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: need to have another look at this logic 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Looks like this one needs to check either configValue != "" or name != command.DefaultContextName, because a couple of lines up, configValue is set to an empty string when using default;

configValue := name
if configValue == "default" {
configValue = ""
}

@nicks @thaJeztah

print appropriate warning messages on 'context list'/'context use'

Signed-off-by: Nick Santos nick.santos@docker.com Signed-off-by: Sebastiaan van Stijn github@gone.nl

@nicks @thaJeztah

Signed-off-by: Nick Santos nick.santos@docker.com

thaJeztah

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; I found some minor issues, and one issue with the check; we can probably also squash the first two commits (as the second one is touching-up the first one).

I was writing along while reviewing, so let me push to your branch to get this going.

Comment on lines 46 to 48

host := os.Getenv(client.EnvOverrideHost)
isIneffective := host != "" && configValue != command.DefaultContextName
if isIneffective {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Looks like this one needs to check either configValue != "" or name != command.DefaultContextName, because a couple of lines up, configValue is set to an empty string when using default;

configValue := name
if configValue == "default" {
configValue = ""
}

Comment on lines 65 to 78

assert.Assert(t,
strings.Contains(
cli.ErrBuffer().String(),
`Warning: DOCKER_HOST environment variable overrides the active context.`))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the above; we could probably add a test here then that verifies that the warning is not triggered for default.

err = newUseCommand(cli).RunE(nil, []string{"test"})
assert.NilError(t, err)
assert.Assert(t, !strings.Contains(out.String(), "Warning"))
assert.Assert(t, strings.Contains(out.String(), "Current context is now \"test\""))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; could probably use ` as quotes make it slightly more readable

For the string comparing, we can use gotest.tools/v3/assert/cmp, which prints a nice diff if things aren't equal.

configDir := t.TempDir()
config.SetDir(configDir)
socketPath := fmt.Sprintf("unix://%s", filepath.Join(configDir, "docker.sock"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; for simple cases, we generally prefer to just concatenate;

socketPath := "unix://" + filepath.Join(configDir, "docker.sock")

thaJeztah

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is happy; let's get this one in!

LGTM

@nicks

@nicks nicks deleted the nicks/issue-3667 branch

July 29, 2022 20:56

@bzhb bzhb mentioned this pull request

Aug 31, 2022