These functions and types are shallow wrappers around the context store and were intended for internal use as implementation for the CLI itself. They were exported in3126920af1to be used by plugins and Docker Desktop. However, there's currently no public uses of this, and Docker Desktop does not use these functions. These were deprecated in95eeafa551and are no longer used. This patch removes the deprecated functions as they were meant to be implementation specific for the CLI. If there's a need to provide utilities for manipulating the context-store other than through the CLI itself, we can consider creating an SDK for that purpose. This removes: - `RunCreate` and `CreateOptions` - `RunExport` and `ExportOptions` - `RunImport` - `RunRemove` and `RemoveOptions` - `RunUpdate` and `UpdateOptions` - `RunUse` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package context
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/docker/cli/cli/context/store"
|
|
"github.com/moby/moby/client"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func newUseCommand(dockerCLI command.Cli) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "use CONTEXT",
|
|
Short: "Set the current docker context",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
name := args[0]
|
|
return runUse(dockerCLI, name)
|
|
},
|
|
ValidArgsFunction: completeContextNames(dockerCLI, 1, false),
|
|
}
|
|
return cmd
|
|
}
|
|
|
|
// runUse set the current Docker context
|
|
func runUse(dockerCLI command.Cli, name string) error {
|
|
// configValue uses an empty string for "default"
|
|
var configValue string
|
|
if name != command.DefaultContextName {
|
|
if err := store.ValidateContextName(name); err != nil {
|
|
return err
|
|
}
|
|
if _, err := dockerCLI.ContextStore().GetMetadata(name); err != nil {
|
|
return err
|
|
}
|
|
configValue = name
|
|
}
|
|
dockerConfig := dockerCLI.ConfigFile()
|
|
// Avoid updating the config-file if nothing changed. This also prevents
|
|
// creating the file and config-directory if the default is used and
|
|
// no config-file existed yet.
|
|
if dockerConfig.CurrentContext != configValue {
|
|
dockerConfig.CurrentContext = configValue
|
|
if err := dockerConfig.Save(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
|
|
_, _ = fmt.Fprintf(dockerCLI.Err(), "Current context is now %q\n", name)
|
|
if name != command.DefaultContextName && os.Getenv(client.EnvOverrideHost) != "" {
|
|
_, _ = fmt.Fprintf(dockerCLI.Err(), "Warning: %[1]s environment variable overrides the active context. "+
|
|
"To use %[2]q, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost, name)
|
|
}
|
|
return nil
|
|
}
|