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-11-04 09:07:20 -04:00
|
|
|
contexts []*formatter.ClientContext
|
|
|
|
)
|
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(),
|
|
|
|
})
|
|
|
|
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,
|
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,
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|