github.com/terraform-modules-krish/terratest@v0.29.0/modules/helm/cmd.go (about)

     1  package helm
     2  
     3  import (
     4  	gruntwork-cli "github.com/terraform-modules-krish/go-commons/errors"
     5  	"github.com/terraform-modules-krish/terratest/modules/shell"
     6  	"github.com/terraform-modules-krish/terratest/modules/testing"
     7  )
     8  
     9  // getCommonArgs extracts common helm options. In this case, these are:
    10  // - kubeconfig path
    11  // - kubeconfig context
    12  // - helm home path
    13  func getCommonArgs(options *Options, args ...string) []string {
    14  	if options.KubectlOptions != nil && options.KubectlOptions.ContextName != "" {
    15  		args = append(args, "--kube-context", options.KubectlOptions.ContextName)
    16  	}
    17  	if options.KubectlOptions != nil && options.KubectlOptions.ConfigPath != "" {
    18  		args = append(args, "--kubeconfig", options.KubectlOptions.ConfigPath)
    19  	}
    20  	if options.HomePath != "" {
    21  		args = append(args, "--home", options.HomePath)
    22  	}
    23  	return args
    24  }
    25  
    26  // getNamespaceArgs returns the args to append for the namespace, if set in the helm Options struct.
    27  func getNamespaceArgs(options *Options) []string {
    28  	if options.KubectlOptions != nil && options.KubectlOptions.Namespace != "" {
    29  		return []string{"--namespace", options.KubectlOptions.Namespace}
    30  	}
    31  	return []string{}
    32  }
    33  
    34  // getValuesArgsE computes the args to pass in for setting values
    35  func getValuesArgsE(t testing.TestingT, options *Options, args ...string) ([]string, error) {
    36  	args = append(args, formatSetValuesAsArgs(options.SetValues, "--set")...)
    37  	args = append(args, formatSetValuesAsArgs(options.SetStrValues, "--set-string")...)
    38  
    39  	valuesFilesArgs, err := formatValuesFilesAsArgsE(t, options.ValuesFiles)
    40  	if err != nil {
    41  		return args, errors.WithStackTrace(err)
    42  	}
    43  	args = append(args, valuesFilesArgs...)
    44  
    45  	setFilesArgs, err := formatSetFilesAsArgsE(t, options.SetFiles)
    46  	if err != nil {
    47  		return args, errors.WithStackTrace(err)
    48  	}
    49  	args = append(args, setFilesArgs...)
    50  	return args, nil
    51  }
    52  
    53  // RunHelmCommandAndGetOutputE runs helm with the given arguments and options and returns stdout/stderr.
    54  func RunHelmCommandAndGetOutputE(t testing.TestingT, options *Options, cmd string, additionalArgs ...string) (string, error) {
    55  	args := []string{cmd}
    56  	args = getCommonArgs(options, args...)
    57  	args = append(args, additionalArgs...)
    58  
    59  	helmCmd := shell.Command{
    60  		Command:    "helm",
    61  		Args:       args,
    62  		WorkingDir: ".",
    63  		Env:        options.EnvVars,
    64  		Logger:     options.Logger,
    65  	}
    66  	return shell.RunCommandAndGetOutputE(t, helmCmd)
    67  }