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
+3 -2
View File
@@ -29,7 +29,7 @@ Requires Go 1.21+. Dependencies: `github.com/spf13/cobra` (CLI), `gopkg.in/yaml.
./node-lookup -n <hostname> # lookup a specific node
./node-lookup -F <fact_name> # filter by fact name
./node-lookup -m <value> # exact value match
./node-lookup --pm <pattern> # partial/regex match on value
./node-lookup -p <pattern> # partial/regex match on value (also --pm)
./node-lookup -R -1 # node names only
./node-lookup -R -2 # values only
./node-lookup -R -C # count occurrences
@@ -94,6 +94,7 @@ No test suite exists. Manual testing requires access to the Consul/PuppetDB envi
## Gotchas
- `-1`, `-2`, `-C`, and `-A` all require `-R` or `-F`; the tool exits with an error otherwise.
- `-C` (count) with stdin reads all lines as pre-fetched `"node value"` output for counting — it does **not** query PuppetDB per line.
- `-C` (count) with stdin extracts the first field of each line as the node name, queries PuppetDB per node, then counts the resulting values.
- JSON output (`-j`) builds `{ hostname: { factname: value } }` where the fact key is the `-F` value, the `role_fact` config value (if `-R`), or `"value"` as fallback.
- `config init` fails if the config file already exists (will not overwrite).
- `--pm` has shorthand `-p`. Use `-p <pattern>` or `--pm <pattern>` — not `-pm <pattern>` (pflag parses single-dash multi-char as combined shorthands).
+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")
+6 -6
View File
@@ -36,42 +36,42 @@ func rawJSON(v interface{}) json.RawMessage {
// ---- buildQuery -------------------------------------------------------------
func TestBuildQuery_NoFilters(t *testing.T) {
q := buildQuery("", "", "", "", "enc_role", false)
q := buildQuery("", "", "", "", "", "enc_role", false)
if !strings.Contains(q, "enc_role") {
t.Fatalf("expected default role query, got %s", q)
}
}
func TestBuildQuery_Node(t *testing.T) {
q := buildQuery("host1", "", "", "", "enc_role", false)
q := buildQuery("host1", "", "", "", "", "enc_role", false)
if !strings.Contains(q, "certname") || !strings.Contains(q, "host1") {
t.Fatalf("unexpected query: %s", q)
}
}
func TestBuildQuery_FactAndMatch(t *testing.T) {
q := buildQuery("", "region", "syd1", "", "enc_role", false)
q := buildQuery("", "region", "syd1", "", "", "enc_role", false)
if !strings.Contains(q, "region") || !strings.Contains(q, "syd1") {
t.Fatalf("unexpected query: %s", q)
}
}
func TestBuildQuery_PartialMatch(t *testing.T) {
q := buildQuery("", "enc_role", "", "dns", "enc_role", false)
q := buildQuery("", "enc_role", "", "dns", "", "enc_role", false)
if !strings.Contains(q, "~") || !strings.Contains(q, "dns") {
t.Fatalf("expected partial match query, got %s", q)
}
}
func TestBuildQuery_ShowRole(t *testing.T) {
q := buildQuery("", "", "", "", "my_role_fact", true)
q := buildQuery("", "", "", "", "", "my_role_fact", true)
if !strings.Contains(q, "my_role_fact") {
t.Fatalf("expected role fact in query, got %s", q)
}
}
func TestBuildQuery_CustomRoleFact(t *testing.T) {
q := buildQuery("", "", "", "", "custom_role", true)
q := buildQuery("", "", "", "", "", "custom_role", true)
if !strings.Contains(q, "custom_role") {
t.Fatalf("expected custom role fact, got %s", q)
}