Fix -pm <value> parsing and add comma-separated multi-fact -F #14

Merged
benvin merged 3 commits from benvin/positional-match-value into main 2026-07-05 20:05:46 +10:00
Owner

Two related query-ergonomics fixes surfaced while using the tool interactively.

1. -pm <value> failed with unknown command "k8s"

pflag does not attach a space-separated value to a string flag (-m) grouped with a bool flag (-p), so k8s was left as a stray positional. Only -pm=k8s or the un-grouped -p -m k8s worked.

  • Allow one positional argument (cobra.MaximumNArgs(1)) and fall back to it for the match value when -m is empty (matchValue()). -pm/-im/-ipm <value> and a bare -p <value> now all work; -m still wins when both are given.

2. -F a,b (multiple facts) returned {}

-jF ipaddress,enc_role queried a single fact literally named ipaddress,enc_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.

Both paths have unit tests; verified live: node-lookup -R -pm externaldns | node-lookup -jF ipaddress,enc_role returns both facts per host. AGENTS.md updated.

Two related query-ergonomics fixes surfaced while using the tool interactively. ## 1. `-pm <value>` failed with `unknown command "k8s"` pflag does not attach a space-separated value to a string flag (`-m`) grouped with a bool flag (`-p`), so `k8s` was left as a stray positional. Only `-pm=k8s` or the un-grouped `-p -m k8s` worked. - Allow one positional argument (`cobra.MaximumNArgs(1)`) and fall back to it for the match value when `-m` is empty (`matchValue()`). `-pm/-im/-ipm <value>` and a bare `-p <value>` now all work; `-m` still wins when both are given. ## 2. `-F a,b` (multiple facts) returned `{}` `-jF ipaddress,enc_role` queried a single fact literally named `ipaddress,enc_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. Both paths have unit tests; verified live: `node-lookup -R -pm externaldns | node-lookup -jF ipaddress,enc_role` returns both facts per host. AGENTS.md updated.
unkinben added 1 commit 2026-07-05 17:02:48 +10:00
Accept match value positionally so -pm <value> works
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
8696097a6a
pflag does not attach a space-separated value to a string flag that is grouped
with a bool flag, so `node-lookup -R -pm k8s` parsed -p and left `k8s` as a
stray positional, failing with "unknown command k8s". Only `-pm=k8s` or the
un-grouped `-p -m k8s` worked, which is surprising.

- Allow one positional argument (cobra.MaximumNArgs(1)) and fall back to it for
  the match value when -m is empty (matchValue()), so -pm/-im/-ipm <value> and
  a bare `-p <value>` all work. -m still wins when both are given.
- Add matchValue unit tests.
- Document the positional value and the pflag grouping quirk in AGENTS.md.
unkinben added 1 commit 2026-07-05 17:16:10 +10:00
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
103ebb2393
`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).
unkinben changed title from Accept match value positionally so -pm <value> works to Fix -pm <value> parsing and add comma-separated multi-fact -F 2026-07-05 17:16:29 +10:00
unkinben added 1 commit 2026-07-05 17:39:55 +10:00
Include queried facts as host vars in -A inventory
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
5d0f69483b
`-A` emitted hosts with empty vars, so `-F ipaddress,enc_role -A` lost the facts
it just queried. Ansible inventories are far more useful with the values inline.

- Extract factsByHost() (the {host: {fact: value}} builder) and share it between
  -j and -A so the Ansible inventory attaches each host's queried fact(s) as
  host vars, keyed by real fact name.
- Strengthen the Ansible test to assert host vars and add a multi-fact case.
benvin merged commit 5982d257d5 into main 2026-07-05 20:05:46 +10:00
benvin deleted branch benvin/positional-match-value 2026-07-05 20:05:47 +10:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: unkin/node-lookup#14