github.com/mponton/terratest@v0.44.0/modules/helm/cmd.go (about)

     1  package helm
     2  
     3  import (
     4  	"github.com/gruntwork-io/go-commons/errors"
     5  	"github.com/mponton/terratest/modules/shell"
     6  	"github.com/mponton/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  	args = append(args, formatSetValuesAsArgs(options.SetJsonValues, "--set-json")...)
    39  
    40  	valuesFilesArgs, err := formatValuesFilesAsArgsE(t, options.ValuesFiles)
    41  	if err != nil {
    42  		return args, errors.WithStackTrace(err)
    43  	}
    44  	args = append(args, valuesFilesArgs...)
    45  
    46  	setFilesArgs, err := formatSetFilesAsArgsE(t, options.SetFiles)
    47  	if err != nil {
    48  		return args, errors.WithStackTrace(err)
    49  	}
    50  	args = append(args, setFilesArgs...)
    51  	return args, nil
    52  }
    53  
    54  // RunHelmCommandAndGetOutputE runs helm with the given arguments and options and returns combined, interleaved stdout/stderr.
    55  func RunHelmCommandAndGetOutputE(t testing.TestingT, options *Options, cmd string, additionalArgs ...string) (string, error) {
    56  	helmCmd := prepareHelmCommand(t, options, cmd, additionalArgs...)
    57  	return shell.RunCommandAndGetOutputE(t, helmCmd)
    58  }
    59  
    60  // RunHelmCommandAndGetStdOutE runs helm with the given arguments and options and returns stdout.
    61  func RunHelmCommandAndGetStdOutE(t testing.TestingT, options *Options, cmd string, additionalArgs ...string) (string, error) {
    62  	helmCmd := prepareHelmCommand(t, options, cmd, additionalArgs...)
    63  	return shell.RunCommandAndGetStdOutE(t, helmCmd)
    64  }
    65  
    66  func prepareHelmCommand(t testing.TestingT, options *Options, cmd string, additionalArgs ...string) shell.Command {
    67  	args := []string{cmd}
    68  	args = getCommonArgs(options, args...)
    69  	args = append(args, getNamespaceArgs(options)...)
    70  	args = append(args, additionalArgs...)
    71  
    72  	helmCmd := shell.Command{
    73  		Command:    "helm",
    74  		Args:       args,
    75  		WorkingDir: ".",
    76  		Env:        options.EnvVars,
    77  		Logger:     options.Logger,
    78  	}
    79  	return helmCmd
    80  }