compose/cli/cmd/context/ls.go

161 lines
3.9 KiB
Go
Raw Normal View History

2020-05-14 21:13:07 +02:00
/*
Copyright 2020 Docker Compose CLI authors
2020-06-18 16:13:24 +02:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2020-05-14 21:13:07 +02:00
*/
2020-05-14 20:55:40 +02:00
package context
import (
"fmt"
"io"
2020-05-14 20:55:40 +02:00
"os"
"sort"
"strings"
2020-05-14 20:55:40 +02:00
"github.com/pkg/errors"
2020-05-14 20:55:40 +02:00
"github.com/spf13/cobra"
"github.com/docker/compose-cli/cli/mobycli"
apicontext "github.com/docker/compose-cli/context"
"github.com/docker/compose-cli/context/store"
"github.com/docker/compose-cli/formatter"
2020-05-14 20:55:40 +02:00
)
type lsOpts struct {
quiet bool
json bool
format string
}
func (o lsOpts) validate() error {
if o.quiet && o.json {
return errors.New(`cannot combine "quiet" and "json" options`)
}
return nil
}
2020-05-14 20:55:40 +02:00
func listCommand() *cobra.Command {
var opts lsOpts
2020-05-14 20:55:40 +02:00
cmd := &cobra.Command{
Use: "list",
Short: "List available contexts",
Aliases: []string{"ls"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runList(cmd, opts)
2020-05-14 20:55:40 +02:00
},
}
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
cmd.Flags().BoolVar(&opts.json, "json", false, "Format output as JSON")
cmd.Flags().StringVar(&opts.format, "format", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
_ = cmd.Flags().MarkHidden("json")
2020-05-14 20:55:40 +02:00
return cmd
}
func runList(cmd *cobra.Command, opts lsOpts) error {
err := opts.validate()
if err != nil {
return err
}
if opts.format != "" && opts.format != formatter.JSON && opts.format != formatter.PRETTY {
mobycli.Exec(cmd.Root())
return nil
}
ctx := cmd.Context()
2020-05-18 15:16:05 +02:00
currentContext := apicontext.CurrentContext(ctx)
2020-05-14 20:55:40 +02:00
s := store.ContextStore(ctx)
contexts, err := s.List()
if err != nil {
return err
}
sort.Slice(contexts, func(i, j int) bool {
return strings.Compare(contexts[i].Name, contexts[j].Name) == -1
})
if opts.quiet {
for _, c := range contexts {
fmt.Println(c.Name)
}
return nil
}
if opts.json {
opts.format = formatter.JSON
}
view := viewFromContextList(contexts, currentContext)
return formatter.Print(view, opts.format, os.Stdout,
func(w io.Writer) {
for _, c := range view {
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
c.Name,
c.Type,
c.Description,
c.DockerEndpoint,
c.KubernetesEndpoint,
c.Orchestrator)
}
},
"NAME", "TYPE", "DESCRIPTION", "DOCKER ENDPOINT", "KUBERNETES ENDPOINT", "ORCHESTRATOR")
2020-05-14 20:55:40 +02:00
}
func getEndpoint(name string, meta map[string]interface{}) string {
endpoints, ok := meta[name]
if !ok {
return ""
}
data, ok := endpoints.(*store.Endpoint)
if !ok {
return ""
}
result := data.Host
if data.DefaultNamespace != "" {
result += fmt.Sprintf(" (%s)", data.DefaultNamespace)
}
return result
}
type contextView struct {
Name string
Type string
Description string
DockerEndpoint string
KubernetesEndpoint string
Orchestrator string
}
func viewFromContextList(contextList []*store.DockerContext, currentContext string) []contextView {
retList := make([]contextView, len(contextList))
for i, c := range contextList {
contextName := c.Name
if c.Name == currentContext {
contextName += " *"
}
retList[i] = contextView{
Name: contextName,
Type: c.Type(),
Description: c.Metadata.Description,
DockerEndpoint: getEndpoint("docker", c.Endpoints),
KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints),
Orchestrator: c.Metadata.StackOrchestrator,
}
}
return retList
}