github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/context/list.go (about) 1 package context 2 3 import ( 4 "fmt" 5 "os" 6 "sort" 7 8 "github.com/docker/cli/cli" 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/cli/cli/command/formatter" 11 "github.com/docker/cli/cli/context/docker" 12 kubecontext "github.com/docker/cli/cli/context/kubernetes" 13 "github.com/fvbommel/sortorder" 14 "github.com/spf13/cobra" 15 ) 16 17 type listOptions struct { 18 format string 19 quiet bool 20 } 21 22 func newListCommand(dockerCli command.Cli) *cobra.Command { 23 opts := &listOptions{} 24 cmd := &cobra.Command{ 25 Use: "ls [OPTIONS]", 26 Aliases: []string{"list"}, 27 Short: "List contexts", 28 Args: cli.NoArgs, 29 RunE: func(cmd *cobra.Command, args []string) error { 30 return runList(dockerCli, opts) 31 }, 32 } 33 34 flags := cmd.Flags() 35 flags.StringVar(&opts.format, "format", "", "Pretty-print contexts using a Go template") 36 flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names") 37 return cmd 38 } 39 40 func runList(dockerCli command.Cli, opts *listOptions) error { 41 if opts.format == "" { 42 opts.format = formatter.TableFormatKey 43 } 44 curContext := dockerCli.CurrentContext() 45 contextMap, err := dockerCli.ContextStore().List() 46 if err != nil { 47 return err 48 } 49 var contexts []*formatter.ClientContext 50 for _, rawMeta := range contextMap { 51 meta, err := command.GetDockerContext(rawMeta) 52 if err != nil { 53 return err 54 } 55 dockerEndpoint, err := docker.EndpointFromContext(rawMeta) 56 if err != nil { 57 return err 58 } 59 kubernetesEndpoint := kubecontext.EndpointFromContext(rawMeta) 60 kubEndpointText := "" 61 if kubernetesEndpoint != nil { 62 kubEndpointText = fmt.Sprintf("%s (%s)", kubernetesEndpoint.Host, kubernetesEndpoint.DefaultNamespace) 63 } 64 if rawMeta.Name == command.DefaultContextName { 65 meta.Description = "Current DOCKER_HOST based configuration" 66 } 67 desc := formatter.ClientContext{ 68 Name: rawMeta.Name, 69 Current: rawMeta.Name == curContext, 70 Description: meta.Description, 71 StackOrchestrator: string(meta.StackOrchestrator), 72 DockerEndpoint: dockerEndpoint.Host, 73 KubernetesEndpoint: kubEndpointText, 74 } 75 contexts = append(contexts, &desc) 76 } 77 sort.Slice(contexts, func(i, j int) bool { 78 return sortorder.NaturalLess(contexts[i].Name, contexts[j].Name) 79 }) 80 if err := format(dockerCli, opts, contexts); err != nil { 81 return err 82 } 83 if os.Getenv("DOCKER_HOST") != "" { 84 fmt.Fprint(dockerCli.Err(), "Warning: DOCKER_HOST environment variable overrides the active context. "+ 85 "To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable.\n") 86 } 87 return nil 88 } 89 90 func format(dockerCli command.Cli, opts *listOptions, contexts []*formatter.ClientContext) error { 91 contextCtx := formatter.Context{ 92 Output: dockerCli.Out(), 93 Format: formatter.NewClientContextFormat(opts.format, opts.quiet), 94 } 95 return formatter.ClientContextWrite(contextCtx, contexts) 96 }