Skip to content
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

fix: Turns globals.jx-* template variables into valid variable names #6682

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions pkg/cmd/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,24 +1182,24 @@ func (o *PromoteOptions) SearchForChart(filter string) (string, error) {
}

func (o *PromoteOptions) GetEnvChartValues(targetNS string, env *v1.Environment) ([]string, []string) {
kind := strings.ToLower(string(env.Spec.Kind))
kind := string(env.Spec.Kind)
values := []string{
fmt.Sprintf("tags.jx-ns-%s=true", targetNS),
fmt.Sprintf("global.jx-ns-%s=true", targetNS),
fmt.Sprintf("tags.jx-%s=true", kind),
fmt.Sprintf("global.jxNs%s=true", util.ToCamelCase(targetNS)),
fmt.Sprintf("tags.jx-%s=true", strings.ToLower(kind)),
fmt.Sprintf("tags.jx-env-%s=true", env.ObjectMeta.Name),
fmt.Sprintf("global.jx-%s=true", kind),
fmt.Sprintf("global.jx-env-%s=true", env.ObjectMeta.Name),
fmt.Sprintf("global.jx%s=true", kind),
fmt.Sprintf("global.jxEnv%s=true", util.ToCamelCase(env.ObjectMeta.Name)),
}
valueString := []string{
fmt.Sprintf("global.jx-ns=%s", targetNS),
fmt.Sprintf("global.jx-type-env=%s", kind),
fmt.Sprintf("global.jx-env=%s", env.ObjectMeta.Name),
fmt.Sprintf("global.jxNs=%s", targetNS),
fmt.Sprintf("global.jxTypeEnv=%s", strings.ToLower(kind)),
fmt.Sprintf("global.jxEnv=%s", env.ObjectMeta.Name),
}
if env.Spec.Kind == v1.EnvironmentKindTypePreview {
valueString = append(valueString,
fmt.Sprintf("global.jx-preview-app=%s", env.Spec.PreviewGitSpec.ApplicationName),
fmt.Sprintf("global.jx-preview-pr=%s", env.Spec.PreviewGitSpec.Name),
fmt.Sprintf("global.jxPreviewApp=%s", env.Spec.PreviewGitSpec.ApplicationName),
fmt.Sprintf("global.jxPreviewPr=%s", env.Spec.PreviewGitSpec.Name),
)
}
return values, valueString
Expand Down
40 changes: 20 additions & 20 deletions pkg/cmd/promote/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,16 @@ func TestGetEnvChartValues(t *testing.T) {
"tags.jx-preview=true",
"tags.jx-env-test-preview=true",
"tags.jx-ns-jx-test-preview-pr-6=true",
"global.jx-preview=true",
"global.jx-env-test-preview=true",
"global.jx-ns-jx-test-preview-pr-6=true",
"global.jxPreview=true",
"global.jxEnvTestPreview=true",
"global.jxNsJxTestPreviewPr6=true",
},
[]string{
"global.jx-type-env=preview",
"global.jx-env=test-preview",
"global.jx-ns=jx-test-preview-pr-6",
"global.jx-preview-app=my-app",
"global.jx-preview-pr=6",
"global.jxTypeEnv=preview",
"global.jxEnv=test-preview",
"global.jxNs=jx-test-preview-pr-6",
"global.jxPreviewApp=my-app",
"global.jxPreviewPr=6",
},
}, {
"jx-custom-env",
Expand All @@ -422,14 +422,14 @@ func TestGetEnvChartValues(t *testing.T) {
"tags.jx-permanent=true",
"tags.jx-env-custom-env=true",
"tags.jx-ns-jx-custom-env=true",
"global.jx-permanent=true",
"global.jx-env-custom-env=true",
"global.jx-ns-jx-custom-env=true",
"global.jxPermanent=true",
"global.jxEnvCustomEnv=true",
"global.jxNsJxCustomEnv=true",
},
[]string{
"global.jx-type-env=permanent",
"global.jx-env=custom-env",
"global.jx-ns=jx-custom-env",
"global.jxTypeEnv=permanent",
"global.jxEnv=custom-env",
"global.jxNs=jx-custom-env",
},
}, {
"ns-rand",
Expand Down Expand Up @@ -459,14 +459,14 @@ func TestGetEnvChartValues(t *testing.T) {
"tags.jx-edit=true",
"tags.jx-env-random-env=true",
"tags.jx-ns-ns-rand=true",
"global.jx-edit=true",
"global.jx-env-random-env=true",
"global.jx-ns-ns-rand=true",
"global.jxEdit=true",
"global.jxEnvRandomEnv=true",
"global.jxNsNsRand=true",
},
[]string{
"global.jx-type-env=edit",
"global.jx-env=random-env",
"global.jx-ns=ns-rand",
"global.jxTypeEnv=edit",
"global.jxEnv=random-env",
"global.jxNs=ns-rand",
},
}}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/step/helm/step_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ func (o *StepHelmOptions) overwriteProviderValues(requirements *config.Requireme
func (o *StepHelmOptions) getChartValues(targetNS string) ([]string, []string) {
return []string{
fmt.Sprintf("tags.jx-ns-%s=true", targetNS),
fmt.Sprintf("global.jx-ns-%s=true", targetNS),
fmt.Sprintf("global.jxNs%s=true", util.ToCamelCase(targetNS)),
}, []string{
fmt.Sprintf("global.jx-ns=%s", targetNS),
fmt.Sprintf("global.jxNs=%s", targetNS),
}
}
6 changes: 6 additions & 0 deletions pkg/util/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,9 @@ func StripTrailingSlash(url string) string {
func StartsWith(s, substr string) bool {
return strings.Index(s, substr) == 0
}

// ToCamelCase turn "my-super-name" into "MySuperName"
// Usefule for creating valid go-template variable names
func ToCamelCase(s string) string {
return strings.Replace(strings.Title(s), "-", "", -1)
}
4 changes: 4 additions & 0 deletions pkg/util/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ func Test_StartsWith(t *testing.T) {
assert.True(t, util.StartsWith("ML-a-machine-learning-quickstart", "ML-"))
assert.False(t, util.StartsWith("not-a-machine-learning-quickstart", "ML-"))
}

func Test_ToCamelCase(t *testing.T) {
assert.Equal(t, util.ToCamelCase("my-super-name"), "MySuperName")
}