Query piped nodes for -R/-F with -C; count input only for bare -C
This commit is contained in:
@@ -357,15 +357,32 @@ func run(cfg config, nodeName, factName, match string, showRole, partial, invers
|
||||
return nil
|
||||
}
|
||||
|
||||
if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" {
|
||||
var reader *bufio.Reader
|
||||
piped := false
|
||||
if nodeName == "" {
|
||||
reader, piped = stdinReader(os.Stdin)
|
||||
}
|
||||
// Bare -C on piped input counts the already-looked-up lines instead of querying.
|
||||
passthrough := count && piped && !showRole && factName == ""
|
||||
|
||||
if (nodeOnly || valueOnly || (count && !passthrough) || ansible) && !showRole && factName == "" {
|
||||
return fmt.Errorf("-R or -F must be used with -1, -2, -C, or -A")
|
||||
}
|
||||
if (match != "" || partial || inverse) && !showRole && factName == "" {
|
||||
return fmt.Errorf("-R or -F must be used with -m, -p, or -i")
|
||||
}
|
||||
|
||||
if passthrough {
|
||||
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 +394,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 +421,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