list.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "text/tabwriter"
  7. v2 "github.com/porter-dev/porter/cli/cmd/v2"
  8. "github.com/fatih/color"
  9. api "github.com/porter-dev/porter/api/client"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/spf13/cobra"
  12. "github.com/stefanmcshane/helm/pkg/release"
  13. )
  14. var allNamespaces bool
  15. // listCmd represents the "porter list" base command and "porter list all" subcommand
  16. var listCmd = &cobra.Command{
  17. Use: "list",
  18. Short: "List applications, addons or jobs.",
  19. Run: func(cmd *cobra.Command, args []string) {
  20. if len(args) == 0 || (args[0] == "all") {
  21. err := checkLoginAndRun(args, listAll)
  22. if err != nil {
  23. os.Exit(1)
  24. }
  25. } else {
  26. color.New(color.FgRed).Fprintf(os.Stderr, "invalid command: %s\n", args[0])
  27. }
  28. },
  29. }
  30. var listAppsCmd = &cobra.Command{
  31. Use: "apps",
  32. Aliases: []string{"applications", "app", "application"},
  33. Short: "Lists applications in a specific namespace, or across all namespaces",
  34. Run: func(cmd *cobra.Command, args []string) {
  35. err := checkLoginAndRun(args, listApps)
  36. if err != nil {
  37. os.Exit(1)
  38. }
  39. },
  40. }
  41. var listJobsCmd = &cobra.Command{
  42. Use: "jobs",
  43. Aliases: []string{"job"},
  44. Short: "Lists jobs in a specific namespace, or across all namespaces",
  45. Run: func(cmd *cobra.Command, args []string) {
  46. err := checkLoginAndRun(args, listJobs)
  47. if err != nil {
  48. os.Exit(1)
  49. }
  50. },
  51. }
  52. var listAddonsCmd = &cobra.Command{
  53. Use: "addons",
  54. Aliases: []string{"addon"},
  55. Short: "Lists addons in a specific namespace, or across all namespaces",
  56. Run: func(cmd *cobra.Command, args []string) {
  57. err := checkLoginAndRun(args, listAddons)
  58. if err != nil {
  59. os.Exit(1)
  60. }
  61. },
  62. }
  63. func init() {
  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. rootCmd.AddCommand(listCmd)
  80. }
  81. func listAll(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  82. ctx := context.Background()
  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(client, "all")
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. func listApps(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  101. ctx := context.Background()
  102. project, err := client.GetProject(ctx, cliConf.Project)
  103. if err != nil {
  104. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  105. }
  106. if project.ValidateApplyV2 {
  107. err = v2.ListApps(ctx)
  108. if err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. err = writeReleases(client, "application")
  114. if err != nil {
  115. return err
  116. }
  117. return nil
  118. }
  119. func listJobs(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  120. ctx := context.Background()
  121. project, err := client.GetProject(ctx, cliConf.Project)
  122. if err != nil {
  123. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  124. }
  125. if project.ValidateApplyV2 {
  126. err = v2.ListJobs(ctx)
  127. if err != nil {
  128. return err
  129. }
  130. return nil
  131. }
  132. err = writeReleases(client, "job")
  133. if err != nil {
  134. return err
  135. }
  136. return nil
  137. }
  138. func listAddons(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  139. err := writeReleases(client, "addon")
  140. if err != nil {
  141. return err
  142. }
  143. return nil
  144. }
  145. func writeReleases(client *api.Client, kind string) error {
  146. var namespaces []string
  147. var releases []*release.Release
  148. if allNamespaces {
  149. resp, err := client.GetK8sNamespaces(context.Background(), cliConf.Project, cliConf.Cluster)
  150. if err != nil {
  151. return err
  152. }
  153. namespaceResp := *resp
  154. for _, ns := range namespaceResp {
  155. namespaces = append(namespaces, ns.Name)
  156. }
  157. } else {
  158. namespaces = append(namespaces, namespace)
  159. }
  160. for _, ns := range namespaces {
  161. resp, err := client.ListReleases(context.Background(), cliConf.Project, cliConf.Cluster, ns,
  162. &types.ListReleasesRequest{
  163. ReleaseListFilter: &types.ReleaseListFilter{
  164. Limit: 50,
  165. Skip: 0,
  166. StatusFilter: []string{
  167. "deployed",
  168. "uninstalled",
  169. "pending",
  170. "pending-install",
  171. "pending-upgrade",
  172. "pending-rollback",
  173. "failed",
  174. },
  175. },
  176. },
  177. )
  178. if err != nil {
  179. return err
  180. }
  181. releases = append(releases, resp...)
  182. }
  183. w := new(tabwriter.Writer)
  184. w.Init(os.Stdout, 3, 8, 2, '\t', tabwriter.AlignRight)
  185. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "NAME", "NAMESPACE", "STATUS", "KIND")
  186. for _, rel := range releases {
  187. chartName := rel.Chart.Name()
  188. if kind == "all" {
  189. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  190. } else if kind == "application" && (chartName == "web" || chartName == "worker") {
  191. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  192. } else if kind == "job" && chartName == "job" {
  193. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  194. } else if kind == "addon" && chartName != "web" && chartName != "worker" && chartName != "job" {
  195. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", rel.Name, rel.Namespace, rel.Info.Status, chartName)
  196. }
  197. }
  198. w.Flush()
  199. return nil
  200. }