Skip to content

Commit 2284108

Browse files
committed
update for feedback, remove unimplemented panics
Simplified the slice copy/update logic. Removed the panics for non required interface functions, the impl was trivial, added simple tests to ensure they work as expected.
1 parent 0a48cc5 commit 2284108

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

provisioner/terraform/otelenv.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,37 @@ func (c *envCarrier) Set(key, value string) {
4343
if strings.HasPrefix(e, key+"=") {
4444
// don't directly update the slice so we don't modify the slice
4545
// passed in
46-
newEnv := slices.Clone(c.Env)
47-
newEnv = append(newEnv[:i], append([]string{fmt.Sprintf("%s=%s", key, value)}, newEnv[i+1:]...)...)
48-
c.Env = newEnv
46+
c.Env = slices.Clone(c.Env)
47+
c.Env[i] = fmt.Sprintf("%s=%s", key, value)
4948
return
5049
}
5150
}
5251
c.Env = append(c.Env, fmt.Sprintf("%s=%s", key, value))
5352
}
5453

55-
func (*envCarrier) Get(_ string) string {
56-
// Get not necessary to inject environment variables
57-
panic("Not implemented")
54+
func (c *envCarrier) Get(key string) string {
55+
if c == nil {
56+
return ""
57+
}
58+
key = toKey(key)
59+
for _, e := range c.Env {
60+
if strings.HasPrefix(e, key+"=") {
61+
return strings.TrimPrefix(e, key+"=")
62+
}
63+
}
64+
return ""
5865
}
5966

60-
func (*envCarrier) Keys() []string {
61-
// Keys not necessary to inject environment variables
62-
panic("Not implemented")
67+
func (c *envCarrier) Keys() []string {
68+
if c == nil {
69+
return nil
70+
}
71+
keys := make([]string, len(c.Env))
72+
for i, e := range c.Env {
73+
k, _, _ := strings.Cut(e, "=")
74+
keys[i] = k
75+
}
76+
return keys
6377
}
6478

6579
// otelEnvInject will add add any necessary environment variables for the span

provisioner/terraform/otelenv_test.go renamed to provisioner/terraform/otelenv_internal_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package terraform // nolint:testpackage
1+
package terraform
22

33
import (
44
"context"
@@ -61,3 +61,25 @@ func TestOtelEnvInject(t *testing.T) {
6161
"TERM=xterm",
6262
}, got)
6363
}
64+
65+
func TestEnvCarrierSet(t *testing.T) {
66+
t.Parallel()
67+
c := &envCarrier{
68+
Env: []string{"PATH=/usr/bin:/bin", "TERM=xterm"},
69+
}
70+
c.Set("PATH", "/usr/local/bin")
71+
c.Set("NEWVAR", "newval")
72+
require.Equal(t, []string{
73+
"PATH=/usr/local/bin",
74+
"TERM=xterm",
75+
"NEWVAR=newval",
76+
}, c.Env)
77+
}
78+
79+
func TestEnvCarrierKeys(t *testing.T) {
80+
t.Parallel()
81+
c := &envCarrier{
82+
Env: []string{"PATH=/usr/bin:/bin", "TERM=xterm"},
83+
}
84+
require.Equal(t, []string{"PATH", "TERM"}, c.Keys())
85+
}

0 commit comments

Comments
 (0)