Skip to content

Commit

Permalink
mvoed the args we copy into the function arguments so the func is eas…
Browse files Browse the repository at this point in the history
…ier to use long term
  • Loading branch information
oliverisaac committed Jan 31, 2022
1 parent 9a106d8 commit 5b695e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
17 changes: 16 additions & 1 deletion koi/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
23 changes: 7 additions & 16 deletions koi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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], "")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5b695e4

Please sign in to comment.