MCPcopy Index your code
hub / github.com/docker/cli / runUse

Function runUse

cli/command/context/use.go:29–58  ·  view source on GitHub ↗

runUse set the current Docker context

(dockerCLI command.Cli, name string)

Source from the content-addressed store, hash-verified

27
28// runUse set the current Docker context
29func 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}

Callers 1

newUseCommandFunction · 0.85

Calls 6

GetMetadataMethod · 0.65
ContextStoreMethod · 0.65
ConfigFileMethod · 0.65
SaveMethod · 0.65
OutMethod · 0.65
ErrMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…