From 755c16d49c6417bfc59542821f2aead9338dd468 Mon Sep 17 00:00:00 2001 From: Bastien Wermeille Date: Thu, 14 Sep 2023 20:41:56 +0200 Subject: [PATCH] Allow no indent at all for json store Signed-off-by: Bastien Wermeille --- README.rst | 6 ++--- config/config.go | 7 +++++ decrypt/decrypt.go | 2 +- stores/json/store.go | 2 +- stores/json/store_test.go | 54 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 4178ee1c9..6b89181db 100644 --- a/README.rst +++ b/README.rst @@ -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: @@ -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 diff --git a/config/config.go b/config/config.go index fbb99d535..8ab15c235 100644 --- a/config/config.go +++ b/config/config.go @@ -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) @@ -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) diff --git a/decrypt/decrypt.go b/decrypt/decrypt.go index dc213fd36..e26cbe479 100644 --- a/decrypt/decrypt.go +++ b/decrypt/decrypt.go @@ -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) diff --git a/stores/json/store.go b/stores/json/store.go index 41009d358..da1cc34ff 100644 --- a/stores/json/store.go +++ b/stores/json/store.go @@ -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) diff --git a/stores/json/store_test.go b/stores/json/store_test.go index 991610cac..eca6d3e96 100644 --- a/stores/json/store_test.go +++ b/stores/json/store_test.go @@ -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)) @@ -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))