github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/modules/helm/format.go (about)

     1  package helm
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/gruntwork-io/go-commons/collections"
     8  	"github.com/gruntwork-io/go-commons/errors"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/gruntwork-io/terratest/modules/files"
    12  	"github.com/gruntwork-io/terratest/modules/testing"
    13  )
    14  
    15  // formatSetValuesAsArgs formats the given values as command line args for helm using the given flag (e.g flags of
    16  // the format "--set"/"--set-string" resulting in args like --set/set-string key=value...)
    17  func formatSetValuesAsArgs(setValues map[string]string, flag string) []string {
    18  	args := []string{}
    19  
    20  	// To make it easier to test, go through the keys in sorted order
    21  	keys := collections.Keys(setValues)
    22  	for _, key := range keys {
    23  		value := setValues[key]
    24  		argValue := fmt.Sprintf("%s=%s", key, value)
    25  		args = append(args, flag, argValue)
    26  	}
    27  
    28  	return args
    29  }
    30  
    31  // formatValuesFilesAsArgs formats the given list of values file paths as command line args for helm (e.g of the format
    32  // -f path). This will fail the test if one of the paths do not exist or the absolute path can not be determined.
    33  func formatValuesFilesAsArgs(t testing.TestingT, valuesFiles []string) []string {
    34  	args, err := formatValuesFilesAsArgsE(t, valuesFiles)
    35  	require.NoError(t, err)
    36  	return args
    37  }
    38  
    39  // formatValuesFilesAsArgsE formats the given list of values file paths as command line args for helm (e.g of the format
    40  // -f path). This will error if the file does not exist.
    41  func formatValuesFilesAsArgsE(t testing.TestingT, valuesFiles []string) ([]string, error) {
    42  	args := []string{}
    43  
    44  	for _, valuesFilePath := range valuesFiles {
    45  		// Pass through filepath.Abs to clean the path, and then make sure this file exists
    46  		absValuesFilePath, err := filepath.Abs(valuesFilePath)
    47  		if err != nil {
    48  			return args, errors.WithStackTrace(err)
    49  		}
    50  		if !files.FileExists(absValuesFilePath) {
    51  			return args, errors.WithStackTrace(ValuesFileNotFoundError{valuesFilePath})
    52  		}
    53  		args = append(args, "-f", absValuesFilePath)
    54  	}
    55  
    56  	return args, nil
    57  }
    58  
    59  // formatSetFilesAsArgs formats the given list of keys and file paths as command line args for helm to set from file
    60  // (e.g of the format --set-file key=path). This will fail the test if one of the paths do not exist or the absolute
    61  // path can not be determined.
    62  func formatSetFilesAsArgs(t testing.TestingT, setFiles map[string]string) []string {
    63  	args, err := formatSetFilesAsArgsE(t, setFiles)
    64  	require.NoError(t, err)
    65  	return args
    66  }
    67  
    68  // formatSetFilesAsArgsE formats the given list of keys and file paths as command line args for helm to set from file
    69  // (e.g of the format --set-file key=path)
    70  func formatSetFilesAsArgsE(t testing.TestingT, setFiles map[string]string) ([]string, error) {
    71  	args := []string{}
    72  
    73  	// To make it easier to test, go through the keys in sorted order
    74  	keys := collections.Keys(setFiles)
    75  	for _, key := range keys {
    76  		setFilePath := setFiles[key]
    77  		// Pass through filepath.Abs to clean the path, and then make sure this file exists
    78  		absSetFilePath, err := filepath.Abs(setFilePath)
    79  		if err != nil {
    80  			return args, errors.WithStackTrace(err)
    81  		}
    82  		if !files.FileExists(absSetFilePath) {
    83  			return args, errors.WithStackTrace(SetFileNotFoundError{setFilePath})
    84  		}
    85  		argValue := fmt.Sprintf("%s=%s", key, absSetFilePath)
    86  		args = append(args, "--set-file", argValue)
    87  	}
    88  
    89  	return args, nil
    90  }