Skip to content

Commit

Permalink
feat(export): switch to treeprint for multiline support in base expor…
Browse files Browse the repository at this point in the history
…t format
  • Loading branch information
FalcoSuessgott committed May 14, 2024
1 parent 6fe3bb4 commit 75f83b2
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (s *VaultSuite) TestMode() {
expected: `e2e/ [type=kv2]
├── sub [v=1]
│ └── user=********
└── sub2 [v=1]
└── key=*****
`,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.31.0
github.com/testcontainers/testcontainers-go/modules/vault v0.31.0
github.com/xlab/treeprint v1.2.0
gotest.tools/gotestsum v1.11.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ github.com/uudashr/gocognit v1.1.2 h1:l6BAEKJqQH2UpKAPKdMfZf5kE4W/2xk8pfU1OVLvni
github.com/uudashr/gocognit v1.1.2/go.mod h1:aAVdLURqcanke8h3vg35BC++eseDm66Z7KmchI5et4k=
github.com/xen0n/gosmopolitan v1.2.2 h1:/p2KTnMzwRexIW8GlKawsTWOxn7UHA+jCMF/V8HHtvU=
github.com/xen0n/gosmopolitan v1.2.2/go.mod h1:7XX7Mj61uLYrj0qmeN0zi7XDon9JRAEhYQqAPLVNTeg=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yagipy/maintidx v1.0.0 h1:h5NvIsCz+nRDapQ0exNv4aJ0yXSI0420omVANTv3GJM=
github.com/yagipy/maintidx v1.0.0/go.mod h1:0qNf/I/CCZXSMhsRsrEPDZ+DkekpKLXAJfsTACwgXLk=
github.com/yeya24/promlinter v0.3.0 h1:JVDbMp08lVCP7Y6NP3qHroGAO6z2yGKQtS5JsjqtoFs=
Expand Down
4 changes: 2 additions & 2 deletions pkg/printer/engine/engine_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ func (p *Printer) Out(engines map[string][]string) error {
return err
}

fmt.Fprintln(p.writer, string(out))
fmt.Fprint(p.writer, string(out))
case JSON:
out, err := utils.ToJSON(map[string]interface{}{"engines": utils.RemoveDuplicates(engineList)})
if err != nil {
return err
}

fmt.Fprintln(p.writer, string(out))
fmt.Fprint(p.writer, string(out))
case Base:
for _, k := range utils.RemoveDuplicates(engineList) {
fmt.Fprintln(p.writer, k)
Expand Down
1 change: 0 additions & 1 deletion pkg/printer/engine/engine_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ a1
expected: `engines:
- a
- b
`,
},
{
Expand Down
6 changes: 3 additions & 3 deletions pkg/printer/namespace/namespace_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewPrinter(opts ...Option) *Printer {
return p
}

// Out prits out namespaces in various formats.
// Out prints out namespaces in various formats.
// nolint: cyclop
func (p *Printer) Out(ns map[string][]string) error {
nsList := p.buildNamespaceList(ns)
Expand All @@ -104,14 +104,14 @@ func (p *Printer) Out(ns map[string][]string) error {
return err
}

fmt.Fprintln(p.writer, string(out))
fmt.Fprint(p.writer, string(out))
case JSON:
out, err := utils.ToJSON(map[string]interface{}{"namespaces": utils.RemoveDuplicates(nsList)})
if err != nil {
return err
}

fmt.Fprintln(p.writer, string(out))
fmt.Fprint(p.writer, string(out))
case Base:
for _, k := range utils.RemoveDuplicates(nsList) {
fmt.Fprintln(p.writer, k)
Expand Down
1 change: 0 additions & 1 deletion pkg/printer/namespace/namespace_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ a/a2
expected: `namespaces:
- a
- b
`,
},
{
Expand Down
21 changes: 11 additions & 10 deletions pkg/printer/secret/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"strings"

"github.com/FalcoSuessgott/vkv/pkg/utils"
"github.com/disiqueira/gotree/v3"
"github.com/savioxavier/termlink"
"github.com/xlab/treeprint"
)

func (p *Printer) printBase(enginePath string, secrets map[string]interface{}) error {
var tree gotree.Tree
var tree treeprint.Tree

m := make(map[string]interface{})

Expand All @@ -39,37 +39,38 @@ func (p *Printer) printBase(enginePath string, secrets map[string]interface{}) e
}
}

tree = gotree.New(baseName)
tree = treeprint.NewWithRoot(baseName)

m = utils.ToMapStringInterface(secrets[k])
}

for _, i := range utils.SortMapKeys(m) {
//nolint: forcetypeassert
tree.AddTree(p.printTree(enginePath, i, m[i].(map[string]interface{})))
tree.AddBranch(p.printTree(enginePath, i, m[i].(map[string]interface{})))
}

fmt.Fprint(p.writer, tree.Print())
fmt.Fprintln(p.writer, strings.TrimSpace(tree.String()))

return nil
}

func (p *Printer) printTree(rootPath, subPath string, m map[string]interface{}) gotree.Tree {
tree := gotree.New(p.buildTreeName(rootPath, subPath))
func (p *Printer) printTree(rootPath, subPath string, m map[string]interface{}) treeprint.Tree {
tree := treeprint.NewWithRoot(p.buildTreeName(rootPath, subPath))

//nolint: nestif
if strings.HasSuffix(subPath, utils.Delimiter) {
for _, i := range utils.SortMapKeys(m) {
//nolint: forcetypeassert
tree.AddTree(p.printTree(rootPath, subPath+i, m[i].(map[string]interface{})))
tree.AddBranch(p.printTree(rootPath, subPath+i, m[i].(map[string]interface{})))
}
} else {
for _, k := range utils.SortMapKeys(m) {
if p.onlyKeys {
tree.Add(k)
tree.AddNode(k)
}

if !p.onlyKeys && !p.onlyPaths {
tree.Add(fmt.Sprintf("%s=%v", k, m[k]))
tree.AddNode(fmt.Sprintf("%s=%v", k, m[k]))
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/printer/secret/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ func TestPrintBase(t *testing.T) {
└── secret
├── key
└── user
`,
},
{
name: "test: multiple lines",
rootPath: "root",
s: map[string]interface{}{
"secret": map[string]interface{}{
"key": "value",
"user": "value",
},
},
opts: []Option{
ToFormat(Base),
ShowValues(true),
},
output: `root/
└── secret
├── key=value
└── user=value
`,
},
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/printer/secret/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package secret

import (
"fmt"
"strings"

"github.com/FalcoSuessgott/vkv/pkg/utils"
)
Expand All @@ -13,7 +12,7 @@ func (p *Printer) printJSON(secrets map[string]interface{}) error {
return err
}

fmt.Fprint(p.writer, strings.TrimSuffix(string(out), "\n"))
fmt.Fprint(p.writer, string(out))

return nil
}
6 changes: 4 additions & 2 deletions pkg/printer/secret/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestPrintJSON(t *testing.T) {
"user": "password"
}
}
}`,
}
`,
},
{
name: "test: normal map to json only keys",
Expand All @@ -59,7 +60,8 @@ func TestPrintJSON(t *testing.T) {
"user": ""
}
}
}`,
}
`,
},
}

Expand Down
8 changes: 4 additions & 4 deletions scripts/prepare-vault.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export VAULT_TOKEN="root"
vault secrets enable -path secret -version=2 kv
vault kv put secret/demo foo=bar
vault kv put secret/admin sub=password
vault kv put secret/sub/demo demo="hello world" user=admin password=s3cre5
vault kv put secret/sub/demo demo="hello world" user=admin password='s3cre5<'
vault kv put secret/sub/sub2/demo foo=bar user=user password=password
vault kv put secret/sub/sub2/demo admin=key foo=bar user=user password=password
vault kv put secret/sub/sub2/demo foo=bar user=user password=password
vault kv metadata put -mount=secret -custom-metadata=key=value admin
vault kv metadata put -mount=secret -custom-metadata=key=value -custom-metadata=admin=false sub/sub2/demo
vault policy write kv assets/kv-policy.hcl

vault secrets enable -path secret_2 -version=2 kv
vault kv put secret_2/demo foo=bar
vault kv put secret_2/admin sub=password
vault kv put secret_2/sub/demo demo="hello world" user=admin password=s3cre5
vault kv put secret_2/sub/sub2/demo foo=bar-updated user=user password=password
vault kv put secret_2/sub/demo demo="hello world" user=admin password='s3cre5<'
vault kv put secret_2/sub/sub2/demo foo=bar-updated user=user password=password

0 comments on commit 75f83b2

Please sign in to comment.