Skip to content

Commit 3a0a450

Browse files
authored
add Add accessor to function Envs (#3098)
1 parent a3edec1 commit 3a0a450

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pkg/functions/function_envs.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ import (
99

1010
type Envs []Env
1111

12+
// Add appends a new environment variable to the Envs slice with the given name and value.
13+
// If name is an empty string, the Name pointer will be nil, which is used for importing
14+
// all key-value pairs from a secret or configmap.
15+
// This method supports template syntax for values:
16+
// - Plain values: Add("KEY", "value")
17+
// - From local env: Add("KEY", "{{ env:LOCAL_VAR }}")
18+
// - From secret: Add("KEY", "{{ secret:secretName:key }}")
19+
// - From configmap: Add("KEY", "{{ configMap:configMapName:key }}")
20+
// - Import all from secret: Add("", "{{ secret:secretName }}")
21+
// - Import all from configmap: Add("", "{{ configMap:configMapName }}")
22+
func (ee *Envs) Add(name, value string) *Envs {
23+
env := Env{Value: &value}
24+
if name != "" {
25+
env.Name = &name
26+
}
27+
*ee = append(*ee, env)
28+
return ee
29+
}
30+
1231
// String returns Envs as a space-separated set of environment variable
1332
// declarations in the form "KEY=VALUE K2=V2"
1433
func (ee Envs) String() string {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package functions
2+
3+
import "testing"
4+
5+
func TestEnvsAdd(t *testing.T) {
6+
var envs Envs
7+
envs.Add("KEY", "value")
8+
9+
if len(envs) != 1 || *envs[0].Name != "KEY" || *envs[0].Value != "value" {
10+
t.Errorf("Add failed: got %v", envs)
11+
}
12+
}

0 commit comments

Comments
 (0)