ls.go 805 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package context
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "text/tabwriter"
  7. "github.com/spf13/cobra"
  8. "github.com/docker/api/context/store"
  9. )
  10. func listCommand() *cobra.Command {
  11. cmd := &cobra.Command{
  12. Use: "list",
  13. Short: "List available contexts",
  14. Aliases: []string{"ls"},
  15. Args: cobra.NoArgs,
  16. RunE: func(cmd *cobra.Command, args []string) error {
  17. return runList(cmd.Context())
  18. },
  19. }
  20. return cmd
  21. }
  22. func runList(ctx context.Context) error {
  23. s := store.ContextStore(ctx)
  24. contexts, err := s.List()
  25. if err != nil {
  26. return err
  27. }
  28. w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
  29. fmt.Fprintln(w, "NAME\tDESCRIPTION\tTYPE")
  30. format := "%s\t%s\t%s\n"
  31. for _, c := range contexts {
  32. fmt.Fprintf(w, format, c.Name, c.Metadata.Description, c.Metadata.Type)
  33. }
  34. return w.Flush()
  35. }