helm_repo.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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().AddFlagSet(helmRepoFlagSet)
  47. helmRepoCmd.AddCommand(helmRepoListCmd)
  48. helmRepoCmd.AddCommand(helmRepoChartCmd)
  49. helmRepoChartCmd.AddCommand(helmRepoChartListCmd)
  50. }
  51. func listHelmRepos(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  52. pID := config.Project
  53. hrs, err := client.ListHelmRepos(
  54. context.Background(),
  55. pID,
  56. )
  57. if err != nil {
  58. return err
  59. }
  60. w := new(tabwriter.Writer)
  61. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  62. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "ID", "NAME", "URL", "SERVICE")
  63. currHelmID := config.HelmRepo
  64. for _, hr := range hrs {
  65. if currHelmID == hr.ID {
  66. 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)
  67. } else {
  68. fmt.Fprintf(w, "%d\t%s\t%s\t%s\n", hr.ID, hr.Name, hr.RepoURL, hr.Service)
  69. }
  70. }
  71. w.Flush()
  72. return nil
  73. }
  74. func listHelmRepoCharts(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  75. pID := config.Project
  76. hrID := config.HelmRepo
  77. charts, err := client.ListCharts(
  78. context.Background(),
  79. pID,
  80. hrID,
  81. )
  82. if err != nil {
  83. return err
  84. }
  85. w := new(tabwriter.Writer)
  86. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  87. fmt.Fprintf(w, "%s\t%s\n", "NAME", "VERSION")
  88. for _, chart := range charts {
  89. for _, version := range chart.Versions {
  90. fmt.Fprintf(w, "%s\t%s\n", strings.ToLower(chart.Name), version)
  91. }
  92. }
  93. w.Flush()
  94. return nil
  95. }