ls.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "context"
  16. "errors"
  17. "fmt"
  18. "os"
  19. "sort"
  20. "strings"
  21. "text/tabwriter"
  22. "github.com/spf13/cobra"
  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. }
  31. func (o lsOpts) validate() error {
  32. if o.quiet && o.json {
  33. return errors.New(`cannot combine "quiet" and "json" options`)
  34. }
  35. return nil
  36. }
  37. func listCommand() *cobra.Command {
  38. var opts lsOpts
  39. cmd := &cobra.Command{
  40. Use: "list",
  41. Short: "List available contexts",
  42. Aliases: []string{"ls"},
  43. Args: cobra.NoArgs,
  44. RunE: func(cmd *cobra.Command, args []string) error {
  45. return runList(cmd.Context(), opts)
  46. },
  47. }
  48. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
  49. cmd.Flags().BoolVar(&opts.json, "json", false, "Format output as JSON")
  50. return cmd
  51. }
  52. func runList(ctx context.Context, opts lsOpts) error {
  53. err := opts.validate()
  54. if err != nil {
  55. return err
  56. }
  57. currentContext := apicontext.CurrentContext(ctx)
  58. s := store.ContextStore(ctx)
  59. contexts, err := s.List()
  60. if err != nil {
  61. return err
  62. }
  63. sort.Slice(contexts, func(i, j int) bool {
  64. return strings.Compare(contexts[i].Name, contexts[j].Name) == -1
  65. })
  66. if opts.quiet {
  67. for _, c := range contexts {
  68. fmt.Println(c.Name)
  69. }
  70. return nil
  71. }
  72. if opts.json {
  73. j, err := formatter.ToStandardJSON(contexts)
  74. if err != nil {
  75. return err
  76. }
  77. fmt.Println(j)
  78. return nil
  79. }
  80. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  81. fmt.Fprintln(w, "NAME\tTYPE\tDESCRIPTION\tDOCKER ENDPOINT\tKUBERNETES ENDPOINT\tORCHESTRATOR")
  82. format := "%s\t%s\t%s\t%s\t%s\t%s\n"
  83. for _, c := range contexts {
  84. contextName := c.Name
  85. if c.Name == currentContext {
  86. contextName += " *"
  87. }
  88. fmt.Fprintf(w,
  89. format,
  90. contextName,
  91. c.Type(),
  92. c.Metadata.Description,
  93. getEndpoint("docker", c.Endpoints),
  94. getEndpoint("kubernetes", c.Endpoints),
  95. c.Metadata.StackOrchestrator)
  96. }
  97. return w.Flush()
  98. }
  99. func getEndpoint(name string, meta map[string]interface{}) string {
  100. endpoints, ok := meta[name]
  101. if !ok {
  102. return ""
  103. }
  104. data, ok := endpoints.(*store.Endpoint)
  105. if !ok {
  106. return ""
  107. }
  108. result := data.Host
  109. if data.DefaultNamespace != "" {
  110. result += fmt.Sprintf(" (%s)", data.DefaultNamespace)
  111. }
  112. return result
  113. }