list.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package cmd
  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. // listCmd represents the "porter list" base command and "porter list all" subcommand
  17. var 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 := checkLoginAndRun(cmd.Context(), 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. var 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 := checkLoginAndRun(cmd.Context(), args, listApps)
  37. if err != nil {
  38. os.Exit(1)
  39. }
  40. },
  41. }
  42. var 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 := checkLoginAndRun(cmd.Context(), args, listJobs)
  48. if err != nil {
  49. os.Exit(1)
  50. }
  51. },
  52. }
  53. var 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 := checkLoginAndRun(cmd.Context(), args, listAddons)
  59. if err != nil {
  60. os.Exit(1)
  61. }
  62. },
  63. }
  64. func init() {
  65. listCmd.PersistentFlags().StringVar(
  66. &namespace,
  67. "namespace",
  68. "default",
  69. "the namespace of the release",
  70. )
  71. listCmd.PersistentFlags().BoolVar(
  72. &allNamespaces,
  73. "all-namespaces",
  74. false,
  75. "list resources for all namespaces",
  76. )
  77. listCmd.AddCommand(listAppsCmd)
  78. listCmd.AddCommand(listJobsCmd)
  79. listCmd.AddCommand(listAddonsCmd)
  80. rootCmd.AddCommand(listCmd)
  81. }
  82. func listAll(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  83. project, err := client.GetProject(ctx, cliConf.Project)
  84. if err != nil {
  85. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  86. }
  87. if project.ValidateApplyV2 {
  88. err = v2.ListAll(ctx)
  89. if err != nil {
  90. return err
  91. }
  92. return nil
  93. }
  94. err = writeReleases(ctx, client, cliConf, "all")
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. func listApps(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  101. project, err := client.GetProject(ctx, cliConf.Project)
  102. if err != nil {
  103. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  104. }
  105. if project.ValidateApplyV2 {
  106. err = v2.ListApps(ctx)
  107. if err != nil {
  108. return err
  109. }
  110. return nil
  111. }
  112. err = writeReleases(ctx, client, cliConf, "application")
  113. if err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. func listJobs(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  119. project, err := client.GetProject(ctx, cliConf.Project)
  120. if err != nil {
  121. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  122. }
  123. if project.ValidateApplyV2 {
  124. err = v2.ListJobs(ctx)
  125. if err != nil {
  126. return err
  127. }
  128. return nil
  129. }
  130. err = writeReleases(ctx, client, cliConf, "job")
  131. if err != nil {
  132. return err
  133. }
  134. return nil
  135. }
  136. func listAddons(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  137. err := writeReleases(ctx, client, cliConf, "addon")
  138. if err != nil {
  139. return err
  140. }
  141. return nil
  142. }
  143. func writeReleases(ctx context.Context, client api.Client, cliConf config.CLIConfig, kind string) error {
  144. var namespaces []string
  145. var releases []*release.Release
  146. if allNamespaces {
  147. resp, err := client.GetK8sNamespaces(ctx, cliConf.Project, cliConf.Cluster)
  148. if err != nil {
  149. return err
  150. }
  151. namespaceResp := *resp
  152. for _, ns := range namespaceResp {
  153. namespaces = append(namespaces, ns.Name)
  154. }
  155. } else {
  156. namespaces = append(namespaces, namespace)
  157. }
  158. for _, ns := range namespaces {
  159. resp, err := client.ListReleases(ctx, cliConf.Project, cliConf.Cluster, ns,
  160. &types.ListReleasesRequest{
  161. ReleaseListFilter: &types.ReleaseListFilter{
  162. Limit: 50,
  163. Skip: 0,
  164. StatusFilter: []string{
  165. "deployed",
  166. "uninstalled",
  167. "pending",
  168. "pending-install",
  169. "pending-upgrade",
  170. "pending-rollback",
  171. "failed",
  172. },
  173. },
  174. },
  175. )
  176. if err != nil {
  177. return err
  178. }
  179. releases = append(releases, resp...)
  180. }
  181. w := new(tabwriter.Writer)
  182. w.Init(os.Stdout, 3, 8, 2, '\t', tabwriter.AlignRight)
  183. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "NAME", "NAMESPACE", "STATUS", "KIND")
  184. for _, rel := range releases {
  185. chartName := rel.Chart.Name()
  186. if kind == "all" {
  187. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  188. } else if kind == "application" && (chartName == "web" || chartName == "worker") {
  189. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  190. } else if kind == "job" && chartName == "job" {
  191. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  192. } else if kind == "addon" && chartName != "web" && chartName != "worker" && chartName != "job" {
  193. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  194. }
  195. }
  196. w.Flush()
  197. return nil
  198. }