Skip to content

Commit

Permalink
fix latest issues
Browse files Browse the repository at this point in the history
- use ascii for the tree lines. Fixes #6
- verbose mode to show resource ApiGroups #7
- put main.go in the kni package. Fixes #3
  • Loading branch information
nimakaviani committed Nov 11, 2019
1 parent 0c821a0 commit 768086e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
go.sum
kni*
!cmd/kni
File renamed without changes.
8 changes: 5 additions & 3 deletions pkg/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
type InspectCmdOptions struct {
ui ui.UI

Debug bool
opts insp.InspectorOptions
Debug bool
Verbose bool
opts insp.InspectorOptions

kubeconfigFlags flags.KubeconfigFlags
namespaceFlags flags.NamespaceFlags
Expand All @@ -34,6 +35,7 @@ func NewInspectCmd(o *InspectCmdOptions) *cobra.Command {
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
}
cmd.Flags().BoolVar(&o.Debug, "debug", false, "Enable debug output")
cmd.Flags().BoolVarP(&o.Verbose, "verbose", "v", false, "Show verbose output")
cmd.Flags().StringSliceVarP(&o.opts.Services, "service", "s", []string{}, "Knative services to inspect (can be specified multiple times)")

o.kubeconfigFlags.Set(cmd)
Expand Down Expand Up @@ -62,6 +64,6 @@ func (o *InspectCmdOptions) inspect() error {
return err
}

kiui.TreeView{Source: strings.Join(o.opts.Services, ","), ResourceMap: result}.Print(o.ui)
kiui.TreeView{Source: strings.Join(o.opts.Services, ","), ResourceMap: result, Verbose: o.Verbose}.Print(o.ui)
return nil
}
35 changes: 20 additions & 15 deletions pkg/cmd/ui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"sort"

"strings"

"github.com/cppforlife/go-cli-ui/ui"
Expand All @@ -17,14 +18,15 @@ type TreeView struct {
Source string
ResourceMap [][]krsc.Resource
Sort bool
Verbose bool
}

func (v TreeView) Print(ui ui.UI) {
groupHeader := uitable.NewHeader("Group")
groupHeader.Hidden = true

versionHeader := uitable.NewHeader("Version")
versionHeader.Hidden = true
versionHeader.Hidden = !v.Verbose

table := uitable.Table{
Title: fmt.Sprintf("Resources in %s", v.Source),
Expand All @@ -34,8 +36,8 @@ func (v TreeView) Print(ui ui.UI) {
groupHeader,
uitable.NewHeader("Namespace"),
uitable.NewHeader("Name"),
uitable.NewHeader("Kind"),
versionHeader,
uitable.NewHeader("Kind"),
uitable.NewHeader("Ready"),
uitable.NewHeader("Reason"),
},
Expand Down Expand Up @@ -148,6 +150,7 @@ func (v TreeView) addBlankRow(table *uitable.Table) {
uitable.NewValueString(" "),
uitable.NewValueString(" "),
uitable.NewValueString(" "),
uitable.NewValueString(" "),
}
table.Rows = append(table.Rows, row)
}
Expand All @@ -172,24 +175,26 @@ func (v TreeView) addRows(table *uitable.Table, rt resourceTree, depth int, conn

var delim string
if depth > 0 {
delim = fmt.Sprintf("%sL ", connector)
delim = fmt.Sprintf("%sL", connector)
if !hasSibling {
delim += "_"
}
delim += " "
}

delim = strings.ReplaceAll(delim, "|L", "L")

row := []uitable.Value{
uitable.NewValueString(""),
uitable.NewValueString(resource.Namespace()),
uitable.NewValueString(resource.Name()),
ValueColored{
uitable.NewValueString(resource.APIVersion()),
ValueEncoded{
S: delim + resource.Kind(),
Func: func(str string, opts ...interface{}) string {
result := fmt.Sprintf(str, opts...)
return strings.Replace(result, delim, color.New(color.Faint).Sprintf("%s", delim), -1)
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(result, "|L", "├─"), "|", "│"), "L_", "└─")
},
},
uitable.NewValueString(resource.APIVersion()),
ValueColored{
ValueEncoded{
S: msg,
Func: func(str string, opts ...interface{}) string {
result := fmt.Sprintf(str, opts...)
Expand All @@ -202,7 +207,7 @@ func (v TreeView) addRows(table *uitable.Table, rt resourceTree, depth int, conn
return result
},
},
ValueColored{
ValueEncoded{
S: reason,
Func: func(str string, opts ...interface{}) string {
result := fmt.Sprintf(str, opts...)
Expand All @@ -227,15 +232,15 @@ func (v TreeView) addRows(table *uitable.Table, rt resourceTree, depth int, conn
}
}

type ValueColored struct {
type ValueEncoded struct {
S string
Func func(string, ...interface{}) string
}

func (t ValueColored) String() string { return t.S }
func (t ValueColored) Value() uitable.Value { return t }
func (t ValueColored) Compare(other uitable.Value) int { panic("Never called") }
func (t ValueEncoded) String() string { return t.S }
func (t ValueEncoded) Value() uitable.Value { return t }
func (t ValueEncoded) Compare(other uitable.Value) int { panic("Never called") }

func (t ValueColored) Fprintf(w io.Writer, pattern string, rest ...interface{}) (int, error) {
func (t ValueEncoded) Fprintf(w io.Writer, pattern string, rest ...interface{}) (int, error) {
return fmt.Fprintf(w, "%s", t.Func(pattern, rest...))
}
2 changes: 1 addition & 1 deletion pkg/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
Version = "0.1.0"
Version = "0.2.0"
)

type VersionOptions struct {
Expand Down

0 comments on commit 768086e

Please sign in to comment.