helm.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package commands
  2. import (
  3. "context"
  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/porter-dev/porter/cli/cmd/config"
  10. "github.com/spf13/cobra"
  11. )
  12. func registerCommand_Helm(cliConf config.CLIConfig) *cobra.Command {
  13. helmCmd := &cobra.Command{
  14. Use: "helm",
  15. Short: "Use helm to interact with a Porter cluster",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runHelm)
  18. if err != nil {
  19. os.Exit(1)
  20. }
  21. },
  22. }
  23. return helmCmd
  24. }
  25. func runHelm(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, 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(ctx, client, cliConf)
  31. if err != nil {
  32. return err
  33. }
  34. defer func() {
  35. os.Remove(tmpFile)
  36. }()
  37. os.Setenv("KUBECONFIG", tmpFile)
  38. execCommand := exec.Command("helm", args...)
  39. execCommand.Stdin = os.Stdin
  40. execCommand.Stdout = os.Stdout
  41. execCommand.Stderr = os.Stderr
  42. err = execCommand.Run()
  43. if err != nil {
  44. return fmt.Errorf("error running helm: %w", err)
  45. }
  46. return nil
  47. }