Skip to content

Commit aa8674c

Browse files
committed
chore: NewExitError() -> Exit()
to match the underlying cli.Exit() function it calls. Signed-off-by: Hoang Nguyen <[email protected]>
1 parent f90d5ad commit aa8674c

File tree

10 files changed

+98
-98
lines changed

10 files changed

+98
-98
lines changed

cmd/sops/common/common.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,23 @@ type DecryptTreeOpts struct {
8585
func DecryptTree(opts DecryptTreeOpts) (dataKey []byte, err error) {
8686
dataKey, err = opts.Tree.Metadata.GetDataKeyWithKeyServices(opts.KeyServices, opts.DecryptionOrder)
8787
if err != nil {
88-
return nil, NewExitError(err, codes.CouldNotRetrieveKey)
88+
return nil, Exit(err, codes.CouldNotRetrieveKey)
8989
}
9090
computedMac, err := opts.Tree.Decrypt(dataKey, opts.Cipher)
9191
if err != nil {
92-
return nil, NewExitError(fmt.Sprintf("Error decrypting tree: %s", err), codes.ErrorDecryptingTree)
92+
return nil, Exit(fmt.Sprintf("Error decrypting tree: %s", err), codes.ErrorDecryptingTree)
9393
}
9494
fileMac, err := opts.Cipher.Decrypt(opts.Tree.Metadata.MessageAuthenticationCode, dataKey, opts.Tree.Metadata.LastModified.Format(time.RFC3339))
9595
if !opts.IgnoreMac {
9696
if err != nil {
97-
return nil, NewExitError(fmt.Sprintf("Cannot decrypt MAC: %s", err), codes.MacMismatch)
97+
return nil, Exit(fmt.Sprintf("Cannot decrypt MAC: %s", err), codes.MacMismatch)
9898
}
9999
if fileMac != computedMac {
100100
// If the file has an empty MAC, display "no MAC" instead of not displaying anything
101101
if fileMac == "" {
102102
fileMac = "no MAC"
103103
}
104-
return nil, NewExitError(fmt.Sprintf("MAC mismatch. File has %s, computed %s", fileMac, computedMac), codes.MacMismatch)
104+
return nil, Exit(fmt.Sprintf("MAC mismatch. File has %s, computed %s", fileMac, computedMac), codes.MacMismatch)
105105
}
106106
}
107107
return dataKey, nil
@@ -121,12 +121,12 @@ type EncryptTreeOpts struct {
121121
func EncryptTree(opts EncryptTreeOpts) error {
122122
unencryptedMac, err := opts.Tree.Encrypt(opts.DataKey, opts.Cipher)
123123
if err != nil {
124-
return NewExitError(fmt.Sprintf("Error encrypting tree: %s", err), codes.ErrorEncryptingTree)
124+
return Exit(fmt.Sprintf("Error encrypting tree: %s", err), codes.ErrorEncryptingTree)
125125
}
126126
opts.Tree.Metadata.LastModified = time.Now().UTC()
127127
opts.Tree.Metadata.MessageAuthenticationCode, err = opts.Cipher.Encrypt(unencryptedMac, opts.DataKey, opts.Tree.Metadata.LastModified.Format(time.RFC3339))
128128
if err != nil {
129-
return NewExitError(fmt.Sprintf("Could not encrypt MAC: %s", err), codes.ErrorEncryptingMac)
129+
return Exit(fmt.Sprintf("Could not encrypt MAC: %s", err), codes.ErrorEncryptingMac)
130130
}
131131
return nil
132132
}
@@ -138,12 +138,12 @@ func LoadEncryptedFileEx(loader sops.EncryptedFileLoader, inputPath string, read
138138
if readFromStdin {
139139
fileBytes, err = io.ReadAll(os.Stdin)
140140
if err != nil {
141-
return nil, NewExitError(fmt.Sprintf("Error reading from stdin: %s", err), codes.CouldNotReadInputFile)
141+
return nil, Exit(fmt.Sprintf("Error reading from stdin: %s", err), codes.CouldNotReadInputFile)
142142
}
143143
} else {
144144
fileBytes, err = os.ReadFile(inputPath)
145145
if err != nil {
146-
return nil, NewExitError(fmt.Sprintf("Error reading file: %s", err), codes.CouldNotReadInputFile)
146+
return nil, Exit(fmt.Sprintf("Error reading file: %s", err), codes.CouldNotReadInputFile)
147147
}
148148
}
149149
path, err := filepath.Abs(inputPath)
@@ -160,11 +160,11 @@ func LoadEncryptedFile(loader sops.EncryptedFileLoader, inputPath string) (*sops
160160
return LoadEncryptedFileEx(loader, inputPath, false)
161161
}
162162

163-
// NewExitError returns a cli.ExitCoder given an error (wrapped in a generic interface{})
163+
// Exit returns a cli.ExitCoder given an error (wrapped in a generic interface{})
164164
// and an exit code to represent the failure
165-
func NewExitError(i interface{}, exitCode int) cli.ExitCoder {
165+
func Exit(i interface{}, exitCode int) cli.ExitCoder {
166166
if userErr, ok := i.(sops.UserError); ok {
167-
return NewExitError(userErr.UserError(), exitCode)
167+
return Exit(userErr.UserError(), exitCode)
168168
}
169169
return cli.Exit(i, exitCode)
170170
}
@@ -312,7 +312,7 @@ func FixAWSKMSEncryptionContextBug(opts GenericDecryptOpts, tree *sops.Tree) (*s
312312
}
313313

314314
if dataKey == nil {
315-
return nil, NewExitError(fmt.Sprintf("Failed to decrypt, meaning there is likely another problem from the encryption context bug: %s", err), codes.ErrorDecryptingTree)
315+
return nil, Exit(fmt.Sprintf("Failed to decrypt, meaning there is likely another problem from the encryption context bug: %s", err), codes.ErrorDecryptingTree)
316316
}
317317

318318
errs := tree.Metadata.UpdateMasterKeysWithKeyServices(dataKey, opts.KeyServices)
@@ -337,12 +337,12 @@ func FixAWSKMSEncryptionContextBug(opts GenericDecryptOpts, tree *sops.Tree) (*s
337337

338338
encryptedFile, err := opts.InputStore.EmitEncryptedFile(*tree)
339339
if err != nil {
340-
return nil, NewExitError(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
340+
return nil, Exit(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
341341
}
342342

343343
file, err := os.Create(opts.InputPath)
344344
if err != nil {
345-
return nil, NewExitError(fmt.Sprintf("Could not open file for writing: %s", err), codes.CouldNotWriteOutputFile)
345+
return nil, Exit(fmt.Sprintf("Could not open file for writing: %s", err), codes.CouldNotWriteOutputFile)
346346
}
347347
defer file.Close()
348348
_, err = file.Write(encryptedFile)

cmd/sops/decrypt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func decrypt(opts decryptOpts) (decryptedFile []byte, err error) {
6767
err = fmt.Errorf("%s\n\n%s", err.Error(), notBinaryHint)
6868
}
6969
if err != nil {
70-
return nil, common.NewExitError(fmt.Sprintf("Error dumping file: %s", err), codes.ErrorDumpingTree)
70+
return nil, common.Exit(fmt.Sprintf("Error dumping file: %s", err), codes.ErrorDumpingTree)
7171
}
7272
return decryptedFile, err
7373
}
@@ -84,15 +84,15 @@ func extract(tree *sops.Tree, path []interface{}, outputStore sops.Store) (outpu
8484
err = fmt.Errorf("%s\n\n%s", err.Error(), notBinaryHint)
8585
}
8686
if err != nil {
87-
return nil, common.NewExitError(fmt.Sprintf("Error dumping file: %s", err), codes.ErrorDumpingTree)
87+
return nil, common.Exit(fmt.Sprintf("Error dumping file: %s", err), codes.ErrorDumpingTree)
8888
}
8989
return decrypted, err
9090
} else if str, ok := v.(string); ok {
9191
return []byte(str), nil
9292
}
9393
bytes, err := outputStore.EmitValue(v)
9494
if err != nil {
95-
return nil, common.NewExitError(fmt.Sprintf("Error dumping tree: %s", err), codes.ErrorDumpingTree)
95+
return nil, common.Exit(fmt.Sprintf("Error dumping tree: %s", err), codes.ErrorDumpingTree)
9696
}
9797
return bytes, nil
9898
}

cmd/sops/edit.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func editExample(opts editExampleOpts) ([]byte, error) {
4747
fileBytes := opts.InputStore.EmitExample()
4848
branches, err := opts.InputStore.LoadPlainFile(fileBytes)
4949
if err != nil {
50-
return nil, common.NewExitError(fmt.Sprintf("Error unmarshalling file: %s", err), codes.CouldNotReadInputFile)
50+
return nil, common.Exit(fmt.Sprintf("Error unmarshalling file: %s", err), codes.CouldNotReadInputFile)
5151
}
5252
path, err := filepath.Abs(opts.InputPath)
5353
if err != nil {
@@ -62,7 +62,7 @@ func editExample(opts editExampleOpts) ([]byte, error) {
6262
// Generate a data key
6363
dataKey, errs := tree.GenerateDataKeyWithKeyServices(opts.KeyServices)
6464
if len(errs) > 0 {
65-
return nil, common.NewExitError(fmt.Sprintf("Error encrypting the data key with one or more master keys: %s", errs), codes.CouldNotRetrieveKey)
65+
return nil, common.Exit(fmt.Sprintf("Error encrypting the data key with one or more master keys: %s", errs), codes.CouldNotRetrieveKey)
6666
}
6767

6868
return editTree(opts.editOpts, &tree, dataKey)
@@ -99,19 +99,19 @@ func editTree(opts editOpts, tree *sops.Tree, dataKey []byte) ([]byte, error) {
9999
// Create temporary file for editing
100100
tmpdir, err := os.MkdirTemp("", "")
101101
if err != nil {
102-
return nil, common.NewExitError(fmt.Sprintf("Could not create temporary directory: %s", err), codes.CouldNotWriteOutputFile)
102+
return nil, common.Exit(fmt.Sprintf("Could not create temporary directory: %s", err), codes.CouldNotWriteOutputFile)
103103
}
104104
defer os.RemoveAll(tmpdir)
105105

106106
tmpfile, err := os.Create(filepath.Join(tmpdir, filepath.Base(opts.InputPath)))
107107
if err != nil {
108-
return nil, common.NewExitError(fmt.Sprintf("Could not create temporary file: %s", err), codes.CouldNotWriteOutputFile)
108+
return nil, common.Exit(fmt.Sprintf("Could not create temporary file: %s", err), codes.CouldNotWriteOutputFile)
109109
}
110110
// Ensure that in any case, the temporary file is always closed.
111111
defer tmpfile.Close()
112112
// Ensure that the file is read+write for owner only.
113113
if err = tmpfile.Chmod(0600); err != nil {
114-
return nil, common.NewExitError(fmt.Sprintf("Could not change permissions of temporary file to read-write for owner only: %s", err), codes.CouldNotWriteOutputFile)
114+
return nil, common.Exit(fmt.Sprintf("Could not change permissions of temporary file to read-write for owner only: %s", err), codes.CouldNotWriteOutputFile)
115115
}
116116

117117
tmpfileName := tmpfile.Name()
@@ -124,17 +124,17 @@ func editTree(opts editOpts, tree *sops.Tree, dataKey []byte) ([]byte, error) {
124124
out, err = opts.OutputStore.EmitPlainFile(tree.Branches)
125125
}
126126
if err != nil {
127-
return nil, common.NewExitError(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
127+
return nil, common.Exit(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
128128
}
129129
_, err = tmpfile.Write(out)
130130
if err != nil {
131-
return nil, common.NewExitError(fmt.Sprintf("Could not write output file: %s", err), codes.CouldNotWriteOutputFile)
131+
return nil, common.Exit(fmt.Sprintf("Could not write output file: %s", err), codes.CouldNotWriteOutputFile)
132132
}
133133

134134
// Compute file hash to detect if the file has been edited
135135
origHash, err := hashFile(tmpfileName)
136136
if err != nil {
137-
return nil, common.NewExitError(fmt.Sprintf("Could not hash file: %s", err), codes.CouldNotReadInputFile)
137+
return nil, common.Exit(fmt.Sprintf("Could not hash file: %s", err), codes.CouldNotReadInputFile)
138138
}
139139

140140
// Close the temporary file, so that an editor can open it.
@@ -164,7 +164,7 @@ func editTree(opts editOpts, tree *sops.Tree, dataKey []byte) ([]byte, error) {
164164
// Output the file
165165
encryptedFile, err := opts.OutputStore.EmitEncryptedFile(*tree)
166166
if err != nil {
167-
return nil, common.NewExitError(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
167+
return nil, common.Exit(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
168168
}
169169
return encryptedFile, nil
170170
}
@@ -173,18 +173,18 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
173173
for {
174174
err := runEditor(opts.TmpFileName)
175175
if err != nil {
176-
return common.NewExitError(fmt.Sprintf("Could not run editor: %s", err), codes.NoEditorFound)
176+
return common.Exit(fmt.Sprintf("Could not run editor: %s", err), codes.NoEditorFound)
177177
}
178178
newHash, err := hashFile(opts.TmpFileName)
179179
if err != nil {
180-
return common.NewExitError(fmt.Sprintf("Could not hash file: %s", err), codes.CouldNotReadInputFile)
180+
return common.Exit(fmt.Sprintf("Could not hash file: %s", err), codes.CouldNotReadInputFile)
181181
}
182182
if bytes.Equal(newHash, opts.OriginalHash) {
183-
return common.NewExitError("File has not changed, exiting.", codes.FileHasNotBeenModified)
183+
return common.Exit("File has not changed, exiting.", codes.FileHasNotBeenModified)
184184
}
185185
edited, err := os.ReadFile(opts.TmpFileName)
186186
if err != nil {
187-
return common.NewExitError(fmt.Sprintf("Could not read edited file: %s", err), codes.CouldNotReadInputFile)
187+
return common.Exit(fmt.Sprintf("Could not read edited file: %s", err), codes.CouldNotReadInputFile)
188188
}
189189
newBranches, err := opts.InputStore.LoadPlainFile(edited)
190190
if err != nil {
@@ -217,7 +217,7 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
217217
opts.Tree.Branches = newBranches
218218
needVersionUpdated, err := version.AIsNewerThanB(version.Version, opts.Tree.Metadata.Version)
219219
if err != nil {
220-
return common.NewExitError(fmt.Sprintf("Failed to compare document version %q with program version %q: %v", opts.Tree.Metadata.Version, version.Version, err), codes.FailedToCompareVersions)
220+
return common.Exit(fmt.Sprintf("Failed to compare document version %q with program version %q: %v", opts.Tree.Metadata.Version, version.Version, err), codes.FailedToCompareVersions)
221221
}
222222
if needVersionUpdated {
223223
opts.Tree.Metadata.Version = version.Version

cmd/sops/encrypt.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,23 @@ func encrypt(opts encryptOpts) (encryptedFile []byte, err error) {
8484
if opts.ReadFromStdin {
8585
fileBytes, err = io.ReadAll(os.Stdin)
8686
if err != nil {
87-
return nil, common.NewExitError(fmt.Sprintf("Error reading from stdin: %s", err), codes.CouldNotReadInputFile)
87+
return nil, common.Exit(fmt.Sprintf("Error reading from stdin: %s", err), codes.CouldNotReadInputFile)
8888
}
8989
} else {
9090
fileBytes, err = os.ReadFile(opts.InputPath)
9191
if err != nil {
92-
return nil, common.NewExitError(fmt.Sprintf("Error reading file: %s", err), codes.CouldNotReadInputFile)
92+
return nil, common.Exit(fmt.Sprintf("Error reading file: %s", err), codes.CouldNotReadInputFile)
9393
}
9494
}
9595
branches, err := opts.InputStore.LoadPlainFile(fileBytes)
9696
if err != nil {
97-
return nil, common.NewExitError(fmt.Sprintf("Error unmarshalling file: %s", err), codes.CouldNotReadInputFile)
97+
return nil, common.Exit(fmt.Sprintf("Error unmarshalling file: %s", err), codes.CouldNotReadInputFile)
9898
}
9999
if len(branches) < 1 {
100-
return nil, common.NewExitError("File cannot be completely empty, it must contain at least one document", codes.NeedAtLeastOneDocument)
100+
return nil, common.Exit("File cannot be completely empty, it must contain at least one document", codes.NeedAtLeastOneDocument)
101101
}
102102
if err := ensureNoMetadata(opts, branches[0]); err != nil {
103-
return nil, common.NewExitError(err, codes.FileAlreadyEncrypted)
103+
return nil, common.Exit(err, codes.FileAlreadyEncrypted)
104104
}
105105
path, err := filepath.Abs(opts.InputPath)
106106
if err != nil {
@@ -128,7 +128,7 @@ func encrypt(opts encryptOpts) (encryptedFile []byte, err error) {
128128

129129
encryptedFile, err = opts.OutputStore.EmitEncryptedFile(tree)
130130
if err != nil {
131-
return nil, common.NewExitError(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
131+
return nil, common.Exit(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
132132
}
133133
return
134134
}

0 commit comments

Comments
 (0)