helm.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. DisableFlagParsing: true,
  14. DisableFlagsInUseLine: true,
  15. Run: func(cmd *cobra.Command, args []string) {
  16. err := checkLoginAndRun(args, runHelm)
  17. if err != nil {
  18. os.Exit(1)
  19. }
  20. },
  21. }
  22. func init() {
  23. rootCmd.AddCommand(helmCmd)
  24. }
  25. func runHelm(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  26. _, err := exec.LookPath("helm")
  27. if err != nil {
  28. return fmt.Errorf("error finding helm: %w", err)
  29. }
  30. tmpFile, err := downloadTempKubeconfig(client)
  31. if err != nil {
  32. return err
  33. }
  34. defer func() {
  35. os.Remove(tmpFile)
  36. }()
  37. os.Setenv("KUBECONFIG", tmpFile)
  38. cmd := exec.Command("helm", args...)
  39. cmd.Stdout = os.Stdout
  40. cmd.Stderr = os.Stderr
  41. err = cmd.Run()
  42. if err != nil {
  43. return fmt.Errorf("error running helm: %w", err)
  44. }
  45. return nil
  46. }