Skip to content

Commit

Permalink
Indentation of json files
Browse files Browse the repository at this point in the history
Signed-off-by: Bastien Wermeille <[email protected]>
  • Loading branch information
Ph0tonic committed Nov 24, 2023
1 parent 6ad2a82 commit c6dc526
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
13 changes: 12 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,18 @@ When operating on stdin, use the ``--input-type`` and ``--output-type`` flags as
$ cat myfile.json | sops --input-type json --output-type json -d /dev/stdin
JSON indentation
~~~~~~~~~~~~~~~~
``sops`` indent ``SOPS`` files by default using one ``tab``. However, you can change
this default behaviour to use spaces be either using the additional ``--indent=2`` cli option or
by configuring ``.sops.yaml`` with :
.. code:: yaml
stores:
json:
indent: 2
YAML indentation
~~~~~~~~~~~~~~~~
Expand All @@ -1174,7 +1186,6 @@ by configuring ``.sops.yaml`` with :
yaml:
indent: 2
YAML anchors
~~~~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,9 @@ func inputStore(context *cli.Context, path string) common.Store {
func outputStore(context *cli.Context, path string) common.Store {
storesConf, _ := loadStoresConfig(context, path)
if context.Int("indent") != 0 {
storesConf.YAML.Indent = context.Int("indent")
indent := context.Int("indent")
storesConf.YAML.Indent = indent
storesConf.JSON.Indent = indent
}

return common.DefaultStoreForPathOrFormat(storesConf, path, context.String("output-type"))
Expand Down
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ type DotenvStoreConfig struct{}

type INIStoreConfig struct{}

type JSONStoreConfig struct{}
type JSONStoreConfig struct{
Indent int `yaml:"indent"`
}

type JSONBinaryStoreConfig struct{}

Expand Down
7 changes: 6 additions & 1 deletion stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"strings"

"github.com/getsops/sops/v3"
"github.com/getsops/sops/v3/config"
Expand Down Expand Up @@ -248,7 +249,11 @@ func (store Store) treeBranchFromJSON(in []byte) (sops.TreeBranch, error) {

func (store Store) reindentJSON(in []byte) ([]byte, error) {
var out bytes.Buffer
err := json.Indent(&out, in, "", "\t")
indent := "\t"
if store.config.Indent != 0 {
indent = strings.Repeat(" ", store.config.Indent)
}
err := json.Indent(&out, in, "", indent)
return out.Bytes(), err
}

Expand Down
81 changes: 81 additions & 0 deletions stores/json/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/getsops/sops/v3"
"github.com/getsops/sops/v3/config"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -409,3 +410,83 @@ func TestEmitValueString(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, []byte("\"hello\""), bytes)
}

func TestIndentTwoSpaces(t *testing.T) {
tree := sops.Tree{
Branches: sops.TreeBranches{
sops.TreeBranch{
sops.TreeItem{
Key: "foo",
Value: []interface{}{
sops.TreeBranch{
sops.TreeItem{
Key: "foo",
Value: 3,
},
sops.TreeItem{
Key: "bar",
Value: false,
},
},
2,
},
},
},
},
}
expected := `{
"foo": [
{
"foo": 3,
"bar": false
},
2
]
}`
store := Store{
config: config.JSONStoreConfig{
Indent: 2,
},
}
out, err := store.EmitPlainFile(tree.Branches)
assert.Nil(t, err)
assert.Equal(t, expected, string(out))
}

func TestIndentDefault(t *testing.T) {
tree := sops.Tree{
Branches: sops.TreeBranches{
sops.TreeBranch{
sops.TreeItem{
Key: "foo",
Value: []interface{}{
sops.TreeBranch{
sops.TreeItem{
Key: "foo",
Value: 3,
},
sops.TreeItem{
Key: "bar",
Value: false,
},
},
2,
},
},
},
},
}
expected := `{
"foo": [
{
"foo": 3,
"bar": false
},
2
]
}`
store := Store{}
out, err := store.EmitPlainFile(tree.Branches)
assert.Nil(t, err)
assert.Equal(t, expected, string(out))
}

0 comments on commit c6dc526

Please sign in to comment.