Skip to content

Commit

Permalink
Allow no indent at all for json store
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 c6dc526 commit 755c16d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,8 @@ 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 :
this default behaviour to use spaces by either using the additional ``--indent=2`` cli option or
by configuring ``.sops.yaml`` with the code below. (value ``0`` is no indentation)
.. code:: yaml
stores:
Expand All @@ -1178,7 +1178,7 @@ YAML indentation
~~~~~~~~~~~~~~~~
``sops`` indent ``YAML`` files by default using 4 spaces. However, you can change
this default behaviour be either using the additional ``--indent=2`` cli option or
this default behaviour by either using the additional ``--indent=2`` cli option or
by configuring ``.sops.yaml`` with :
.. code:: yaml
Expand Down
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ type creationRule struct {
MACOnlyEncrypted bool `yaml:"mac_only_encrypted"`
}

func NewStoresConfig() *StoresConfig{
storesConfig := &StoresConfig{}
storesConfig.JSON.Indent = -1
return storesConfig
}

// Load loads a sops config file into a temporary struct
func (f *configFile) load(bytes []byte) error {
err := yaml.Unmarshal(bytes, f)
Expand Down Expand Up @@ -252,6 +258,7 @@ func loadConfigFile(confPath string) (*configFile, error) {
return nil, fmt.Errorf("could not read config file: %s", err)
}
conf := &configFile{}
conf.Stores = *NewStoresConfig()
err = conf.load(confBytes)
if err != nil {
return nil, fmt.Errorf("error loading config: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func File(path, format string) (cleartext []byte, err error) {
// decrypts the data and returns its cleartext in an []byte.
func DataWithFormat(data []byte, format Format) (cleartext []byte, err error) {

store := common.StoreForFormat(format, &config.StoresConfig{})
store := common.StoreForFormat(format, config.NewStoresConfig())

// Load SOPS file and access the data key
tree, err := store.LoadEncryptedFile(data)
Expand Down
2 changes: 1 addition & 1 deletion stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ 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 != 0 {
if store.config.Indent != -1 {
indent = strings.Repeat(" ", store.config.Indent)
}
err := json.Indent(&out, in, "", indent)
Expand Down
54 changes: 52 additions & 2 deletions stores/json/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ func TestEncodeJSONArrayOfObjects(t *testing.T) {
2
]
}`
store := Store{}
store := Store{
config: config.JSONStoreConfig{
Indent: -1,
},
}
out, err := store.EmitPlainFile(tree.Branches)
assert.Nil(t, err)
assert.Equal(t, expected, string(out))
Expand Down Expand Up @@ -485,7 +489,53 @@ func TestIndentDefault(t *testing.T) {
2
]
}`
store := Store{}
store := Store{
config: config.JSONStoreConfig{
Indent: -1,
},
}
out, err := store.EmitPlainFile(tree.Branches)
assert.Nil(t, err)
assert.Equal(t, expected, string(out))
}

func TestNoIndent(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: 0,
},
}
out, err := store.EmitPlainFile(tree.Branches)
assert.Nil(t, err)
assert.Equal(t, expected, string(out))
Expand Down

0 comments on commit 755c16d

Please sign in to comment.