diff --git a/main.go b/main.go index 652207a..234febc 100644 --- a/main.go +++ b/main.go @@ -213,14 +213,34 @@ 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 allFactsForNode(puppetDBURL, node string) ([]fact, error) { + query, _ := json.Marshal([]interface{}{"=", "certname", node}) + return queryPuppetDB(puppetDBURL, string(query)) +} + +func run(cfg config, nodeName, factName, match, partialMatch string, showRole, nodeOnly, valueOnly, count, ansible, jsonMode, allFacts bool) error { signal.Ignore(syscall.SIGPIPE) + if allFacts { + if nodeName == "" { + return fmt.Errorf("-a requires -n") + } + facts, err := allFactsForNode(cfg.PuppetDBURL, nodeName) + if err != nil { + return err + } + sort.Slice(facts, func(i, j int) bool { return facts[i].Name < facts[j].Name }) + for _, f := range facts { + fmt.Printf("%-40s %s\n", f.Name, valueString(f.Value)) + } + return nil + } + if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" { return fmt.Errorf("-R or -F must be used with -1, -2, -C, or -A") } - var allFacts []fact + var collected []fact var stdinLines []string doQuery := func(node string) error { @@ -229,7 +249,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n if err != nil { return err } - allFacts = append(allFacts, facts...) + collected = append(collected, facts...) return nil } @@ -256,12 +276,12 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n } } - returnData := processResults(allFacts) + returnData := processResults(collected) switch { case jsonMode: hostFactMap := map[string]map[string]interface{}{} - for _, f := range allFacts { + for _, f := range collected { if _, ok := hostFactMap[f.Certname]; !ok { hostFactMap[f.Certname] = map[string]interface{}{} } @@ -337,6 +357,7 @@ func main() { count bool ansible bool jsonMode bool + allFacts bool puppetDBURL string ) @@ -350,7 +371,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, showRole, nodeOnly, valueOnly, count, ansible, jsonMode, allFacts) }, SilenceUsage: true, } @@ -366,6 +387,7 @@ func main() { f.BoolVarP(&count, "count", "C", false, "Count fact occurrences") f.BoolVarP(&ansible, "ansible", "A", false, "Output as Ansible inventory") f.BoolVarP(&jsonMode, "json", "j", false, "Emit valid JSON for all output") + f.BoolVarP(&allFacts, "all", "a", false, "Show all facts for a node (requires -n)") rootCmd.PersistentFlags().StringVar(&puppetDBURL, "url", cfg.PuppetDBURL, "PuppetDB facts URL (overrides config and NODE_LOOKUP_URL)") configCmd := &cobra.Command{