From c832db9ddbad0cbb4a1c1f16c9329a9e6dcb78a6 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 20 Aug 2019 16:39:15 -0700 Subject: [PATCH] Add utils.Includes() helper function --- modules/kubernetes/widget.go | 12 ++----- utils/utils.go | 27 ++++++++++++---- utils/utils_test.go | 63 ++++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/modules/kubernetes/widget.go b/modules/kubernetes/widget.go index a05e6990..198ba1f4 100644 --- a/modules/kubernetes/widget.go +++ b/modules/kubernetes/widget.go @@ -40,18 +40,12 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget { // Refresh executes the command and updates the view with the results func (widget *Widget) Refresh() { - title := widget.generateTitle() - client := widget.getInstance() var content string - // Debug Info - //content += fmt.Sprintf("Namespaces: %q %d\n", widget.namespaces, len(widget.namespaces)) - //content += fmt.Sprintf("Objects: %q %d\n\n", widget.objects, len(widget.objects)) - - if !utils.DoesNotInclude(widget.objects, "nodes") { + if utils.Includes(widget.objects, "nodes") { nodeList, nodeError := client.getNodes() if nodeError != nil { widget.Redraw(title, "[red] Error getting node data [white]\n", true) @@ -64,7 +58,7 @@ func (widget *Widget) Refresh() { content += "\n" } - if !utils.DoesNotInclude(widget.objects, "deployments") { + if utils.Includes(widget.objects, "deployments") { deploymentList, deploymentError := client.getDeployments(widget.namespaces) if deploymentError != nil { widget.Redraw(title, "[red] Error getting deployment data [white]\n", true) @@ -77,7 +71,7 @@ func (widget *Widget) Refresh() { content += "\n" } - if !utils.DoesNotInclude(widget.objects, "pods") { + if utils.Includes(widget.objects, "pods") { podList, podError := client.getPods(widget.namespaces) if podError != nil { widget.Redraw(title, "[red] Error getting pod data [white]\n", false) diff --git a/utils/utils.go b/utils/utils.go index 3bd4fe71..8e4878df 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -33,12 +33,7 @@ const ( // > true // func DoesNotInclude(strs []string, val string) bool { - for _, str := range strs { - if val == str { - return false - } - } - return true + return !Includes(strs, val) } // ExecuteCommand executes an external command on the local machine as the current user @@ -76,6 +71,26 @@ func FindMatch(pattern string, data string) [][]string { return r.FindAllStringSubmatch(data, -1) } +// Includes takes a slice of strings and a target string and returns +// TRUE if the slice includes the target, FALSE if it does not +// +// Example: +// +// x := Includes([]string{"cat", "dog", "rat"}, "dog") +// > true +// +// x := Includes([]string{"cat", "dog", "rat"}, "pig") +// > false +// +func Includes(strs []string, val string) bool { + for _, str := range strs { + if val == str { + return true + } + } + return false +} + // OpenFile opens the file defined in `path` via the operating system func OpenFile(path string) { if (strings.HasPrefix(path, "http://")) || (strings.HasPrefix(path, "https://")) { diff --git a/utils/utils_test.go b/utils/utils_test.go index 06191de8..e396cc4e 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -9,8 +9,35 @@ import ( ) func Test_DoesNotInclude(t *testing.T) { - Equal(t, true, DoesNotInclude([]string{"cat", "dog", "rat"}, "bat")) - Equal(t, false, DoesNotInclude([]string{"cat", "dog", "rat"}, "dog")) + tests := []struct { + name string + strs []string + val string + expected bool + }{ + { + name: "when included", + strs: []string{"a", "b", "c"}, + val: "b", + expected: false, + }, + { + name: "when not included", + strs: []string{"a", "b", "c"}, + val: "f", + expected: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := DoesNotInclude(tt.strs, tt.val) + + if tt.expected != actual { + t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) + } + }) + } } func Test_ExecuteCommand(t *testing.T) { @@ -50,6 +77,38 @@ func Test_FindMatch(t *testing.T) { Equal(t, expected, result) } +func Test_Includes(t *testing.T) { + tests := []struct { + name string + strs []string + val string + expected bool + }{ + { + name: "when included", + strs: []string{"a", "b", "c"}, + val: "b", + expected: true, + }, + { + name: "when not included", + strs: []string{"a", "b", "c"}, + val: "f", + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := Includes(tt.strs, tt.val) + + if tt.expected != actual { + t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) + } + }) + } +} + func Test_ReadFileBytes(t *testing.T) { tests := []struct { name string