Skip to content

Commit a506957

Browse files
committed
Refactor loop.
Signed-off-by: Felix Fontein <felix@fontein.de>
1 parent 75487cd commit a506957

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

cmd/sops/edit.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ func editTree(opts editOpts, tree *sops.Tree, dataKey []byte) ([]byte, error) {
174174
return encryptedFile, nil
175175
}
176176

177+
const pressKeyMsg = "Press a key to return to the editor, or Ctrl+C to exit."
178+
179+
func waitForKeyPress() {
180+
bufio.NewReader(os.Stdin).ReadByte()
181+
}
182+
177183
func runEditorUntilOk(opts runEditorUntilOkOpts) error {
178184
for {
179185
err := runEditor(opts.TmpFileName)
@@ -196,10 +202,8 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
196202
log.WithField(
197203
"error",
198204
err,
199-
).Errorf("Could not load tree, probably due to invalid " +
200-
"syntax. Press a key to return to the editor, or Ctrl+C to " +
201-
"exit.")
202-
bufio.NewReader(os.Stdin).ReadByte()
205+
).Errorf("Could not load tree, probably due to invalid syntax. " + pressKeyMsg)
206+
waitForKeyPress()
203207
continue
204208
}
205209
if opts.ShowMasterKeys {
@@ -210,9 +214,8 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
210214
log.WithField(
211215
"error",
212216
err,
213-
).Errorf("SOPS metadata is invalid. Press a key to " +
214-
"return to the editor, or Ctrl+C to exit.")
215-
bufio.NewReader(os.Stdin).ReadByte()
217+
).Errorf("SOPS metadata is invalid. " + pressKeyMsg)
218+
waitForKeyPress()
216219
continue
217220
}
218221
// Replace the whole tree, because otherwise newBranches would
@@ -223,8 +226,8 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
223226
log.WithField(
224227
"error",
225228
userErr.UserError(),
226-
).Errorf("Tree not valid for encryption. Press a key to return to the editor, or Ctrl+C to exit.")
227-
bufio.NewReader(os.Stdin).ReadByte()
229+
).Errorf("Tree not valid for encryption. " + pressKeyMsg)
230+
waitForKeyPress()
228231
continue
229232
}
230233
}
@@ -237,10 +240,8 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
237240
opts.Tree.Metadata.Version = version.Version
238241
}
239242
if opts.Tree.Metadata.MasterKeyCount() == 0 {
240-
log.Error("No master keys were provided, so sops can't " +
241-
"encrypt the file. Press a key to return to the editor, or " +
242-
"Ctrl+C to exit.")
243-
bufio.NewReader(os.Stdin).ReadByte()
243+
log.Error("No master keys were provided, so sops can't encrypt the file. " + pressKeyMsg)
244+
waitForKeyPress()
244245
continue
245246
}
246247
break

0 commit comments

Comments
 (0)