2024-06-07 11:29:24 -04:00
|
|
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
|
|
|
|
//go:build go1.19
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-09 11:12:12 -04:00
|
|
|
"os"
|
2018-11-09 09:10:41 -05:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2018-11-09 09:10:41 -05:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/cli/cli/context/docker"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2022-03-24 19:15:14 -04:00
|
|
|
"github.com/docker/docker/client"
|
2020-08-28 08:35:09 -04:00
|
|
|
"github.com/fvbommel/sortorder"
|
2018-11-09 09:10:41 -05:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
format string
|
|
|
|
quiet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
|
|
|
opts := &listOptions{}
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List contexts",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runList(dockerCli, opts)
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
|
2018-11-09 09:10:41 -05:00
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runList(dockerCli command.Cli, opts *listOptions) error {
|
|
|
|
if opts.format == "" {
|
|
|
|
opts.format = formatter.TableFormatKey
|
|
|
|
}
|
2019-04-18 09:12:30 -04:00
|
|
|
contextMap, err := dockerCli.ContextStore().List()
|
2018-11-09 09:10:41 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-04 09:07:20 -04:00
|
|
|
var (
|
|
|
|
curContext = dockerCli.CurrentContext()
|
2022-11-04 10:48:30 -04:00
|
|
|
curFound bool
|
2022-09-03 14:07:29 -04:00
|
|
|
contexts = make([]*formatter.ClientContext, 0, len(contextMap))
|
2022-11-04 09:07:20 -04:00
|
|
|
)
|
2018-11-09 09:10:41 -05:00
|
|
|
for _, rawMeta := range contextMap {
|
2022-11-04 09:07:20 -04:00
|
|
|
isCurrent := rawMeta.Name == curContext
|
2022-11-04 10:48:30 -04:00
|
|
|
if isCurrent {
|
|
|
|
curFound = true
|
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
meta, err := command.GetDockerContext(rawMeta)
|
|
|
|
if err != nil {
|
2022-11-04 09:39:07 -04:00
|
|
|
// Add a stub-entry to the list, including the error-message
|
|
|
|
// indicating that the context couldn't be loaded.
|
|
|
|
contexts = append(contexts, &formatter.ClientContext{
|
|
|
|
Name: rawMeta.Name,
|
|
|
|
Current: isCurrent,
|
|
|
|
Error: err.Error(),
|
2024-05-30 08:33:15 -04:00
|
|
|
|
|
|
|
ContextType: getContextType(nil, opts.format),
|
2022-11-04 09:39:07 -04:00
|
|
|
})
|
|
|
|
continue
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
2022-11-04 09:39:07 -04:00
|
|
|
var errMsg string
|
2018-11-09 09:10:41 -05:00
|
|
|
dockerEndpoint, err := docker.EndpointFromContext(rawMeta)
|
|
|
|
if err != nil {
|
2022-11-04 09:39:07 -04:00
|
|
|
errMsg = err.Error()
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
desc := formatter.ClientContext{
|
2022-02-22 07:46:35 -05:00
|
|
|
Name: rawMeta.Name,
|
2022-11-04 09:07:20 -04:00
|
|
|
Current: isCurrent,
|
2022-02-22 07:46:35 -05:00
|
|
|
Description: meta.Description,
|
|
|
|
DockerEndpoint: dockerEndpoint.Host,
|
2022-11-04 09:39:07 -04:00
|
|
|
Error: errMsg,
|
2024-05-30 08:33:15 -04:00
|
|
|
|
|
|
|
ContextType: getContextType(meta.AdditionalFields, opts.format),
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
contexts = append(contexts, &desc)
|
|
|
|
}
|
2022-11-04 10:48:30 -04:00
|
|
|
if !curFound {
|
|
|
|
// The currently specified context wasn't found. We add a stub-entry
|
|
|
|
// to the list, including the error-message indicating that the context
|
|
|
|
// wasn't found.
|
|
|
|
var errMsg string
|
|
|
|
_, err := dockerCli.ContextStore().GetMetadata(curContext)
|
|
|
|
if err != nil {
|
|
|
|
errMsg = err.Error()
|
|
|
|
}
|
|
|
|
contexts = append(contexts, &formatter.ClientContext{
|
|
|
|
Name: curContext,
|
|
|
|
Current: true,
|
|
|
|
Error: errMsg,
|
2024-05-30 08:33:15 -04:00
|
|
|
|
|
|
|
ContextType: getContextType(nil, opts.format),
|
2022-11-04 10:48:30 -04:00
|
|
|
})
|
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
sort.Slice(contexts, func(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(contexts[i].Name, contexts[j].Name)
|
|
|
|
})
|
2019-04-09 11:12:12 -04:00
|
|
|
if err := format(dockerCli, opts, contexts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-24 19:15:14 -04:00
|
|
|
if os.Getenv(client.EnvOverrideHost) != "" {
|
2022-11-04 09:07:20 -04:00
|
|
|
_, _ = fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+
|
2022-03-24 19:15:14 -04:00
|
|
|
"To use a context, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost)
|
2019-04-09 11:12:12 -04:00
|
|
|
}
|
|
|
|
return nil
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
2024-05-30 08:33:15 -04:00
|
|
|
// getContextType sets the LegacyContextType field for compatibility with
|
|
|
|
// Visual Studio, which depends on this field from the "cloud integration"
|
|
|
|
// wrapper.
|
|
|
|
//
|
|
|
|
// https://github.com/docker/compose-cli/blob/c156ce6da4c2b317174d42daf1b019efa87e9f92/api/context/store/contextmetadata.go#L28-L34
|
|
|
|
// https://github.com/docker/compose-cli/blob/c156ce6da4c2b317174d42daf1b019efa87e9f92/api/context/store/store.go#L34-L51
|
|
|
|
//
|
|
|
|
// TODO(thaJeztah): remove this and [ClientContext.ContextType] once Visual Studio is updated to no longer depend on this.
|
|
|
|
func getContextType(meta map[string]any, format string) string {
|
|
|
|
if format != formatter.JSONFormat && format != formatter.JSONFormatKey {
|
|
|
|
// We only need the ContextType field when formatting as JSON,
|
|
|
|
// which is the format-string used by Visual Studio to detect the
|
|
|
|
// context-type.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if ct, ok := meta["Type"]; ok {
|
|
|
|
// If the context on-disk has a context-type (ecs, aci), return it.
|
|
|
|
return ct.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the default context-type.
|
|
|
|
return "moby"
|
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func format(dockerCli command.Cli, opts *listOptions, contexts []*formatter.ClientContext) error {
|
|
|
|
contextCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewClientContextFormat(opts.format, opts.quiet),
|
|
|
|
}
|
|
|
|
return formatter.ClientContextWrite(contextCtx, contexts)
|
|
|
|
}
|