mirror of
https://github.com/getsops/sops.git
synced 2026-02-05 12:45:21 +01:00
Merge branch 'main' into fixes
This commit is contained in:
@@ -186,18 +186,20 @@ func (store Store) encodeValue(v interface{}) ([]byte, error) {
|
||||
|
||||
func (store Store) encodeArray(array []interface{}) ([]byte, error) {
|
||||
out := "["
|
||||
for i, item := range array {
|
||||
empty := true
|
||||
for _, item := range array {
|
||||
if _, ok := item.(sops.Comment); ok {
|
||||
continue
|
||||
}
|
||||
if !empty {
|
||||
out += ","
|
||||
}
|
||||
v, err := store.encodeValue(item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out += string(v)
|
||||
if i != len(array)-1 {
|
||||
out += ","
|
||||
}
|
||||
empty = false
|
||||
}
|
||||
out += "]"
|
||||
return []byte(out), nil
|
||||
@@ -205,10 +207,14 @@ func (store Store) encodeArray(array []interface{}) ([]byte, error) {
|
||||
|
||||
func (store Store) encodeTree(tree sops.TreeBranch) ([]byte, error) {
|
||||
out := "{"
|
||||
for i, item := range tree {
|
||||
empty := true
|
||||
for _, item := range tree {
|
||||
if _, ok := item.Key.(sops.Comment); ok {
|
||||
continue
|
||||
}
|
||||
if !empty {
|
||||
out += ","
|
||||
}
|
||||
v, err := store.encodeValue(item.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error encoding value %s: %s", v, err)
|
||||
@@ -218,9 +224,7 @@ func (store Store) encodeTree(tree sops.TreeBranch) ([]byte, error) {
|
||||
return nil, fmt.Errorf("Error encoding key %s: %s", k, err)
|
||||
}
|
||||
out += string(k) + `: ` + string(v)
|
||||
if i != len(tree)-1 {
|
||||
out += ","
|
||||
}
|
||||
empty = false
|
||||
}
|
||||
return []byte(out + "}"), nil
|
||||
}
|
||||
|
||||
@@ -562,3 +562,70 @@ func TestConflictingAttributes(t *testing.T) {
|
||||
_, err := s.LoadPlainFile([]byte(data))
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestComments(t *testing.T) {
|
||||
tree := sops.Tree{
|
||||
Branches: sops.TreeBranches{
|
||||
sops.TreeBranch{
|
||||
sops.TreeItem{
|
||||
Key: "foo",
|
||||
Value: []interface{}{
|
||||
sops.Comment{Value: " comment 0"},
|
||||
sops.TreeBranch{
|
||||
sops.TreeItem{
|
||||
Key: sops.Comment{Value: " comment 1"},
|
||||
Value: nil,
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: "foo",
|
||||
Value: 3,
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: sops.Comment{Value: " comment 2"},
|
||||
Value: nil,
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: sops.Comment{Value: " comment 3"},
|
||||
Value: nil,
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: "bar",
|
||||
Value: false,
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: sops.Comment{Value: " comment 4"},
|
||||
Value: nil,
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: sops.Comment{Value: " comment 5"},
|
||||
Value: nil,
|
||||
},
|
||||
},
|
||||
sops.Comment{Value: " comment 6"},
|
||||
sops.Comment{Value: " comment 7"},
|
||||
2,
|
||||
sops.Comment{Value: " comment 8"},
|
||||
sops.Comment{Value: " comment 9"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user