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