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 12, 2024
1 parent c115e3d commit be396de
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 21 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
31 changes: 21 additions & 10 deletions pkg/printer/secret/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"fmt"
"net/url"
"path"
"strconv"
"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 +40,47 @@ 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{})))

Check warning on line 65 in pkg/printer/secret/base.go

View check run for this annotation

Codecov / codecov/patch

pkg/printer/secret/base.go#L65

Added line #L65 was not covered by tests
}
} 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]))
//nolint:forcetypeassert
value := m[k].(string)

// https://stackoverflow.com/a/69693440
t, err := strconv.Unquote("\"" + value + "\"")
if err == nil {
value = t
}

tree.AddNode(fmt.Sprintf("%s=%v", k, value))
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/printer/secret/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ 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": "hey\nthis\nis\na\nmultiline",
},
},
opts: []Option{
ToFormat(Base),
ShowValues(true),
},
output: `root/
└── secret
├── key=value
└── user=hey
this
is
a
multiline
`,
},
}
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

0 comments on commit be396de

Please sign in to comment.