list.go 4.5 KB

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