Skip to content

Commit fd7e1b5

Browse files
authored
Merge pull request #1399 from merico-dev/feat-return-err-if-env-not-exist
feat: raise err when config env is not exist
2 parents ff51cfc + f4e28fc commit fd7e1b5

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.18
44

55
require (
66
github.com/Masterminds/semver v1.5.0
7-
github.com/Masterminds/sprig/v3 v3.2.2
87
github.com/adlio/trello v1.9.0
98
github.com/argoproj/argo-cd/v2 v2.2.2
109
github.com/argoproj/gitops-engine v0.5.2
@@ -56,6 +55,7 @@ require (
5655
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
5756
github.com/Masterminds/goutils v1.1.1 // indirect
5857
github.com/Masterminds/semver/v3 v3.1.1 // indirect
58+
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
5959
github.com/Masterminds/squirrel v1.5.2 // indirect
6060
github.com/Microsoft/go-winio v0.4.17 // indirect
6161
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect

pkg/util/template/client.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package template
33
import (
44
"bytes"
55
"fmt"
6+
"os"
67
"text/template"
78

8-
"github.com/Masterminds/sprig/v3"
9-
109
"github.com/devstream-io/devstream/pkg/util/log"
1110
)
1211

@@ -82,10 +81,22 @@ func (c *renderClient) newTemplateClient() *template.Template {
8281
if !c.Option.IgnoreMissKeyError {
8382
t = t.Option("missingkey=error")
8483
}
85-
// use sprig functions such as "env"
86-
t.Funcs(sprig.TxtFuncMap())
84+
t.Funcs(defaultFuncMap)
8785
if c.Option.FuncMap != nil {
8886
t.Funcs(c.Option.FuncMap)
8987
}
9088
return t
9189
}
90+
91+
// defaultFuncMap is the default logic for templatge render func
92+
var defaultFuncMap = map[string]any{
93+
"env": getEnvInTemplate,
94+
}
95+
96+
func getEnvInTemplate(envKey string) (string, error) {
97+
envVal := os.Getenv(envKey)
98+
if envVal == "" {
99+
return "", fmt.Errorf("template can't get environment variable %s, maybe you should set this environment first", envKey)
100+
}
101+
return envVal, nil
102+
}

pkg/util/template/client_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package template
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67
"text/template"
78

@@ -106,5 +107,48 @@ var _ = Describe("renderClient", func() {
106107
Expect(err.Error()).Should(ContainSubstring("render template: default_template"))
107108
})
108109
})
110+
})
109111

112+
var _ = Describe("template default funcs", func() {
113+
Context("getEnvInTemplate func", func() {
114+
var (
115+
tokenKey string
116+
existVal string
117+
)
118+
BeforeEach(func() {
119+
tokenKey = "TEMPLATE_ENV_TEST"
120+
existVal = os.Getenv(tokenKey)
121+
if existVal != "" {
122+
err := os.Unsetenv(tokenKey)
123+
Expect(err).ShouldNot(HaveOccurred())
124+
}
125+
})
126+
When("env not exist", func() {
127+
BeforeEach(func() {
128+
err := os.Unsetenv(tokenKey)
129+
Expect(err).ShouldNot(HaveOccurred())
130+
})
131+
It("should return err", func() {
132+
_, err := getEnvInTemplate(tokenKey)
133+
Expect(err).Should(HaveOccurred())
134+
Expect(err.Error()).Should(Equal("template can't get environment variable TEMPLATE_ENV_TEST, maybe you should set this environment first"))
135+
})
136+
})
137+
When("env exist", func() {
138+
BeforeEach(func() {
139+
err := os.Setenv(tokenKey, "test")
140+
Expect(err).ShouldNot(HaveOccurred())
141+
})
142+
It("should return err", func() {
143+
data, err := getEnvInTemplate(tokenKey)
144+
Expect(err).ShouldNot(HaveOccurred())
145+
Expect(data).Should(Equal("test"))
146+
})
147+
})
148+
AfterEach(func() {
149+
if existVal != "" {
150+
os.Setenv(tokenKey, existVal)
151+
}
152+
})
153+
})
110154
})

0 commit comments

Comments
 (0)