Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 708dc47

Browse files
committed
Move os environment to environment struct
1 parent 9d35b31 commit 708dc47

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

internals/secrethub/env_source.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
type environment struct {
2626
io ui.IO
27-
osEnv []string
27+
osEnv func() []string
2828
readFile func(filename string) ([]byte, error)
2929
osStat func(filename string) (os.FileInfo, error)
3030
envar map[string]string
@@ -38,7 +38,7 @@ type environment struct {
3838
func newEnvironment(io ui.IO) *environment {
3939
return &environment{
4040
io: io,
41-
osEnv: os.Environ(),
41+
osEnv: os.Environ,
4242
readFile: ioutil.ReadFile,
4343
osStat: os.Stat,
4444
templateVars: make(map[string]string),
@@ -57,9 +57,13 @@ func (env *environment) register(clause *cli.CommandClause) {
5757
}
5858

5959
func (env *environment) env() (map[string]value, error) {
60-
osEnv, _ := parseKeyValueStringsToMap(env.osEnv)
60+
osEnvMap, _ := parseKeyValueStringsToMap(env.osEnv())
6161
var sources []EnvSource
6262

63+
sources = append(sources, &osEnv{
64+
osEnv: osEnvMap,
65+
})
66+
6367
// .secretsenv dir (for backwards compatibility)
6468
envDir := filepath.Join(secretspec.SecretEnvPath, env.secretsEnvDir)
6569
_, err := os.Stat(envDir)
@@ -82,7 +86,7 @@ func (env *environment) env() (map[string]value, error) {
8286
}
8387

8488
if env.envFile != "" {
85-
templateVariableReader, err := newVariableReader(osEnv, env.templateVars)
89+
templateVariableReader, err := newVariableReader(osEnvMap, env.templateVars)
8690
if err != nil {
8791
return nil, err
8892
}
@@ -109,7 +113,7 @@ func (env *environment) env() (map[string]value, error) {
109113
}
110114

111115
// secret references (secrethub://)
112-
referenceEnv := newReferenceEnv(osEnv)
116+
referenceEnv := newReferenceEnv(osEnvMap)
113117
sources = append(sources, referenceEnv)
114118

115119
// --envar flag

internals/secrethub/run.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,9 @@ func (cmd *RunCommand) Run() error {
182182
// sourceEnvironment returns the environment of the subcommand, with all the secrets sourced
183183
// and the secret values that need to be masked.
184184
func (cmd *RunCommand) sourceEnvironment() ([]string, []string, error) {
185-
osEnv, passthroughEnv := parseKeyValueStringsToMap(cmd.osEnv)
185+
_, passthroughEnv := parseKeyValueStringsToMap(cmd.osEnv)
186186

187-
// add os envs
188187
newEnv := map[string]string{}
189-
for name, value := range osEnv {
190-
newEnv[name] = value
191-
}
192188

193189
envValues, err := cmd.environment.env()
194190
if err != nil {

internals/secrethub/run_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,9 @@ func TestRunCommand_Run(t *testing.T) {
589589
command: []string{"echo", "test"},
590590
io: ui.NewFakeIO(),
591591
environment: &environment{
592-
osEnv: []string{"TEST=secrethub://nonexistent/secret/path"},
592+
osEnv: func() []string {
593+
return []string{"TEST=secrethub://nonexistent/secret/path"}
594+
},
593595
osStat: osStatNotExist,
594596
},
595597
newClient: func() (secrethub.ClientInterface, error) {
@@ -612,7 +614,9 @@ func TestRunCommand_Run(t *testing.T) {
612614
command: []string{"echo", "test"},
613615
io: ui.NewFakeIO(),
614616
environment: &environment{
615-
osEnv: []string{"TEST=secrethub://nonexistent/secret/path"},
617+
osEnv: func() []string {
618+
return []string{"TEST=secrethub://nonexistent/secret/path"}
619+
},
616620
osStat: osStatNotExist,
617621
},
618622
newClient: func() (secrethub.ClientInterface, error) {
@@ -772,7 +776,9 @@ func TestRunCommand_environment(t *testing.T) {
772776
readFile: readFileFunc("secrethub.env", "TEST=aaa"),
773777
dontPromptMissingTemplateVar: true,
774778
templateVersion: "2",
775-
osEnv: []string{"TEST=secrethub://test/test/test"},
779+
osEnv: func() []string {
780+
return []string{"TEST=secrethub://test/test/test"}
781+
},
776782
},
777783
newClient: func() (secrethub.ClientInterface, error) {
778784
return fakeclient.Client{
@@ -885,7 +891,9 @@ func TestRunCommand_environment(t *testing.T) {
885891
readFile: readFileFunc("secrethub.env", "TEST = {{ test/$variable/test }}"),
886892
dontPromptMissingTemplateVar: true,
887893
templateVersion: "2",
888-
osEnv: []string{"SECRETHUB_VAR_VARIABLE=test"},
894+
osEnv: func() []string {
895+
return []string{"SECRETHUB_VAR_VARIABLE=test"}
896+
},
889897
},
890898
newClient: func() (secrethub.ClientInterface, error) {
891899
return fakeclient.Client{

0 commit comments

Comments
 (0)