Print Krmfile data for cfg tree

This commit is contained in:
Phani Teja Marupaka
2020-09-10 23:21:04 -07:00
parent 5c8c7a043a
commit 880009b648
6 changed files with 152 additions and 32 deletions

View File

@@ -6,6 +6,7 @@ package kio
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
@@ -33,10 +34,11 @@ var GraphStructures = []string{string(TreeStructureGraph), string(TreeStructureP
// TODO(pwittrock): test this package better. it is lower-risk since it is only
// used for printing rather than updating or editing.
type TreeWriter struct {
Writer io.Writer
Root string
Fields []TreeWriterField
Structure TreeStructure
Writer io.Writer
Root string
Fields []TreeWriterField
Structure TreeStructure
OpenAPIFileName string
}
// TreeWriterField configures a Resource field to be included in the tree
@@ -72,7 +74,7 @@ func (p TreeWriter) packageStructure(nodes []*yaml.RNode) error {
// create a new branch for the package
createOk := pkg != "." // special edge case logic for tree on current working dir
if createOk {
branch = branch.AddBranch(pkg)
branch = branch.AddBranch(branchName(p.Root, pkg, p.OpenAPIFileName))
}
// cache the branch for this package
@@ -91,6 +93,19 @@ func (p TreeWriter) packageStructure(nodes []*yaml.RNode) error {
return err
}
// branchName takes the root directory and relative path to the directory
// and returns the branch name
func branchName(root, dirRelPath, openAPIFileName string) string {
name := filepath.Base(dirRelPath)
_, err := os.Stat(filepath.Join(root, dirRelPath, openAPIFileName))
if !os.IsNotExist(err) {
// add Pkg: prefix indicating that it is a separate package as it has
// openAPIFile
return fmt.Sprintf("Pkg: %s", name)
}
return name
}
// Write writes the ascii tree to p.Writer
func (p TreeWriter) Write(nodes []*yaml.RNode) error {
switch p.Structure {

View File

@@ -6,7 +6,6 @@ package kio_test
import (
"bytes"
"fmt"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
@@ -76,9 +75,9 @@ spec:
└── foo-package
├── [f1.yaml] Deployment default/foo
├── [f1.yaml] Service default/foo
└── foo-package%s3
└── 3
└── [f3.yaml] Deployment default/foo
`, string(filepath.Separator)), out.String()) {
`), out.String()) {
t.FailNow()
}
}