Skip to content

Commit

Permalink
Use errors.New / errors.Is.
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
felixfontein committed Sep 20, 2023
1 parent 47744aa commit b7c2142
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
9 changes: 5 additions & 4 deletions cmd/sops/decrypt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"

"github.com/getsops/sops/v3"
Expand Down Expand Up @@ -49,8 +50,8 @@ func decrypt(opts decryptOpts) (decryptedFile []byte, err error) {
return extract(tree, opts.Extract, opts.OutputStore)
}
decryptedFile, err = opts.OutputStore.EmitPlainFile(tree.Branches)
if bsepe, ok := err.(json.BinaryStoreEmitPlainError); ok {
err = fmt.Errorf("%s. %s", bsepe.Error(), notBinaryHint)
if errors.Is(err, json.BinaryStoreEmitPlainError) {
err = fmt.Errorf("%s. %s", err.Error(), notBinaryHint)
}
if err != nil {
return nil, common.NewExitError(fmt.Sprintf("Error dumping file: %s", err), codes.ErrorDumpingTree)
Expand All @@ -66,8 +67,8 @@ func extract(tree *sops.Tree, path []interface{}, outputStore sops.Store) (outpu
if newBranch, ok := v.(sops.TreeBranch); ok {
tree.Branches[0] = newBranch
decrypted, err := outputStore.EmitPlainFile(tree.Branches)
if bsepe, ok := err.(json.BinaryStoreEmitPlainError); ok {
err = fmt.Errorf("%s. %s", bsepe.Error(), notBinaryHint)
if errors.Is(err, json.BinaryStoreEmitPlainError) {
err = fmt.Errorf("%s. %s", err.Error(), notBinaryHint)
}
if err != nil {
return nil, common.NewExitError(fmt.Sprintf("Error dumping file: %s", err), codes.ErrorDumpingTree)
Expand Down
15 changes: 5 additions & 10 deletions stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package json //import "github.com/getsops/sops/v3/stores/json"
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -42,30 +43,24 @@ func (store BinaryStore) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
return store.store.EmitEncryptedFile(in)
}

type BinaryStoreEmitPlainError struct {
message string
}

func (e BinaryStoreEmitPlainError) Error() string {
return e.message
}
var BinaryStoreEmitPlainError = errors.New("Error emitting binary store")

// EmitPlainFile produces plaintext json file's bytes from its corresponding sops.TreeBranches object
func (store BinaryStore) EmitPlainFile(in sops.TreeBranches) ([]byte, error) {
if len(in) != 1 {
return nil, BinaryStoreEmitPlainError{message: "There must be exactly one tree branch"}
return nil, fmt.Errorf("%w: there must be exactly one tree branch", BinaryStoreEmitPlainError)
}
// JSON stores a single object per file
for _, item := range in[0] {
if item.Key == "data" {
if value, ok := item.Value.(string); ok {
return []byte(value), nil
} else {
return nil, BinaryStoreEmitPlainError{message: "'data' key in tree does not have a string value"}
return nil, fmt.Errorf("%w: 'data' key in tree does not have a string value", BinaryStoreEmitPlainError)
}
}
}
return nil, BinaryStoreEmitPlainError{message: "No binary data found in tree"}
return nil, fmt.Errorf("%w: no binary data found in tree", BinaryStoreEmitPlainError)
}

// EmitValue extracts a value from a generic interface{} object representing a structured set
Expand Down
6 changes: 3 additions & 3 deletions stores/json/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ func TestEmitBinaryFileWrongBranches(t *testing.T) {
},
})
assert.Nil(t, data)
assert.Contains(t, err.Error(), "There must be exactly one tree branch")
assert.Contains(t, err.Error(), "there must be exactly one tree branch")

data, err = store.EmitPlainFile(sops.TreeBranches{})
assert.Nil(t, data)
assert.Contains(t, err.Error(), "There must be exactly one tree branch")
assert.Contains(t, err.Error(), "there must be exactly one tree branch")
}

func TestEmitBinaryFileNoData(t *testing.T) {
Expand All @@ -369,7 +369,7 @@ func TestEmitBinaryFileNoData(t *testing.T) {
},
})
assert.Nil(t, data)
assert.Contains(t, err.Error(), "No binary data found in tree")
assert.Contains(t, err.Error(), "no binary data found in tree")
}

func TestEmitBinaryFileWrongDataType(t *testing.T) {
Expand Down

0 comments on commit b7c2142

Please sign in to comment.