list.go 5.4 KB

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