From 9f3e66fe463112d612542fcf94d0eba81740bb53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Blaise?= Date: Tue, 2 Feb 2021 08:23:59 +0100 Subject: [PATCH] Wip multi args completion and tests --- cmd/shared.go | 23 +++++++++++---- cmd/shared_test.go | 54 +++++++++++++++++++++++++++++++++++ internal/utils/collections.go | 16 +++++++++++ 3 files changed, 88 insertions(+), 5 deletions(-) diff --git a/cmd/shared.go b/cmd/shared.go index 3733409..55ac271 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -10,20 +10,33 @@ import ( "strings" ) +func extractMultitpleCompletion(toComplete string) (remainder string, current string) { + toCompletes := strings.Split(toComplete, ",") + + if len(toCompletes) == 1 { + return utils.EmptyString, toCompletes[0] + } + + remainder = fmt.Sprintf("%s%s", strings.Join(toCompletes[:len(toCompletes)-1], ","), ",") + current = toCompletes[len(toCompletes)-1] + return +} + func filterCompletion(toComplete string, path string) ([]string, cobra.ShellCompDirective) { - // logrus.SetLevel(logrus.PanicLevel) + logrus.SetLevel(logrus.PanicLevel) - key, op, value := ansible.ParseFilter(toComplete) + remainder, current := extractMultitpleCompletion(toComplete) + cobra.CompDebug(fmt.Sprintf("extract muitple: remainder:%s current:%s\n", remainder, current), true) - cobra.CompDebug(fmt.Sprintf("key:%s op:%s value:%s", key, op, value), true) + key, op, value := ansible.ParseFilter(current) + cobra.CompDebug(fmt.Sprintf("parse filter: key:%s op:%s value:%s\n", key, op, value), true) k8s := ansible.Projects.LoadFromPath(path) - availableKeys := k8s.InventoryKeys() blank := key == utils.EmptyString && op == utils.EmptyString && value == utils.EmptyString if blank { - return availableKeys, cobra.ShellCompDirectiveDefault + return utils.AppendPrefixOnSlice(remainder, availableKeys), cobra.ShellCompDirectiveDefault } writingKey := key != utils.EmptyString && op == utils.EmptyString && value == utils.EmptyString diff --git a/cmd/shared_test.go b/cmd/shared_test.go index 90a737d..f7cea8b 100644 --- a/cmd/shared_test.go +++ b/cmd/shared_test.go @@ -12,6 +12,47 @@ const ( ProjectSimpleLevelPath = "../testdata/projectSimpleLevel" ) +func TestExtractMultitpleCompletion(t *testing.T) { + testCases := map[string]struct { + toComplete string + expectsRemainder string + expectsCurrent string + }{"empty string should return empty strings": { + toComplete: "", + expectsRemainder: "", + expectsCurrent: "", + }, "'first' should return no remainder and 'first' as current": { + toComplete: "first", + expectsRemainder: "", + expectsCurrent: "first", + }, "'first,' should return 'first,' as remainder and empty as current": { + toComplete: "first,", + expectsRemainder: "first,", + expectsCurrent: "", + }, "'first,second' should return 'first,' as remainder and second as current": { + toComplete: "first,second", + expectsRemainder: "first,", + expectsCurrent: "second", + }, "'first,second,' should return 'first,second,' as remainder and empty as current": { + toComplete: "first,second,", + expectsRemainder: "first,second,", + expectsCurrent: "", + }, "'first,second,third' should return 'first,second,' as remainder and third as current": { + toComplete: "first,second,third", + expectsRemainder: "first,second,", + expectsCurrent: "third", + }} + + for testName, testCase := range testCases { + t.Run(testName, func(t *testing.T) { + actualRemainder, actualCurrent := extractMultitpleCompletion(testCase.toComplete) + assert.Equal(t, testCase.expectsRemainder, actualRemainder) + assert.Equal(t, testCase.expectsCurrent, actualCurrent) + }) + } + +} + func TestFilterCompletion(t *testing.T) { testCases := map[string]struct { toComplete string @@ -68,10 +109,23 @@ func TestFilterCompletion(t *testing.T) { path: ProjectMultiLevelPath, expect: nil, }, + "multi-level with 'customer==customer1,' should return to complete": { + toComplete: "customer==customer1", + path: ProjectMultiLevelPath, + expect: []string{"customer==customer1"}, + }, + "multi-level with 'customer==customer1,' should return all vars": { + toComplete: "customer==customer1,", + path: ProjectMultiLevelPath, + expect: []string{"customer", "env", "os", "platform"}, + }, } for testName, testCase := range testCases { t.Run(testName, func(t *testing.T) { + + fmt.Println() + actual, _ := filterCompletion(testCase.toComplete, testCase.path) assert.Equal(t, testCase.expect, actual) }) diff --git a/internal/utils/collections.go b/internal/utils/collections.go index 95ae1af..edeb46b 100644 --- a/internal/utils/collections.go +++ b/internal/utils/collections.go @@ -1,5 +1,7 @@ package utils +import "fmt" + // Index returns the first index of the target string `t`, or // -1 if no match is found. func Index(vs []string, t string) int { @@ -74,3 +76,17 @@ func MapHasAllTrue(a map[string]bool) bool { return true } + +func AppendPrefixOnSlice(prefix string, slice []string) (result []string) { + for _, s := range slice { + result = append(result, fmt.Sprintf("%s%s", prefix, s)) + } + return +} + +func AppendSuffixOnSlice(suffix string, slice []string) (result []string) { + for _, s := range slice { + result = append(result, fmt.Sprintf("%s%s", s, suffix)) + } + return +}