helm_repo.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "text/tabwriter"
  8. "github.com/fatih/color"
  9. "github.com/porter-dev/porter/cli/cmd/api"
  10. "github.com/spf13/cobra"
  11. )
  12. // helmRepoCmd represents the "porter helmrepo" base command when called
  13. // without any subcommands
  14. var helmRepoCmd = &cobra.Command{
  15. Use: "helmrepo",
  16. Aliases: []string{"helm", "helmrepos"},
  17. Short: "Commands that read from a connected Helm repository",
  18. }
  19. var helmRepoListCmd = &cobra.Command{
  20. Use: "list",
  21. Short: "Lists the Helm repositories linked to a project",
  22. Run: func(cmd *cobra.Command, args []string) {
  23. err := checkLoginAndRun(args, listHelmRepos)
  24. if err != nil {
  25. os.Exit(1)
  26. }
  27. },
  28. }
  29. var helmRepoChartCmd = &cobra.Command{
  30. Use: "chart",
  31. Aliases: []string{"charts"},
  32. Short: "Commands for interacting with Helm repository charts",
  33. }
  34. var helmRepoChartListCmd = &cobra.Command{
  35. Use: "list",
  36. Short: "Lists charts in the default Helm repository",
  37. Run: func(cmd *cobra.Command, args []string) {
  38. err := checkLoginAndRun(args, listHelmRepoCharts)
  39. if err != nil {
  40. os.Exit(1)
  41. }
  42. },
  43. }
  44. func init() {
  45. rootCmd.AddCommand(helmRepoCmd)
  46. helmRepoCmd.PersistentFlags().UintVar(
  47. &helmRepoID,
  48. "helmrepo-id",
  49. 0,
  50. "id of the helm repo",
  51. )
  52. helmRepoCmd.AddCommand(helmRepoListCmd)
  53. helmRepoCmd.AddCommand(helmRepoChartCmd)
  54. helmRepoChartCmd.AddCommand(helmRepoChartListCmd)
  55. }
  56. func listHelmRepos(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  57. pID := getProjectID()
  58. hrs, err := client.ListHelmRepos(
  59. context.Background(),
  60. pID,
  61. )
  62. if err != nil {
  63. return err
  64. }
  65. w := new(tabwriter.Writer)
  66. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  67. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "ID", "NAME", "URL", "SERVICE")
  68. currHelmID := getHelmRepoID()
  69. for _, hr := range hrs {
  70. if currHelmID == hr.ID {
  71. color.New(color.FgGreen).Fprintf(w, "%d\t%s\t%s\t%s (current helm repo)\n", hr.ID, hr.Name, hr.RepoURL, hr.Service)
  72. } else {
  73. fmt.Fprintf(w, "%d\t%s\t%s\t%s\n", hr.ID, hr.Name, hr.RepoURL, hr.Service)
  74. }
  75. }
  76. w.Flush()
  77. return nil
  78. }
  79. func listHelmRepoCharts(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  80. pID := getProjectID()
  81. hrID := getHelmRepoID()
  82. charts, err := client.ListCharts(
  83. context.Background(),
  84. pID,
  85. hrID,
  86. )
  87. if err != nil {
  88. return err
  89. }
  90. w := new(tabwriter.Writer)
  91. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  92. fmt.Fprintf(w, "%s\t%s\n", "NAME", "VERSION")
  93. for _, chart := range charts {
  94. fmt.Fprintf(w, "%s\t%s\n", strings.ToLower(chart.Name), chart.Version)
  95. }
  96. w.Flush()
  97. return nil
  98. }