mirror of
https://github.com/getsops/sops.git
synced 2026-02-05 12:45:21 +01:00
Add cli indent option for yaml store
Signed-off-by: Bastien Wermeille <bastien.wermeille@gmail.com>
This commit is contained in:
@@ -12,6 +12,8 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const IndentDefault = 4
|
||||
|
||||
// Store handles storage of YAML data
|
||||
type Store struct {
|
||||
config config.YAMLStoreConfig
|
||||
@@ -329,7 +331,11 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
|
||||
func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
|
||||
var b bytes.Buffer
|
||||
e := yaml.NewEncoder(io.Writer(&b))
|
||||
e.SetIndent(4)
|
||||
indent := IndentDefault
|
||||
if store.config.Indent != 0 {
|
||||
indent = store.config.Indent
|
||||
}
|
||||
e.SetIndent(indent)
|
||||
for _, branch := range in.Branches {
|
||||
// Document root
|
||||
var doc = yaml.Node{}
|
||||
@@ -361,7 +367,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(4)
|
||||
indent := IndentDefault
|
||||
if store.config.Indent != 0 {
|
||||
indent = store.config.Indent
|
||||
}
|
||||
e.SetIndent(indent)
|
||||
for _, branch := range branches {
|
||||
// Document root
|
||||
var doc = yaml.Node{}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/getsops/sops/v3"
|
||||
"github.com/getsops/sops/v3/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -211,6 +212,32 @@ e:
|
||||
- f
|
||||
`)
|
||||
|
||||
var INDENT_1_IN = []byte(`## Configuration for prometheus-node-exporter subchart
|
||||
##
|
||||
prometheus-node-exporter:
|
||||
podLabels:
|
||||
## Add the 'node-exporter' label to be used by serviceMonitor to match standard common usage in rules and grafana dashboards
|
||||
##
|
||||
|
||||
jobLabel: node-exporter
|
||||
extraArgs:
|
||||
- --collector.filesystem.ignored-mount-points=^/(dev|proc|sys|var/lib/docker/.+)($|/)
|
||||
- --collector.filesystem.ignored-fs-types=^(autofs|binfmt_misc|cgroup|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|mqueue|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|sysfs|tracefs)$
|
||||
`)
|
||||
|
||||
var INDENT_1_OUT = []byte(`## Configuration for prometheus-node-exporter subchart
|
||||
##
|
||||
prometheus-node-exporter:
|
||||
podLabels:
|
||||
## Add the 'node-exporter' label to be used by serviceMonitor to match standard common usage in rules and grafana dashboards
|
||||
##
|
||||
jobLabel: node-exporter
|
||||
extraArgs:
|
||||
- --collector.filesystem.ignored-mount-points=^/(dev|proc|sys|var/lib/docker/.+)($|/)
|
||||
- --collector.filesystem.ignored-fs-types=^(autofs|binfmt_misc|cgroup|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|mqueue|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|sysfs|tracefs)$
|
||||
`)
|
||||
|
||||
|
||||
func TestUnmarshalMetadataFromNonSOPSFile(t *testing.T) {
|
||||
data := []byte(`hello: 2`)
|
||||
_, err := (&Store{}).LoadEncryptedFile(data)
|
||||
@@ -340,3 +367,17 @@ func TestComment7(t *testing.T) {
|
||||
assert.Equal(t, string(COMMENT_7_OUT), string(bytes))
|
||||
assert.Equal(t, COMMENT_7_OUT, bytes)
|
||||
}
|
||||
|
||||
func TestIndent1(t *testing.T) {
|
||||
// First iteration: load and store
|
||||
branches, err := (&Store{}).LoadPlainFile(INDENT_1_IN)
|
||||
assert.Nil(t, err)
|
||||
bytes, err := (&Store{
|
||||
config: config.YAMLStoreConfig{
|
||||
Indent: 2,
|
||||
},
|
||||
}).EmitPlainFile(branches)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, string(INDENT_1_OUT), string(bytes))
|
||||
assert.Equal(t, INDENT_1_OUT, bytes)
|
||||
}
|
||||
Reference in New Issue
Block a user