-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
command: print appropriate warning messages on 'context list'/'contex… #3668
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3668 +/- ##
==========================================
+ Coverage 59.01% 59.03% +0.02%
==========================================
Files 289 289
Lines 24623 24626 +3
==========================================
+ Hits 14532 14539 +7
+ Misses 9218 9215 -3
+ Partials 873 872 -1 |
cli/command/context/list.go
Outdated
if os.Getenv(client.EnvOverrideHost) != "" { | ||
if _, present := os.LookupEnv(client.EnvOverrideHost); present { |
There was a problem hiding this comment.
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.
49f4db9
to
2e494c1
Compare
@thaJeztah I updated the PR so that it treats DOCKER_HOST="" and DOCKER_HOST unset the same way. |
b622717
to
c5d33b9
Compare
cli/command/cli.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🤔)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated!
cli/command/context/use.go
Outdated
host := os.Getenv(client.EnvOverrideHost) | ||
isIneffective := host != "" && configValue != command.DefaultContextName | ||
if isIneffective { |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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
;
cli/cli/command/context/use.go
Lines 34 to 37 in a445d97
configValue := name | |
if configValue == "default" { | |
configValue = "" | |
} |
print appropriate warning messages on 'context list'/'context use' Signed-off-by: Nick Santos <[email protected]> Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Nick Santos <[email protected]>
There was a problem hiding this comment.
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.
cli/command/context/use.go
Outdated
host := os.Getenv(client.EnvOverrideHost) | ||
isIneffective := host != "" && configValue != command.DefaultContextName | ||
if isIneffective { |
There was a problem hiding this comment.
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
;
cli/cli/command/context/use.go
Lines 34 to 37 in a445d97
configValue := name | |
if configValue == "default" { | |
configValue = "" | |
} |
cli/command/context/use_test.go
Outdated
assert.Assert(t, | ||
strings.Contains( | ||
cli.ErrBuffer().String(), | ||
`Warning: DOCKER_HOST environment variable overrides the active context.`)) | ||
} |
There was a problem hiding this comment.
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
.
cli/command/context/use_test.go
Outdated
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\"")) |
There was a problem hiding this comment.
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.
cli/command/context/use_test.go
Outdated
configDir := t.TempDir() | ||
config.SetDir(configDir) | ||
|
||
socketPath := fmt.Sprintf("unix://%s", filepath.Join(configDir, "docker.sock")) |
There was a problem hiding this comment.
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")
dd6198e
to
50893d7
Compare
There was a problem hiding this comment.
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
woooo, thanks!! |
- What I did
print appropriate warning messages on 'docker context list' and 'docker context use'
- How I did it
Makes sure that the use/list implementations use the same logic for checking env
variables as the code that processes the override.
- How to verify it
see attached issues
docker context use
fails silently when DOCKER_HOST set to empty string #3667- Description for the changelog
print appropriate warning messages on 'docker context list' and 'docker context use'
- A picture of a cute animal (not mandatory but encouraged)