ls.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package context
  14. import (
  15. "errors"
  16. "fmt"
  17. "os"
  18. "sort"
  19. "strings"
  20. "text/tabwriter"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/api/cli/mobycli"
  23. apicontext "github.com/docker/api/context"
  24. "github.com/docker/api/context/store"
  25. "github.com/docker/api/formatter"
  26. )
  27. type lsOpts struct {
  28. quiet bool
  29. json bool
  30. format string
  31. }
  32. func (o lsOpts) validate() error {
  33. if o.quiet && o.json {
  34. return errors.New(`cannot combine "quiet" and "json" options`)
  35. }
  36. return nil
  37. }
  38. func listCommand() *cobra.Command {
  39. var opts lsOpts
  40. cmd := &cobra.Command{
  41. Use: "list",
  42. Short: "List available contexts",
  43. Aliases: []string{"ls"},
  44. Args: cobra.NoArgs,
  45. RunE: func(cmd *cobra.Command, args []string) error {
  46. return runList(cmd, opts)
  47. },
  48. }
  49. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
  50. cmd.Flags().BoolVar(&opts.json, "json", false, "Format output as JSON")
  51. cmd.Flags().StringVar(&opts.format, "format", "", "Format output as JSON")
  52. return cmd
  53. }
  54. func runList(cmd *cobra.Command, opts lsOpts) error {
  55. err := opts.validate()
  56. if err != nil {
  57. return err
  58. }
  59. if opts.format != "" {
  60. return mobycli.ExecCmd(cmd)
  61. }
  62. ctx := cmd.Context()
  63. currentContext := apicontext.CurrentContext(ctx)
  64. s := store.ContextStore(ctx)
  65. contexts, err := s.List()
  66. if err != nil {
  67. return err
  68. }
  69. sort.Slice(contexts, func(i, j int) bool {
  70. return strings.Compare(contexts[i].Name, contexts[j].Name) == -1
  71. })
  72. if opts.quiet {
  73. for _, c := range contexts {
  74. fmt.Println(c.Name)
  75. }
  76. return nil
  77. }
  78. if opts.json {
  79. j, err := formatter.ToStandardJSON(contexts)
  80. if err != nil {
  81. return err
  82. }
  83. fmt.Println(j)
  84. return nil
  85. }
  86. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  87. fmt.Fprintln(w, "NAME\tTYPE\tDESCRIPTION\tDOCKER ENDPOINT\tKUBERNETES ENDPOINT\tORCHESTRATOR")
  88. format := "%s\t%s\t%s\t%s\t%s\t%s\n"
  89. for _, c := range contexts {
  90. contextName := c.Name
  91. if c.Name == currentContext {
  92. contextName += " *"
  93. }
  94. fmt.Fprintf(w,
  95. format,
  96. contextName,
  97. c.Type(),
  98. c.Metadata.Description,
  99. getEndpoint("docker", c.Endpoints),
  100. getEndpoint("kubernetes", c.Endpoints),
  101. c.Metadata.StackOrchestrator)
  102. }
  103. return w.Flush()
  104. }
  105. func getEndpoint(name string, meta map[string]interface{}) string {
  106. endpoints, ok := meta[name]
  107. if !ok {
  108. return ""
  109. }
  110. data, ok := endpoints.(*store.Endpoint)
  111. if !ok {
  112. return ""
  113. }
  114. result := data.Host
  115. if data.DefaultNamespace != "" {
  116. result += fmt.Sprintf(" (%s)", data.DefaultNamespace)
  117. }
  118. return result
  119. }