1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Add flux tree command

The `flux tree kustomization` command prints the resources reconciled by the given Kustomization.

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
Stefan Prodan
2021-10-23 11:04:31 +03:00
parent f2475988bd
commit 80ef184b60
8 changed files with 483 additions and 0 deletions

141
internal/tree/tree.go Normal file
View File

@@ -0,0 +1,141 @@
/*
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Derived work from https://github.com/d6o/GoTree
Copyright (c) 2017 Diego Siqueira
*/
package tree
import (
"strings"
"github.com/fluxcd/pkg/ssa"
"sigs.k8s.io/cli-utils/pkg/object"
)
const (
newLine = "\n"
emptySpace = " "
middleItem = "├── "
continueItem = "│ "
lastItem = "└── "
)
type (
objMetadataTree struct {
objMetadata object.ObjMetadata
items []ObjMetadataTree
}
ObjMetadataTree interface {
Add(objMetadata object.ObjMetadata) ObjMetadataTree
AddTree(tree ObjMetadataTree)
Items() []ObjMetadataTree
Text() string
Print() string
}
printer struct {
}
Printer interface {
Print(ObjMetadataTree) string
}
)
func New(objMetadata object.ObjMetadata) ObjMetadataTree {
return &objMetadataTree{
objMetadata: objMetadata,
items: []ObjMetadataTree{},
}
}
func (t *objMetadataTree) Add(objMetadata object.ObjMetadata) ObjMetadataTree {
n := New(objMetadata)
t.items = append(t.items, n)
return n
}
func (t *objMetadataTree) AddTree(tree ObjMetadataTree) {
t.items = append(t.items, tree)
}
func (t *objMetadataTree) Text() string {
return ssa.FmtObjMetadata(t.objMetadata)
}
func (t *objMetadataTree) Items() []ObjMetadataTree {
return t.items
}
func (t *objMetadataTree) Print() string {
return newPrinter().Print(t)
}
func newPrinter() Printer {
return &printer{}
}
func (p *printer) Print(t ObjMetadataTree) string {
return t.Text() + newLine + p.printItems(t.Items(), []bool{})
}
func (p *printer) printText(text string, spaces []bool, last bool) string {
var result string
for _, space := range spaces {
if space {
result += emptySpace
} else {
result += continueItem
}
}
indicator := middleItem
if last {
indicator = lastItem
}
var out string
lines := strings.Split(text, "\n")
for i := range lines {
text := lines[i]
if i == 0 {
out += result + indicator + text + newLine
continue
}
if last {
indicator = emptySpace
} else {
indicator = continueItem
}
out += result + indicator + text + newLine
}
return out
}
func (p *printer) printItems(t []ObjMetadataTree, spaces []bool) string {
var result string
for i, f := range t {
last := i == len(t)-1
result += p.printText(f.Text(), spaces, last)
if len(f.Items()) > 0 {
spacesChild := append(spaces, last)
result += p.printItems(f.Items(), spacesChild)
}
}
return result
}