@@ -52,15 +52,28 @@ func (err *fileAlreadyEncryptedError) UserError() string {
5252 "encrypt files that already contain such an entry.\n \n " +
5353 "If this is an unencrypted file, rename the '" + stores .SopsMetadataKey + "' entry.\n \n " +
5454 "If this is an encrypted file and you want to edit it, use the " +
55- "editor mode, for example: `sops my_file.yaml`"
55+ "editor mode, for example: `sops edit my_file.yaml`"
5656 return wordwrap .WrapString (message , 75 )
5757}
5858
59- func ensureNoMetadata (opts encryptOpts , branch sops.TreeBranch ) error {
60- if opts .OutputStore .HasSopsTopLevelKey (branch ) {
61- return & fileAlreadyEncryptedError {}
59+ type needAtLeastOneDocument struct {}
60+
61+ func (err * needAtLeastOneDocument ) Error () string {
62+ return "Empty file"
63+ }
64+
65+ func (err * needAtLeastOneDocument ) UserError () string {
66+ return "File cannot be completely empty, it must contain at least one document"
67+ }
68+
69+ func validateFileForEncryption (outputStore sops.Store , branches []sops.TreeBranch ) (sops.UserError , int ) {
70+ if len (branches ) < 1 {
71+ return & needAtLeastOneDocument {}, codes .NeedAtLeastOneDocument
72+ }
73+ if outputStore .HasSopsTopLevelKey (branches [0 ]) {
74+ return & fileAlreadyEncryptedError {}, codes .FileAlreadyEncrypted
6275 }
63- return nil
76+ return nil , 0
6477}
6578
6679func metadataFromEncryptionConfig (config encryptConfig ) sops.Metadata {
@@ -96,11 +109,8 @@ func encrypt(opts encryptOpts) (encryptedFile []byte, err error) {
96109 if err != nil {
97110 return nil , common .NewExitError (fmt .Sprintf ("Error unmarshalling file: %s" , err ), codes .CouldNotReadInputFile )
98111 }
99- if len (branches ) < 1 {
100- return nil , common .NewExitError ("File cannot be completely empty, it must contain at least one document" , codes .NeedAtLeastOneDocument )
101- }
102- if err := ensureNoMetadata (opts , branches [0 ]); err != nil {
103- return nil , common .NewExitError (err , codes .FileAlreadyEncrypted )
112+ if err , code := validateFileForEncryption (opts .OutputStore , branches ); err != nil {
113+ return nil , common .NewExitError (err , code )
104114 }
105115 path , err := filepath .Abs (opts .InputPath )
106116 if err != nil {
0 commit comments