Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to show only context name in set cmd #49

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type setCmd struct {
cmd *cobra.Command
}

var showAll bool

func newSetCommand() *setCmd {

sc := &setCmd{
Expand All @@ -34,7 +36,7 @@ func newSetCommand() *setCmd {
Short: "Set kubeconfig to use in current shell",
Args: cobra.MaximumNArgs(1),
Long: `Sets kubeconfig to use or start picker dialogue.

Examples:
-> 'set' run konf selection
-> 'set <konfig id>' set a specific konf
Expand All @@ -44,6 +46,8 @@ Examples:
ValidArgsFunction: sc.completeSet,
}

sc.cmd.Flags().BoolVar(&showAll, "show-all", false, "show all columns, not only context")

return sc
}

Expand Down Expand Up @@ -170,13 +174,13 @@ func saveLatestKonf(f afero.Fs, id konf.KonfID) error {
func createSetPrompt(options []*store.Metadata) *promptui.Select {
// TODO use ssh/terminal to get the terminalsize and set trunc accordingly https://stackoverflow.com/questions/16569433/get-terminal-size-in-go
trunc := 25
promptInactive, promptActive, label, fmap := prompt.NewTableOutputTemplates(trunc)
promptInactive, promptActive, label, fmap := prompt.NewTableOutputTemplates(trunc, showAll)

// Wrapper is required as we need access to options, but the methodSignature from promptUI
// requires you to only pass an index not the whole func
// This wrapper allows us to unit-test the FuzzyFilterKonf func better
var wrapFuzzyFilterKonf = func(input string, index int) bool {
return prompt.FuzzyFilterKonf(input, options[index])
return prompt.FuzzyFilterKonf(input, options[index], showAll)
}

prompt := promptui.Select{
Expand Down
19 changes: 16 additions & 3 deletions prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/manifoldco/promptui"
"github.com/simontheleg/konf-go/store"
// to replace promptui
// "github.com/AlecAivazis/survey/v2"
)

// RunFunc describes a generic function of a prompt. It returns the selected item.
Expand All @@ -26,18 +28,24 @@ func Terminal(prompt *promptui.Select) (sel int, err error) {
}

// FuzzyFilterKonf allows fuzzy searching of a list of konf metadata in the form of store.TableOutput
func FuzzyFilterKonf(searchTerm string, curItem *store.Metadata) bool {
func FuzzyFilterKonf(searchTerm string, curItem *store.Metadata, showAll bool) bool {
// since there is no weight on any of the table entries, we can just combine them to one string
// and run the contains on it, which automatically is going to match any of the three values
contextOnly := curItem.Context

r := fmt.Sprintf("%s %s %s", curItem.Context, curItem.Cluster, curItem.File)
if !showAll {
r = contextOnly
}

return fuzzy.Match(searchTerm, r)
}

// NewTableOutputTemplates returns templating strings for creating a nicely
// formatted table out of an store.Metadata. Additionally it returns a
// template.FuncMap with all required templating funcs for the strings. Maximum
// length per column can be configured.
func NewTableOutputTemplates(maxColumnLen int) (inactive, active, label string, fmap template.FuncMap) {
func NewTableOutputTemplates(maxColumnLen int, showAll bool) (inactive, active, label string, fmap template.FuncMap) {
// minColumnLen is determined by the length of the largest word in the label line
minColumnLen := 7
if maxColumnLen < minColumnLen {
Expand All @@ -56,6 +64,12 @@ func NewTableOutputTemplates(maxColumnLen int) (inactive, active, label string,
inactive = fmt.Sprintf(` {{ repeat %[1]d " " | print .Context | trunc %[1]d | %[2]s }} | {{ repeat %[1]d " " | print .Cluster | trunc %[1]d | %[2]s }} | {{ repeat %[1]d " " | print .File | trunc %[1]d | %[2]s }} |`, maxColumnLen, "")
active = fmt.Sprintf(`▸ {{ repeat %[1]d " " | print .Context | trunc %[1]d | %[2]s }} | {{ repeat %[1]d " " | print .Cluster | trunc %[1]d | %[2]s }} | {{ repeat %[1]d " " | print .File | trunc %[1]d | %[2]s }} |`, maxColumnLen, "bold | cyan")
label = fmt.Sprint(" Context" + strings.Repeat(" ", maxColumnLen-7) + " | " + "Cluster" + strings.Repeat(" ", maxColumnLen-7) + " | " + "File" + strings.Repeat(" ", maxColumnLen-4) + " ") // repeat = trunc - length of the word before it

if !showAll {
inactive = fmt.Sprintf(` {{ repeat %[1]d " " | print .Context | %[2]s }}`, 0, "")
active = fmt.Sprintf(`▸ {{ repeat %[1]d " " | print .Context | %[2]s }}`, 0, "bold | cyan")
label = " Context "
}
return inactive, active, label, fmap
}

Expand All @@ -73,4 +87,3 @@ func trunc(len int, str string) string {
func repeat(count int, str string) string {
return strings.Repeat(str, count)
}

36 changes: 33 additions & 3 deletions prompt/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func TestFuzzyFilterKonf(t *testing.T) {

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
res := FuzzyFilterKonf(tc.search, tc.item)
res := FuzzyFilterKonf(tc.search, tc.item, false)
if res != tc.expRes {
t.Errorf("Exp res to be %t got %t", tc.expRes, res)
}
})
}
}

func TestPrepareTemplates(t *testing.T) {
func TestPrepareTemplatesFull(t *testing.T) {
tt := map[string]struct {
Values store.Metadata
Trunc int
Expand Down Expand Up @@ -103,7 +103,37 @@ func TestPrepareTemplates(t *testing.T) {

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
inactive, active, label, fmap := NewTableOutputTemplates(tc.Trunc)
inactive, active, label, fmap := NewTableOutputTemplates(tc.Trunc, false)

checkTemplate(t, inactive, tc.Values, tc.ExpInactive, fmap)
checkTemplate(t, active, tc.Values, tc.ExpActive, fmap)
checkTemplate(t, label, tc.Values, tc.ExpLabel, fmap)
})
}
}

func TestPrepareTemplatesClusterOnly(t *testing.T) {
tt := map[string]struct {
Values store.Metadata
Trunc int
ExpInactive string
ExpActive string
ExpLabel string
}{
"values": {
store.Metadata{
Context: "0123456789",
},
10,
" 0123456789",
"▸ 0123456789",
" Context ",
},
}

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
inactive, active, label, fmap := NewTableOutputTemplates(tc.Trunc, true)

checkTemplate(t, inactive, tc.Values, tc.ExpInactive, fmap)
checkTemplate(t, active, tc.Values, tc.ExpActive, fmap)
Expand Down