@@ -39,6 +39,7 @@ type runEditorUntilOkOpts struct {
3939 TmpFileName string
4040 OriginalHash []byte
4141 InputStore sops.Store
42+ OutputStore common.Store
4243 ShowMasterKeys bool
4344 Tree * sops.Tree
4445}
@@ -147,8 +148,12 @@ func editTree(opts editOpts, tree *sops.Tree, dataKey []byte) ([]byte, error) {
147148
148149 // Let the user edit the file
149150 err = runEditorUntilOk (runEditorUntilOkOpts {
150- InputStore : opts .InputStore , OriginalHash : origHash , TmpFileName : tmpfileName ,
151- ShowMasterKeys : opts .ShowMasterKeys , Tree : tree })
151+ InputStore : opts .InputStore ,
152+ OutputStore : opts .OutputStore ,
153+ OriginalHash : origHash ,
154+ TmpFileName : tmpfileName ,
155+ ShowMasterKeys : opts .ShowMasterKeys ,
156+ Tree : tree })
152157 if err != nil {
153158 return nil , err
154159 }
@@ -169,6 +174,12 @@ func editTree(opts editOpts, tree *sops.Tree, dataKey []byte) ([]byte, error) {
169174 return encryptedFile , nil
170175}
171176
177+ const pressKeyMsg = "Press enter to return to the editor, or Ctrl+C to exit."
178+
179+ func waitForKeyPress () {
180+ bufio .NewReader (os .Stdin ).ReadByte ()
181+ }
182+
172183func runEditorUntilOk (opts runEditorUntilOkOpts ) error {
173184 for {
174185 err := runEditor (opts .TmpFileName )
@@ -191,10 +202,8 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
191202 log .WithField (
192203 "error" ,
193204 err ,
194- ).Errorf ("Could not load tree, probably due to invalid " +
195- "syntax. Press a key to return to the editor, or Ctrl+C to " +
196- "exit." )
197- bufio .NewReader (os .Stdin ).ReadByte ()
205+ ).Errorf ("Could not load tree, probably due to invalid syntax. " + pressKeyMsg )
206+ waitForKeyPress ()
198207 continue
199208 }
200209 if opts .ShowMasterKeys {
@@ -205,14 +214,22 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
205214 log .WithField (
206215 "error" ,
207216 err ,
208- ).Errorf ("SOPS metadata is invalid. Press a key to " +
209- "return to the editor, or Ctrl+C to exit." )
210- bufio .NewReader (os .Stdin ).ReadByte ()
217+ ).Errorf ("SOPS metadata is invalid. " + pressKeyMsg )
218+ waitForKeyPress ()
211219 continue
212220 }
213221 // Replace the whole tree, because otherwise newBranches would
214222 // contain the SOPS metadata
215223 opts .Tree = & t
224+ } else {
225+ if userErr , _ := validateFileForEncryption (opts .OutputStore , newBranches ); userErr != nil {
226+ log .WithField (
227+ "error" ,
228+ userErr .UserError (),
229+ ).Errorf ("Tree not valid for encryption. " + pressKeyMsg )
230+ waitForKeyPress ()
231+ continue
232+ }
216233 }
217234 opts .Tree .Branches = newBranches
218235 needVersionUpdated , err := version .AIsNewerThanB (version .Version , opts .Tree .Metadata .Version )
@@ -223,10 +240,8 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
223240 opts .Tree .Metadata .Version = version .Version
224241 }
225242 if opts .Tree .Metadata .MasterKeyCount () == 0 {
226- log .Error ("No master keys were provided, so sops can't " +
227- "encrypt the file. Press a key to return to the editor, or " +
228- "Ctrl+C to exit." )
229- bufio .NewReader (os .Stdin ).ReadByte ()
243+ log .Error ("No master keys were provided, so sops can't encrypt the file. " + pressKeyMsg )
244+ waitForKeyPress ()
230245 continue
231246 }
232247 break
0 commit comments