helm.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package cmd
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/spf13/cobra"
  10. )
  11. var helmCmd = &cobra.Command{
  12. Use: "helm",
  13. Short: "Use helm to interact with a Porter cluster",
  14. Run: func(cmd *cobra.Command, args []string) {
  15. err := checkLoginAndRun(args, runHelm)
  16. if err != nil {
  17. os.Exit(1)
  18. }
  19. },
  20. }
  21. func init() {
  22. rootCmd.AddCommand(helmCmd)
  23. }
  24. func runHelm(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  25. _, err := exec.LookPath("helm")
  26. if err != nil {
  27. return fmt.Errorf("error finding helm: %w", err)
  28. }
  29. tmpFile, err := downloadTempKubeconfig(client)
  30. if err != nil {
  31. return err
  32. }
  33. defer func() {
  34. os.Remove(tmpFile)
  35. }()
  36. os.Setenv("KUBECONFIG", tmpFile)
  37. cmd := exec.Command("helm", args...)
  38. var out bytes.Buffer
  39. var stderr bytes.Buffer
  40. cmd.Stdout = &out
  41. cmd.Stderr = &stderr
  42. err = cmd.Run()
  43. if err != nil {
  44. return fmt.Errorf("error running helm: %s", stderr.String())
  45. }
  46. fmt.Print(out.String())
  47. return nil
  48. }