From d98ad65d149b8036ccbec89bfb8ce84302dc1fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 6 May 2024 08:28:05 +0200 Subject: [PATCH 1/2] Perf improvement: process '-has' flag at the end The '--has' flag is costly because it forces variables to be merged. When looping over a lot of catalog items, it can slow down the command a lot. Move its processing at the last position when filtering catalog items, so it's executed less often when used in combination of other flags. --- cli/agnosticv.go | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/cli/agnosticv.go b/cli/agnosticv.go index 16d9b25..bc6702a 100644 --- a/cli/agnosticv.go +++ b/cli/agnosticv.go @@ -424,33 +424,6 @@ func findCatalogItems(workdir string, hasFlags []string, relatedFlags []string, return nil } - if len(hasFlags) > 0 { - logDebug.Println("hasFlags", hasFlags) - // Here we need yaml.v3 in order to use jmespath - merged, _, err := mergeVars(p, mergeStrategies) - if err != nil { - // Print the error and move to next file - logErr.Println(err) - return nil - } - - for _, hasFlag := range hasFlags { - r, err := jmespath.Search(hasFlag, merged) - if err != nil { - logErr.Printf("ERROR: JMESPath '%q' not correct, %v", hasFlag, err) - return err - } - - logDebug.Printf("merged=%#v\n", merged) - logDebug.Printf("r=%#v\n", r) - - // If JMESPath expression does not match, skip file - if r == nil || r == false { - return nil - } - } - } - if len(relatedFlags) > 0 || len(orRelatedFlags) > 0 { mergeList, err := getMergeList(pAbs) related := extendMergeListWithRelated(pAbs, mergeList) @@ -499,6 +472,33 @@ func findCatalogItems(workdir string, hasFlags []string, relatedFlags []string, } } + if len(hasFlags) > 0 { + logDebug.Println("hasFlags", hasFlags) + // Here we need yaml.v3 in order to use jmespath + merged, _, err := mergeVars(p, mergeStrategies) + if err != nil { + // Print the error and move to next file + logErr.Println(err) + return nil + } + + for _, hasFlag := range hasFlags { + r, err := jmespath.Search(hasFlag, merged) + if err != nil { + logErr.Printf("ERROR: JMESPath '%q' not correct, %v", hasFlag, err) + return err + } + + logDebug.Printf("merged=%#v\n", merged) + logDebug.Printf("r=%#v\n", r) + + // If JMESPath expression does not match, skip file + if r == nil || r == false { + return nil + } + } + } + result = append(result, p) return nil }) From a5d742472b18393090b0144c8b7839733794bb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 6 May 2024 11:51:22 +0200 Subject: [PATCH 2/2] Fix tests --- cli/agnosticv.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cli/agnosticv.go b/cli/agnosticv.go index bc6702a..acc0464 100644 --- a/cli/agnosticv.go +++ b/cli/agnosticv.go @@ -446,6 +446,32 @@ func findCatalogItems(workdir string, hasFlags []string, relatedFlags []string, } if containsPath(related, orRelatedAbs) { + if len(hasFlags) > 0 { + logDebug.Println("hasFlags", hasFlags) + // Here we need yaml.v3 in order to use jmespath + merged, _, err := mergeVars(p, mergeStrategies) + if err != nil { + // Print the error and move to next file + logErr.Println(err) + return nil + } + + for _, hasFlag := range hasFlags { + r, err := jmespath.Search(hasFlag, merged) + if err != nil { + logErr.Printf("ERROR: JMESPath '%q' not correct, %v", hasFlag, err) + return err + } + + logDebug.Printf("merged=%#v\n", merged) + logDebug.Printf("r=%#v\n", r) + + // If JMESPath expression does not match, skip file + if r == nil || r == false { + return nil + } + } + } // Add catalog item to result result = append(result, p) return nil