2
0

helm.go 995 B

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