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"
|
|
|
|
"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"
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
curContext := dockerCli.CurrentContext()
|
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
|
|
|
|
}
|
|
|
|
var contexts []*formatter.ClientContext
|
|
|
|
for _, rawMeta := range contextMap {
|
|
|
|
meta, err := command.GetDockerContext(rawMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dockerEndpoint, err := docker.EndpointFromContext(rawMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-06 09:01:12 -05:00
|
|
|
if rawMeta.Name == command.DefaultContextName {
|
|
|
|
meta.Description = "Current DOCKER_HOST based configuration"
|
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
desc := formatter.ClientContext{
|
2022-02-22 07:46:35 -05:00
|
|
|
Name: rawMeta.Name,
|
|
|
|
Current: rawMeta.Name == curContext,
|
|
|
|
Description: meta.Description,
|
|
|
|
DockerEndpoint: dockerEndpoint.Host,
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
contexts = append(contexts, &desc)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if os.Getenv("DOCKER_HOST") != "" {
|
|
|
|
fmt.Fprint(dockerCli.Err(), "Warning: DOCKER_HOST environment variable overrides the active context. "+
|
|
|
|
"To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable.\n")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|