Query piped nodes when counting with -R/-F (#22)
ci/woodpecker/tag/release Pipeline was successful
ci/woodpecker/tag/release Pipeline was successful
With piped input, `-C` skipped the query and counted the piped lines' second column, so `... | node-lookup -R -C` reported the upstream fact instead of each node's role. - query each piped node for `-R`/`-F` with `-C` and count the results - count piped lines directly only for bare `-C` with piped input; `-C` with `-j`/`-1`/`-2` still requires `-R`/`-F` - validate flags before reading stdin so invalid combos never block on an open pipe - add tests for piped count paths, rejected combos and validation ordering Reviewed-on: #22 Co-authored-by: unkin-agent <unkin-agent@unkin.net> Co-committed-by: unkin-agent <unkin-agent@unkin.net>
This commit was merged in pull request #22.
This commit is contained in:
@@ -357,15 +357,38 @@ func run(cfg config, nodeName, factName, match string, showRole, partial, invers
|
||||
return nil
|
||||
}
|
||||
|
||||
if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" {
|
||||
return fmt.Errorf("-R or -F must be used with -1, -2, -C, or -A")
|
||||
hasQuery := showRole || factName != ""
|
||||
// Bare -C counts already-looked-up piped lines; only it needs stdin to validate.
|
||||
bareCount := count && !hasQuery && !nodeOnly && !valueOnly && !ansible && !jsonMode && nodeName == ""
|
||||
countErr := fmt.Errorf("-R or -F must be used with -1, -2, -C, or -A")
|
||||
|
||||
if (nodeOnly || valueOnly || (count && !bareCount) || ansible) && !hasQuery {
|
||||
return countErr
|
||||
}
|
||||
if (match != "" || partial || inverse) && !showRole && factName == "" {
|
||||
if (match != "" || partial || inverse) && !hasQuery {
|
||||
return fmt.Errorf("-R or -F must be used with -m, -p, or -i")
|
||||
}
|
||||
|
||||
var reader *bufio.Reader
|
||||
piped := false
|
||||
if nodeName == "" {
|
||||
reader, piped = stdinReader(os.Stdin)
|
||||
}
|
||||
|
||||
if bareCount {
|
||||
if !piped {
|
||||
return countErr
|
||||
}
|
||||
var lines []string
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
fmt.Println(strings.Join(countResults(lines), "\n"))
|
||||
return nil
|
||||
}
|
||||
|
||||
var collected []fact
|
||||
var stdinLines []string
|
||||
|
||||
doQuery := func(node string) error {
|
||||
query := buildQuery(node, factName, match, cfg.RoleFact, showRole, partial, inverse)
|
||||
@@ -377,21 +400,15 @@ func run(cfg config, nodeName, factName, match string, showRole, partial, invers
|
||||
return nil
|
||||
}
|
||||
|
||||
if reader, ok := stdinReader(os.Stdin); ok && nodeName == "" {
|
||||
if piped {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
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(qualifyNode(fields[0], cfg.Domain)); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
}
|
||||
if err := doQuery(qualifyNode(fields[0], cfg.Domain)); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -410,11 +427,7 @@ func run(cfg config, nodeName, factName, match string, showRole, partial, invers
|
||||
_ = enc.Encode(factsByHost(collected, factName, cfg.RoleFact, showRole))
|
||||
|
||||
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:
|
||||
// Attach each host's queried fact(s) as inventory host vars, e.g.
|
||||
|
||||
Reference in New Issue
Block a user