-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use viper to read create template
params from a config
#2235
Conversation
create template
params from a config
- Read configFile into a separate var, it can't be inside the config - Unmarshal struct has to have public properties apparently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ and with tests! ✨
- NAMESPACE=test-namespace | ||
- GIT_REPO_NAMESPACE=test-git-repo-namespace | ||
- GIT_REPO_NAME=test-git-repo-name | ||
- PATH=../clusters/out.yaml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spf13/viper#411 :(, Had a quick play w/ StringToString but we end up with case-insensitive keys here which is a shame. This might be the best we can do without writing lots of parsing stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we could extract these parameters from the template file and compare its lower case version to the received parameter and swap the keys, and if it's actually necessary 😅
e.g. diff --git a/cmd/gitops/app/create/templates/cmd.go b/cmd/gitops/app/create/templates/cmd.go
index 5b78dba1..2c32f796 100644
--- a/cmd/gitops/app/create/templates/cmd.go
+++ b/cmd/gitops/app/create/templates/cmd.go
@@ -22,10 +22,10 @@ import (
)
type Config struct {
- ParameterValues []string `mapstructure:"values"`
- Export bool `mapstructure:"export"`
- OutputDir string `mapstructure:"output-dir"`
- TemplateFile string `mapstructure:"template-file"`
+ ParameterValues map[string]string `mapstructure:"values"`
+ Export bool `mapstructure:"export"`
+ OutputDir string `mapstructure:"output-dir"`
+ TemplateFile string `mapstructure:"template-file"`
}
var config Config
@@ -49,7 +49,7 @@ var CreateCommand = &cobra.Command{
func init() {
flags := CreateCommand.Flags()
- flags.StringSlice("values", []string{}, "Set parameter values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
+ flags.StringToString("values", map[string]string{}, "Set parameter values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
flags.Bool("export", false, "export in YAML format to stdout")
flags.String("output-dir", "", "write YAML format to file")
flags.String("template-file", "", "template file to use")
@@ -102,18 +102,9 @@ func templatesCmdRunE() func(*cobra.Command, []string) error {
}
ctx := context.Background()
- vals := make(map[string]string)
-
- // parse parameter values
- for _, v := range config.ParameterValues {
- kv := strings.SplitN(v, "=", 2)
- if len(kv) == 2 {
- vals[kv[0]] = kv[1]
- }
- }
getFilesRequest := server.GetFilesRequest{
- ParameterValues: vals,
+ ParameterValues: config.ParameterValues,
TemplateName: parsedTemplate.Name,
TemplateKind: parsedTemplate.Kind,
}
diff --git a/cmd/gitops/app/create/templates/cmd_test.go b/cmd/gitops/app/create/templates/cmd_test.go
index e95e4cf1..6a3294f9 100644
--- a/cmd/gitops/app/create/templates/cmd_test.go
+++ b/cmd/gitops/app/create/templates/cmd_test.go
@@ -106,9 +106,14 @@ func Test_initializeConfig(t *testing.T) {
assert.Equal(t, config.TemplateFile, "template.yaml")
- expectedParams := []string{
- "CLUSTER_NAME=test-cluster", "RESOURCE_NAME=test-resource", "NAMESPACE=test-namespace",
- "GIT_REPO_NAMESPACE=test-git-repo-namespace", "GIT_REPO_NAME=test-git-repo-name", "PATH=../clusters/out.yaml"}
+ expectedParams := map[string]string{
+ "CLUSTER_NAME": "test-cluster",
+ "RESOURCE_NAME": "test-resource",
+ "NAMESPACE": "test-namespace",
+ "GIT_REPO_NAMESPACE": "test-git-repo-namespace",
+ "GIT_REPO_NAME": "test-git-repo-name",
+ "PATH": "../clusters/out.yaml",
+ }
if diff := cmp.Diff(expectedParams, config.ParameterValues); diff != "" {
t.Fatalf("result didn't match expected:\n%s", diff)
diff --git a/cmd/gitops/app/create/templates/testdata/config.yaml b/cmd/gitops/app/create/templates/testdata/config.yaml
index ef47dd7e..a4b9ee85 100644
--- a/cmd/gitops/app/create/templates/testdata/config.yaml
+++ b/cmd/gitops/app/create/templates/testdata/config.yaml
@@ -1,8 +1,8 @@
template-file: template.yaml
values:
- - CLUSTER_NAME=test-cluster
- - RESOURCE_NAME=test-resource
- - NAMESPACE=test-namespace
- - GIT_REPO_NAMESPACE=test-git-repo-namespace
- - GIT_REPO_NAME=test-git-repo-name
- - PATH=../clusters/out.yaml
\ No newline at end of file
+ CLUSTER_NAME: test-cluster
+ RESOURCE_NAME: test-resource
+ NAMESPACE: test-namespace
+ GIT_REPO_NAMESPACE: test-git-repo-namespace
+ GIT_REPO_NAME: test-git-repo-name
+ PATH: ../clusters/out.yaml Fails with:
|
So still LGTM! 🎉 |
Closes #2227
What changed?
Added viper to read
create template
params from a config file instead of setting them manually.Why was this change made?
The command requires writing many parameters when running it.
How was this change implemented?
How did you validate the change?
Manually + unit tests.
Release notes
Documentation Changes