mirror of
https://github.com/getsops/sops.git
synced 2026-02-05 12:45:21 +01:00
Convert yaml comments to generic comments
This commit is contained in:
@@ -138,6 +138,14 @@ func (store Store) encodeValue(v interface{}, pad string) ([]byte, error) {
|
||||
switch v := v.(type) {
|
||||
case sops.TreeBranch:
|
||||
return store.encodeTree(v, pad+"\t")
|
||||
case []interface{}:
|
||||
// Remove all comments
|
||||
for i, value := range v {
|
||||
if _, ok := value.(sops.Comment); ok {
|
||||
v = append(v[:i], v[i+1:]...)
|
||||
}
|
||||
}
|
||||
fallthrough
|
||||
default:
|
||||
return json.Marshal(v)
|
||||
}
|
||||
@@ -146,6 +154,9 @@ func (store Store) encodeValue(v interface{}, pad string) ([]byte, error) {
|
||||
func (store Store) encodeTree(tree sops.TreeBranch, pad string) ([]byte, error) {
|
||||
out := pad + "{\n"
|
||||
for i, item := range tree {
|
||||
if _, ok := item.Key.(sops.Comment); ok {
|
||||
continue
|
||||
}
|
||||
v, err := store.encodeValue(item.Value, pad)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error encoding value %s: %s", v, err)
|
||||
|
||||
10
sops.go
10
sops.go
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"go.mozilla.org/sops/kms"
|
||||
"go.mozilla.org/sops/pgp"
|
||||
"go.mozilla.org/yaml.v2"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -32,6 +31,11 @@ type DataKeyCipher interface {
|
||||
Decrypt(value string, key []byte, path string) (plaintext interface{}, stashValue interface{}, err error)
|
||||
}
|
||||
|
||||
// Comment represents a comment in the sops tree for the file formats that actually support them.
|
||||
type Comment struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
// TreeItem is an item inside sops's tree
|
||||
type TreeItem struct {
|
||||
Key interface{}
|
||||
@@ -100,7 +104,7 @@ func (tree TreeBranch) walkValue(in interface{}, path []string, onLeaves func(in
|
||||
|
||||
func (tree TreeBranch) walkSlice(in []interface{}, path []string, onLeaves func(in interface{}, path []string) (interface{}, error)) ([]interface{}, error) {
|
||||
for i, v := range in {
|
||||
if _, ok := v.(yaml.Comment); ok {
|
||||
if _, ok := v.(Comment); ok {
|
||||
continue
|
||||
}
|
||||
newV, err := tree.walkValue(v, path, onLeaves)
|
||||
@@ -114,7 +118,7 @@ func (tree TreeBranch) walkSlice(in []interface{}, path []string, onLeaves func(
|
||||
|
||||
func (tree TreeBranch) walkBranch(in TreeBranch, path []string, onLeaves func(in interface{}, path []string) (interface{}, error)) (TreeBranch, error) {
|
||||
for i, item := range in {
|
||||
if _, ok := item.Key.(yaml.Comment); ok {
|
||||
if _, ok := item.Key.(Comment); ok {
|
||||
continue
|
||||
}
|
||||
newV, err := tree.walkValue(item.Value, append(path, item.Key.(string)), onLeaves)
|
||||
|
||||
@@ -17,10 +17,20 @@ type Store struct {
|
||||
func (store Store) mapSliceToTreeBranch(in yaml.MapSlice) sops.TreeBranch {
|
||||
branch := make(sops.TreeBranch, 0)
|
||||
for _, item := range in {
|
||||
branch = append(branch, sops.TreeItem{
|
||||
Key: item.Key,
|
||||
Value: store.yamlValueToTreeValue(item.Value),
|
||||
})
|
||||
if comment, ok := item.Key.(yaml.Comment); ok {
|
||||
// Convert the yaml comment to a generic sops comment
|
||||
branch = append(branch, sops.TreeItem{
|
||||
Key: sops.Comment{
|
||||
Value: comment.Value,
|
||||
},
|
||||
Value: nil,
|
||||
})
|
||||
} else {
|
||||
branch = append(branch, sops.TreeItem{
|
||||
Key: item.Key,
|
||||
Value: store.yamlValueToTreeValue(item.Value),
|
||||
})
|
||||
}
|
||||
}
|
||||
return branch
|
||||
}
|
||||
@@ -47,6 +57,8 @@ func (store Store) yamlValueToTreeValue(in interface{}) interface{} {
|
||||
return store.mapSliceToTreeBranch(in)
|
||||
case []interface{}:
|
||||
return store.yamlSliceToTreeValue(in)
|
||||
case yaml.Comment:
|
||||
return sops.Comment{Value: in.Value}
|
||||
default:
|
||||
return in
|
||||
}
|
||||
@@ -88,10 +100,17 @@ func (store Store) treeValueToYamlValue(in interface{}) interface{} {
|
||||
func (store Store) treeBranchToYamlMap(in sops.TreeBranch) yaml.MapSlice {
|
||||
branch := make(yaml.MapSlice, 0)
|
||||
for _, item := range in {
|
||||
branch = append(branch, yaml.MapItem{
|
||||
Key: item.Key,
|
||||
Value: store.treeValueToYamlValue(item.Value),
|
||||
})
|
||||
if comment, ok := item.Key.(sops.Comment); ok {
|
||||
branch = append(branch, yaml.MapItem{
|
||||
Key: yaml.Comment{Value: comment.Value},
|
||||
Value: nil,
|
||||
})
|
||||
} else {
|
||||
branch = append(branch, yaml.MapItem{
|
||||
Key: item.Key,
|
||||
Value: store.treeValueToYamlValue(item.Value),
|
||||
})
|
||||
}
|
||||
}
|
||||
return branch
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user