From 5b695e49280485f0600d4c232a81e67b66ddaabb Mon Sep 17 00:00:00 2001 From: Oliver Isaac Date: Sun, 30 Jan 2022 22:42:43 -0600 Subject: [PATCH] mvoed the args we copy into the function arguments so the func is easier to use long term --- koi/events.go | 17 ++++++++++++++++- koi/helpers.go | 23 +++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/koi/events.go b/koi/events.go index 277e290..7fd98fe 100644 --- a/koi/events.go +++ b/koi/events.go @@ -9,12 +9,27 @@ func EventsCommand(exe string, args []string) (exitCode int, runError error) { "--sort-by=.metadata.creationTimestamp", } - cmdArg = copyImportantArgsIntoNewArgs(args, cmdArg) + cmdArg = copyArgsIntoNewArgs(args, + cmdArg, + [][]string{ + {"-n", "--namespace"}, + {"-o", "--ouptut"}, + {"--context"}, + }, + [][]string{ + {"--all-namespaces"}, + }, + ) filterRegex := regexp.MustCompile(`\bNormal\b`) filter := func(line string) (string, bool) { return line, !filterRegex.MatchString(line) } + // If the output is set, then we don't want to do any filtering + if val := extractValueArgumentFromArgs(cmdArg, "-o", "--output"); val != "" { + filter = nil + } + return runCommandAndFilterOutput(exe, cmdArg, filter) } diff --git a/koi/helpers.go b/koi/helpers.go index 821a08c..868b18b 100644 --- a/koi/helpers.go +++ b/koi/helpers.go @@ -51,23 +51,14 @@ func extractBoolArgumentFromArgs(args []string, argumentFlags ...string) bool { return false } -func copyImportantArgsIntoNewArgs(oldArgs, newArgs []string) []string { - importantValueArgs := [][]string{ - {"-n", "--namespace"}, - {"--context"}, - } - - importantBoolArgs := [][]string{ - {"--all-namespaces"}, - } - - for _, ia := range importantValueArgs { +func copyArgsIntoNewArgs(oldArgs, newArgs []string, copyValueArgs, copyBoolArgs [][]string) []string { + for _, ia := range copyValueArgs { val := extractValueArgumentFromArgs(oldArgs, ia...) if val != "" { newArgs = appendArgument(newArgs, ia[0], val) } } - for _, ia := range importantBoolArgs { + for _, ia := range copyBoolArgs { val := extractBoolArgumentFromArgs(oldArgs, ia...) if val { newArgs = appendArgument(newArgs, ia[0], "") @@ -118,12 +109,12 @@ func runCommandAndFilterOutput(exe string, args []string, lineFilter ...func(lin log.Trace("Start scanning") for scanner.Scan() { line := scanner.Text() - log.Tracef("Read line: %s", line[0:20]) - printLine := true for _, filter := range lineFilter { - if line, printLine = filter(line); !printLine { - break + if filter != nil { + if line, printLine = filter(line); !printLine { + break + } } } if printLine {