Skip to content

Commit

Permalink
Add checks for indentation value
Browse files Browse the repository at this point in the history
Signed-off-by: Bastien <bastien.wermeille@gmail.com>
  • Loading branch information
Ph0tonic committed Nov 13, 2023
1 parent 5dda5ca commit 56d19dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
4 changes: 3 additions & 1 deletion stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ func (store Store) treeBranchFromJSON(in []byte) (sops.TreeBranch, error) {
func (store Store) reindentJSON(in []byte) ([]byte, error) {
var out bytes.Buffer
indent := "\t"
if store.config.Indent != -1 {
if store.config.Indent > -1 {
indent = strings.Repeat(" ", store.config.Indent)
} else if store.config.Indent < -1 {
return nil, errors.New("JSON Indentation parameter smaller than -1 is not accepted")
}
err := json.Indent(&out, in, "", indent)
return out.Bytes(), err
Expand Down
23 changes: 17 additions & 6 deletions stores/yaml/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package yaml //import "github.com/getsops/sops/v3/stores/yaml"

import (
"bytes"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -326,19 +327,25 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
return branches, nil
}

func (store *Store) getIndentation() int {
if store.config.Indent != 0 {
return store.config.Indent
func (store *Store) getIndentation() (int, error) {
if store.config.Indent > 0 {
return store.config.Indent, nil
} else if store.config.Indent < 0 {
return 0, errors.New("YAML Negative indentation not accepted")
}
return IndentDefault
return IndentDefault, nil
}

// EmitEncryptedFile returns the encrypted bytes of the yaml file corresponding to a
// sops.Tree runtime object
func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
var b bytes.Buffer
e := yaml.NewEncoder(io.Writer(&b))
e.SetIndent(store.getIndentation())
indent, err := store.getIndentation()
if err != nil {
return nil, err
}
e.SetIndent(indent)
for _, branch := range in.Branches {
// Document root
var doc = yaml.Node{}
Expand Down Expand Up @@ -370,7 +377,11 @@ func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
func (store *Store) EmitPlainFile(branches sops.TreeBranches) ([]byte, error) {
var b bytes.Buffer
e := yaml.NewEncoder(io.Writer(&b))
e.SetIndent(store.getIndentation())
indent, err := store.getIndentation()
if err != nil {
return nil, err
}
e.SetIndent(indent)
for _, branch := range branches {
// Document root
var doc = yaml.Node{}
Expand Down

0 comments on commit 56d19dc

Please sign in to comment.