list.go 2.9 KB

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