runUse set the current Docker context
(dockerCLI command.Cli, name string)
| 27 | |
| 28 | // runUse set the current Docker context |
| 29 | func runUse(dockerCLI command.Cli, name string) error { |
| 30 | // configValue uses an empty string for "default" |
| 31 | var configValue string |
| 32 | if name != command.DefaultContextName { |
| 33 | if err := store.ValidateContextName(name); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if _, err := dockerCLI.ContextStore().GetMetadata(name); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | configValue = name |
| 40 | } |
| 41 | dockerConfig := dockerCLI.ConfigFile() |
| 42 | // Avoid updating the config-file if nothing changed. This also prevents |
| 43 | // creating the file and config-directory if the default is used and |
| 44 | // no config-file existed yet. |
| 45 | if dockerConfig.CurrentContext != configValue { |
| 46 | dockerConfig.CurrentContext = configValue |
| 47 | if err := dockerConfig.Save(); err != nil { |
| 48 | return err |
| 49 | } |
| 50 | } |
| 51 | _, _ = fmt.Fprintln(dockerCLI.Out(), name) |
| 52 | _, _ = fmt.Fprintf(dockerCLI.Err(), "Current context is now %q\n", name) |
| 53 | if name != command.DefaultContextName && os.Getenv(client.EnvOverrideHost) != "" { |
| 54 | _, _ = fmt.Fprintf(dockerCLI.Err(), "Warning: %[1]s environment variable overrides the active context. "+ |
| 55 | "To use %[2]q, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost, name) |
| 56 | } |
| 57 | return nil |
| 58 | } |
no test coverage detected
searching dependent graphs…