Support comma-separated -F for querying multiple facts
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful

`node-lookup -jF ipaddress,enc_role` returned `{}` because it queried a single
fact literally named "ipaddress,enc_role". Requesting several facts per host is
a natural need (e.g. pairing an address with its role).

- Split -F on commas (splitFactNames) and match any of them via an "or" over
  ["=","name",<n>] clauses (nameFilter); a single name keeps the plain "="
  form.
- Key JSON output by each result's real fact name so all requested facts appear
  under the host (previously keyed by the raw -F string).
- Update the -F flag help and add unit tests (split, single vs multi query
  shape, multi-fact JSON).
This commit is contained in:
2026-07-05 17:16:07 +10:00
parent 8696097a6a
commit 103ebb2393
3 changed files with 113 additions and 9 deletions
+38 -8
View File
@@ -110,6 +110,31 @@ type fact struct {
Value json.RawMessage `json:"value"`
}
// splitFactNames splits a comma-separated -F value into trimmed, non-empty
// names, so `-F ipaddress,enc_role` queries both facts.
func splitFactNames(factName string) []string {
var names []string
for _, n := range strings.Split(factName, ",") {
if n = strings.TrimSpace(n); n != "" {
names = append(names, n)
}
}
return names
}
// nameFilter returns a PQL filter matching any of the given fact names: a plain
// equality for one name, an "or" over per-name equalities for several.
func nameFilter(names []string) []interface{} {
if len(names) == 1 {
return []interface{}{"=", "name", names[0]}
}
or := []interface{}{"or"}
for _, n := range names {
or = append(or, []interface{}{"=", "name", n})
}
return or
}
func buildQuery(node, factName, match, roleFact string, showRole, partial, inverse bool) string {
type filter = []interface{}
var filters []filter
@@ -117,8 +142,8 @@ func buildQuery(node, factName, match, roleFact string, showRole, partial, inver
if node != "" {
filters = append(filters, filter{"=", "certname", node})
}
if factName != "" {
filters = append(filters, filter{"=", "name", factName})
if names := splitFactNames(factName); len(names) > 0 {
filters = append(filters, nameFilter(names))
} else if showRole {
filters = append(filters, filter{"=", "name", roleFact})
}
@@ -326,12 +351,17 @@ func run(cfg config, nodeName, factName, match string, showRole, partial, invers
if _, ok := hostFactMap[f.Certname]; !ok {
hostFactMap[f.Certname] = map[string]interface{}{}
}
key := factName
// Key by the fact's actual name so multiple -F facts (e.g.
// ipaddress,enc_role) each appear under the host. Fall back to the
// computed key only if the result carries no name.
key := f.Name
if key == "" {
if showRole {
key = cfg.RoleFact
} else {
key = "value"
if key = factName; key == "" {
if showRole {
key = cfg.RoleFact
} else {
key = "value"
}
}
}
hostFactMap[f.Certname][key] = valueAny(f.Value)
@@ -426,7 +456,7 @@ func main() {
f := rootCmd.Flags()
f.StringVarP(&nodeName, "node", "n", "", "Node name")
f.StringVarP(&factName, "fact", "F", "", "Fact name")
f.StringVarP(&factName, "fact", "F", "", "Fact name (comma-separated for several, e.g. -F ipaddress,enc_role)")
f.BoolVarP(&showRole, "role", "R", false, "Show role fact ("+defaultRoleFact+" by default)")
f.StringVarP(&match, "match", "m", "", "Value to match (use with -p and/or -i)")
f.BoolVarP(&partial, "partial", "p", false, "Partial/regex match modifier (combine with -m)")