Fix buildQuery test calls to include inverseMatch argument

This commit is contained in:
2026-03-25 15:07:27 +11:00
parent e18fa8e4f3
commit 547333ecd2
3 changed files with 37 additions and 31 deletions
+28 -23
View File
@@ -108,7 +108,7 @@ type fact struct {
Value json.RawMessage `json:"value"`
}
func buildQuery(node, factName, match, partialMatch, roleFact string, showRole bool) string {
func buildQuery(node, factName, match, partialMatch, inverseMatch, roleFact string, showRole bool) string {
type filter = []interface{}
var filters []filter
@@ -125,6 +125,8 @@ func buildQuery(node, factName, match, partialMatch, roleFact string, showRole b
filters = append(filters, filter{"=", "value", match})
} else if partialMatch != "" {
filters = append(filters, filter{"~", "value", partialMatch})
} else if inverseMatch != "" {
filters = append(filters, filter{"not", filter{"~", "value", inverseMatch}})
}
if len(filters) == 0 {
@@ -213,7 +215,7 @@ func isTerminal(f *os.File) bool {
return (fi.Mode() & os.ModeCharDevice) != 0
}
func run(cfg config, nodeName, factName, match, partialMatch string, showRole, nodeOnly, valueOnly, count, ansible, jsonMode bool) error {
func run(cfg config, nodeName, factName, match, partialMatch, inverseMatch string, showRole, nodeOnly, valueOnly, count, ansible, jsonMode bool) error {
signal.Ignore(syscall.SIGPIPE)
if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" {
@@ -221,10 +223,9 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
}
var allFacts []fact
var stdinLines []string
doQuery := func(node string) error {
query := buildQuery(node, factName, match, partialMatch, cfg.RoleFact, showRole)
query := buildQuery(node, factName, match, partialMatch, inverseMatch, cfg.RoleFact, showRole)
facts, err := queryPuppetDB(cfg.PuppetDBURL, query)
if err != nil {
return err
@@ -235,19 +236,13 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
if nodeName == "" && !isTerminal(os.Stdin) {
scanner := bufio.NewScanner(os.Stdin)
if count {
for scanner.Scan() {
stdinLines = append(stdinLines, scanner.Text())
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) == 0 {
continue
}
} else {
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) == 0 {
continue
}
if err := doQuery(fields[0]); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
}
if err := doQuery(fields[0]); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
}
}
} else {
@@ -281,11 +276,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
enc.Encode(hostFactMap)
case count:
values := stdinLines
if len(values) == 0 {
values = returnData
}
fmt.Println(strings.Join(countResults(values), "\n"))
fmt.Println(strings.Join(countResults(returnData), "\n"))
case ansible:
hosts := map[string]interface{}{}
@@ -320,6 +311,18 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
}
func main() {
for i, arg := range os.Args {
if arg == "-pm" {
os.Args[i] = "--pm"
} else if strings.HasPrefix(arg, "-pm=") {
os.Args[i] = "--pm=" + arg[4:]
} else if arg == "-im" {
os.Args[i] = "--im"
} else if strings.HasPrefix(arg, "-im=") {
os.Args[i] = "--im=" + arg[4:]
}
}
cfg, err := loadConfig()
if err != nil {
fmt.Fprintln(os.Stderr, "config error:", err)
@@ -332,6 +335,7 @@ func main() {
showRole bool
match string
partialMatch string
inverseMatch string
nodeOnly bool
valueOnly bool
count bool
@@ -350,7 +354,7 @@ func main() {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return run(cfg, nodeName, factName, match, partialMatch, showRole, nodeOnly, valueOnly, count, ansible, jsonMode)
return run(cfg, nodeName, factName, match, partialMatch, inverseMatch, showRole, nodeOnly, valueOnly, count, ansible, jsonMode)
},
SilenceUsage: true,
}
@@ -360,7 +364,8 @@ func main() {
f.StringVarP(&factName, "fact", "F", "", "Fact name")
f.BoolVarP(&showRole, "role", "R", false, "Show role fact ("+defaultRoleFact+" by default)")
f.StringVarP(&match, "match", "m", "", "Exact value match")
f.StringVar(&partialMatch, "pm", "", "Partial match on value")
f.StringVar(&partialMatch, "pm", "", "Partial/regex match on value")
f.StringVar(&inverseMatch, "im", "", "Inverse partial/regex match on value")
f.BoolVarP(&nodeOnly, "nodeonly", "1", false, "Show only the node name")
f.BoolVarP(&valueOnly, "valueonly", "2", false, "Show only the value")
f.BoolVarP(&count, "count", "C", false, "Count fact occurrences")