list.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "text/tabwriter"
  7. "github.com/fatih/color"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/spf13/cobra"
  11. )
  12. // listCmd represents the "porter list" base command and "porter list all" subcommand
  13. var listCmd = &cobra.Command{
  14. Use: "list",
  15. Short: "List applications, addons or jobs.",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. if len(args) == 0 || (args[0] == "all") {
  18. err := checkLoginAndRun(args, listAll)
  19. if err != nil {
  20. os.Exit(1)
  21. }
  22. } else {
  23. color.New(color.FgRed).Printf("invalid command: %s\n", args[0])
  24. }
  25. },
  26. }
  27. var listAppsCmd = &cobra.Command{
  28. Use: "apps",
  29. Aliases: []string{"applications", "app", "application"},
  30. Short: "Lists applications in a specific namespace, or across all namespaces",
  31. Run: func(cmd *cobra.Command, args []string) {
  32. err := checkLoginAndRun(args, listApps)
  33. if err != nil {
  34. os.Exit(1)
  35. }
  36. },
  37. }
  38. var listJobsCmd = &cobra.Command{
  39. Use: "jobs",
  40. Aliases: []string{"job"},
  41. Short: "Lists jobs in a specific namespace, or across all namespaces",
  42. Run: func(cmd *cobra.Command, args []string) {
  43. err := checkLoginAndRun(args, listJobs)
  44. if err != nil {
  45. os.Exit(1)
  46. }
  47. },
  48. }
  49. var listAddonsCmd = &cobra.Command{
  50. Use: "addons",
  51. Aliases: []string{"addon"},
  52. Short: "Lists addons in a specific namespace, or across all namespaces",
  53. Run: func(cmd *cobra.Command, args []string) {
  54. err := checkLoginAndRun(args, listAddons)
  55. if err != nil {
  56. os.Exit(1)
  57. }
  58. },
  59. }
  60. func init() {
  61. listCmd.PersistentFlags().StringVar(
  62. &namespace,
  63. "namespace",
  64. "default",
  65. "the namespace of the release",
  66. )
  67. listCmd.AddCommand(listAppsCmd)
  68. listCmd.AddCommand(listJobsCmd)
  69. listCmd.AddCommand(listAddonsCmd)
  70. rootCmd.AddCommand(listCmd)
  71. }
  72. func listAll(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  73. err := writeReleases(client, "all")
  74. if err != nil {
  75. return err
  76. }
  77. return nil
  78. }
  79. func listApps(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  80. err := writeReleases(client, "application")
  81. if err != nil {
  82. return err
  83. }
  84. return nil
  85. }
  86. func listJobs(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  87. err := writeReleases(client, "job")
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. func listAddons(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  94. err := writeReleases(client, "addon")
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. func writeReleases(client *api.Client, kind string) error {
  101. releases, err := client.ListReleases(context.Background(), cliConf.Project, cliConf.Cluster, namespace, &types.ListReleasesRequest{
  102. ReleaseListFilter: &types.ReleaseListFilter{
  103. Limit: 50,
  104. Skip: 0,
  105. StatusFilter: []string{
  106. "deployed",
  107. "uninstalled",
  108. "pending",
  109. "pending-install",
  110. "pending-upgrade",
  111. "pending-rollback",
  112. "failed",
  113. },
  114. },
  115. })
  116. if err != nil {
  117. return err
  118. }
  119. w := new(tabwriter.Writer)
  120. w.Init(os.Stdout, 3, 8, 2, '\t', tabwriter.AlignRight)
  121. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "NAME", "NAMESPACE", "STATUS", "KIND")
  122. for _, rel := range releases {
  123. chartName := rel.Chart.Name()
  124. if kind == "all" {
  125. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  126. } else if kind == "application" && (chartName == "web" || chartName == "worker") {
  127. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  128. } else if kind == "job" && chartName == "job" {
  129. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  130. } else if kind == "addon" && chartName != "web" && chartName != "worker" && chartName != "job" {
  131. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  132. }
  133. }
  134. w.Flush()
  135. return nil
  136. }