mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add utils.Includes() helper function
This commit is contained in:
parent
faf365baa1
commit
c832db9ddb
@ -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)
|
||||
|
@ -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://")) {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user