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

Commit 3ec79c7

Browse files
committed
Update variable reader to only prompt for each variable once
1 parent a101c91 commit 3ec79c7

2 files changed

Lines changed: 55 additions & 32 deletions

File tree

internals/secrethub/variable_reader.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,37 @@ func (v *variableReader) ReadVariable(name string) (string, error) {
5252
}
5353

5454
type promptMissingVariableReader struct {
55-
reader tpl.VariableReader
56-
io ui.IO
55+
reader tpl.VariableReader
56+
io ui.IO
57+
answers map[string]string
5758
}
5859

5960
func newPromptMissingVariableReader(reader tpl.VariableReader, io ui.IO) tpl.VariableReader {
6061
return &promptMissingVariableReader{
61-
reader: reader,
62-
io: io,
62+
reader: reader,
63+
io: io,
64+
answers: map[string]string{},
6365
}
6466
}
6567

6668
// ReadVariable fetches a template variable and prompts the user if it is not found.
6769
func (p *promptMissingVariableReader) ReadVariable(name string) (string, error) {
6870
variable, err := p.reader.ReadVariable(name)
69-
if err == tpl.ErrTemplateVarNotFound(name) {
70-
question := fmt.Sprintf("What is the value of the \"%s\" template variable?\n", name)
71-
answer, err := ui.Ask(p.io, question)
72-
if err != nil {
73-
return "", tpl.ErrTemplateVarNotFound(name)
74-
}
75-
return answer, nil
76-
} else if err != nil {
77-
return "", err
71+
if err != tpl.ErrTemplateVarNotFound(name) {
72+
return variable, err
73+
}
74+
75+
variable, ok := p.answers[name]
76+
if ok {
77+
return variable, nil
78+
}
79+
80+
question := fmt.Sprintf("What is the value of the \"%s\" template variable?\n", name)
81+
variable, err = ui.Ask(p.io, question)
82+
if err != nil {
83+
return "", tpl.ErrTemplateVarNotFound(name)
7884
}
85+
p.answers[name] = variable
7986

8087
return variable, err
8188
}

internals/secrethub/variable_reader_test.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package secrethub
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/secrethub/secrethub-cli/internals/cli/ui"
@@ -120,39 +119,56 @@ func TestPromptVariableReader(t *testing.T) {
120119
assert.OK(t, err)
121120

122121
cases := map[string]struct {
123-
promptIn string
124-
varName string
125-
expected string
126-
err error
122+
promptIn []string
123+
varNames []string
124+
expected []string
125+
err []error
127126
}{
128127
"prompt": {
129-
promptIn: "foobar",
130-
varName: "test5",
131-
expected: "foobar",
128+
promptIn: []string{"foobar\n"},
129+
varNames: []string{"test5"},
130+
expected: []string{"foobar"},
132131
},
133132
"no prompt": {
134-
varName: "test4",
135-
expected: "testD",
133+
varNames: []string{"test4"},
134+
expected: []string{"testD"},
136135
},
137136
"from os env": {
138-
varName: "test2",
139-
expected: "testB",
137+
varNames: []string{"test2"},
138+
expected: []string{"testB"},
140139
},
141140
"template vars shadow os env": {
142-
varName: "test1",
143-
expected: "testAA",
141+
varNames: []string{"test1"},
142+
expected: []string{"testAA"},
143+
},
144+
"only prompt once": {
145+
promptIn: []string{"foobar\n"},
146+
varNames: []string{"test8", "test8"},
147+
expected: []string{"foobar", "foobar"},
148+
},
149+
"prompt for each new variable": {
150+
promptIn: []string{"foobar\n", "foo\n"},
151+
varNames: []string{"test8", "test8", "test2", "test9", "test8", "test2"},
152+
expected: []string{"foobar", "foobar", "testB", "foo", "foobar", "testB"},
144153
},
145154
}
146155

147156
for name, tc := range cases {
148157
t.Run(name, func(t *testing.T) {
149158
io := ui.NewFakeIO()
150-
io.PromptIn.Buffer = bytes.NewBufferString(tc.promptIn)
151-
reader = newPromptMissingVariableReader(reader, io)
159+
io.PromptIn.Reads = tc.promptIn
152160

153-
val, err := reader.ReadVariable(tc.varName)
154-
assert.Equal(t, val, tc.expected)
155-
assert.Equal(t, err, tc.err)
161+
reader := newPromptMissingVariableReader(reader, io)
162+
163+
for i, varName := range tc.varNames {
164+
val, err := reader.ReadVariable(varName)
165+
if tc.err != nil {
166+
assert.Equal(t, err, tc.err[i])
167+
} else {
168+
assert.OK(t, err)
169+
}
170+
assert.Equal(t, val, tc.expected[i])
171+
}
156172
})
157173
}
158174
}

0 commit comments

Comments
 (0)