Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 547333ecd2 |
@@ -29,7 +29,7 @@ Requires Go 1.21+. Dependencies: `github.com/spf13/cobra` (CLI), `gopkg.in/yaml.
|
|||||||
./node-lookup -n <hostname> # lookup a specific node
|
./node-lookup -n <hostname> # lookup a specific node
|
||||||
./node-lookup -F <fact_name> # filter by fact name
|
./node-lookup -F <fact_name> # filter by fact name
|
||||||
./node-lookup -m <value> # exact value match
|
./node-lookup -m <value> # exact value match
|
||||||
./node-lookup --pm <pattern> # partial/regex match on value
|
./node-lookup -p <pattern> # partial/regex match on value (also --pm)
|
||||||
./node-lookup -R -1 # node names only
|
./node-lookup -R -1 # node names only
|
||||||
./node-lookup -R -2 # values only
|
./node-lookup -R -2 # values only
|
||||||
./node-lookup -R -C # count occurrences
|
./node-lookup -R -C # count occurrences
|
||||||
@@ -94,6 +94,7 @@ No test suite exists. Manual testing requires access to the Consul/PuppetDB envi
|
|||||||
## Gotchas
|
## Gotchas
|
||||||
|
|
||||||
- `-1`, `-2`, `-C`, and `-A` all require `-R` or `-F`; the tool exits with an error otherwise.
|
- `-1`, `-2`, `-C`, and `-A` all require `-R` or `-F`; the tool exits with an error otherwise.
|
||||||
- `-C` (count) with stdin reads all lines as pre-fetched `"node value"` output for counting — it does **not** query PuppetDB per line.
|
- `-C` (count) with stdin extracts the first field of each line as the node name, queries PuppetDB per node, then counts the resulting values.
|
||||||
- JSON output (`-j`) builds `{ hostname: { factname: value } }` where the fact key is the `-F` value, the `role_fact` config value (if `-R`), or `"value"` as fallback.
|
- JSON output (`-j`) builds `{ hostname: { factname: value } }` where the fact key is the `-F` value, the `role_fact` config value (if `-R`), or `"value"` as fallback.
|
||||||
- `config init` fails if the config file already exists (will not overwrite).
|
- `config init` fails if the config file already exists (will not overwrite).
|
||||||
|
- `--pm` has shorthand `-p`. Use `-p <pattern>` or `--pm <pattern>` — not `-pm <pattern>` (pflag parses single-dash multi-char as combined shorthands).
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ type fact struct {
|
|||||||
Value json.RawMessage `json:"value"`
|
Value json.RawMessage `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildQuery(node, factName, match, partialMatch, roleFact string, showRole bool) string {
|
func buildQuery(node, factName, match, partialMatch, inverseMatch, roleFact string, showRole bool) string {
|
||||||
type filter = []interface{}
|
type filter = []interface{}
|
||||||
var filters []filter
|
var filters []filter
|
||||||
|
|
||||||
@@ -125,6 +125,8 @@ func buildQuery(node, factName, match, partialMatch, roleFact string, showRole b
|
|||||||
filters = append(filters, filter{"=", "value", match})
|
filters = append(filters, filter{"=", "value", match})
|
||||||
} else if partialMatch != "" {
|
} else if partialMatch != "" {
|
||||||
filters = append(filters, filter{"~", "value", partialMatch})
|
filters = append(filters, filter{"~", "value", partialMatch})
|
||||||
|
} else if inverseMatch != "" {
|
||||||
|
filters = append(filters, filter{"not", filter{"~", "value", inverseMatch}})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(filters) == 0 {
|
if len(filters) == 0 {
|
||||||
@@ -213,7 +215,7 @@ func isTerminal(f *os.File) bool {
|
|||||||
return (fi.Mode() & os.ModeCharDevice) != 0
|
return (fi.Mode() & os.ModeCharDevice) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(cfg config, nodeName, factName, match, partialMatch string, showRole, nodeOnly, valueOnly, count, ansible, jsonMode bool) error {
|
func run(cfg config, nodeName, factName, match, partialMatch, inverseMatch string, showRole, nodeOnly, valueOnly, count, ansible, jsonMode bool) error {
|
||||||
signal.Ignore(syscall.SIGPIPE)
|
signal.Ignore(syscall.SIGPIPE)
|
||||||
|
|
||||||
if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" {
|
if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" {
|
||||||
@@ -221,10 +223,9 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
|
|||||||
}
|
}
|
||||||
|
|
||||||
var allFacts []fact
|
var allFacts []fact
|
||||||
var stdinLines []string
|
|
||||||
|
|
||||||
doQuery := func(node string) error {
|
doQuery := func(node string) error {
|
||||||
query := buildQuery(node, factName, match, partialMatch, cfg.RoleFact, showRole)
|
query := buildQuery(node, factName, match, partialMatch, inverseMatch, cfg.RoleFact, showRole)
|
||||||
facts, err := queryPuppetDB(cfg.PuppetDBURL, query)
|
facts, err := queryPuppetDB(cfg.PuppetDBURL, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -235,19 +236,13 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
|
|||||||
|
|
||||||
if nodeName == "" && !isTerminal(os.Stdin) {
|
if nodeName == "" && !isTerminal(os.Stdin) {
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
if count {
|
for scanner.Scan() {
|
||||||
for scanner.Scan() {
|
fields := strings.Fields(scanner.Text())
|
||||||
stdinLines = append(stdinLines, scanner.Text())
|
if len(fields) == 0 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
if err := doQuery(fields[0]); err != nil {
|
||||||
for scanner.Scan() {
|
fmt.Fprintln(os.Stderr, "error:", err)
|
||||||
fields := strings.Fields(scanner.Text())
|
|
||||||
if len(fields) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := doQuery(fields[0]); err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "error:", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -281,11 +276,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
|
|||||||
enc.Encode(hostFactMap)
|
enc.Encode(hostFactMap)
|
||||||
|
|
||||||
case count:
|
case count:
|
||||||
values := stdinLines
|
fmt.Println(strings.Join(countResults(returnData), "\n"))
|
||||||
if len(values) == 0 {
|
|
||||||
values = returnData
|
|
||||||
}
|
|
||||||
fmt.Println(strings.Join(countResults(values), "\n"))
|
|
||||||
|
|
||||||
case ansible:
|
case ansible:
|
||||||
hosts := map[string]interface{}{}
|
hosts := map[string]interface{}{}
|
||||||
@@ -320,6 +311,18 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
for i, arg := range os.Args {
|
||||||
|
if arg == "-pm" {
|
||||||
|
os.Args[i] = "--pm"
|
||||||
|
} else if strings.HasPrefix(arg, "-pm=") {
|
||||||
|
os.Args[i] = "--pm=" + arg[4:]
|
||||||
|
} else if arg == "-im" {
|
||||||
|
os.Args[i] = "--im"
|
||||||
|
} else if strings.HasPrefix(arg, "-im=") {
|
||||||
|
os.Args[i] = "--im=" + arg[4:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cfg, err := loadConfig()
|
cfg, err := loadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "config error:", err)
|
fmt.Fprintln(os.Stderr, "config error:", err)
|
||||||
@@ -332,6 +335,7 @@ func main() {
|
|||||||
showRole bool
|
showRole bool
|
||||||
match string
|
match string
|
||||||
partialMatch string
|
partialMatch string
|
||||||
|
inverseMatch string
|
||||||
nodeOnly bool
|
nodeOnly bool
|
||||||
valueOnly bool
|
valueOnly bool
|
||||||
count bool
|
count bool
|
||||||
@@ -350,7 +354,7 @@ func main() {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
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, inverseMatch, showRole, nodeOnly, valueOnly, count, ansible, jsonMode)
|
||||||
},
|
},
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
@@ -360,7 +364,8 @@ func main() {
|
|||||||
f.StringVarP(&factName, "fact", "F", "", "Fact name")
|
f.StringVarP(&factName, "fact", "F", "", "Fact name")
|
||||||
f.BoolVarP(&showRole, "role", "R", false, "Show role fact ("+defaultRoleFact+" by default)")
|
f.BoolVarP(&showRole, "role", "R", false, "Show role fact ("+defaultRoleFact+" by default)")
|
||||||
f.StringVarP(&match, "match", "m", "", "Exact value match")
|
f.StringVarP(&match, "match", "m", "", "Exact value match")
|
||||||
f.StringVar(&partialMatch, "pm", "", "Partial match on value")
|
f.StringVar(&partialMatch, "pm", "", "Partial/regex match on value")
|
||||||
|
f.StringVar(&inverseMatch, "im", "", "Inverse partial/regex match on value")
|
||||||
f.BoolVarP(&nodeOnly, "nodeonly", "1", false, "Show only the node name")
|
f.BoolVarP(&nodeOnly, "nodeonly", "1", false, "Show only the node name")
|
||||||
f.BoolVarP(&valueOnly, "valueonly", "2", false, "Show only the value")
|
f.BoolVarP(&valueOnly, "valueonly", "2", false, "Show only the value")
|
||||||
f.BoolVarP(&count, "count", "C", false, "Count fact occurrences")
|
f.BoolVarP(&count, "count", "C", false, "Count fact occurrences")
|
||||||
|
|||||||
+6
-6
@@ -36,42 +36,42 @@ func rawJSON(v interface{}) json.RawMessage {
|
|||||||
// ---- buildQuery -------------------------------------------------------------
|
// ---- buildQuery -------------------------------------------------------------
|
||||||
|
|
||||||
func TestBuildQuery_NoFilters(t *testing.T) {
|
func TestBuildQuery_NoFilters(t *testing.T) {
|
||||||
q := buildQuery("", "", "", "", "enc_role", false)
|
q := buildQuery("", "", "", "", "", "enc_role", false)
|
||||||
if !strings.Contains(q, "enc_role") {
|
if !strings.Contains(q, "enc_role") {
|
||||||
t.Fatalf("expected default role query, got %s", q)
|
t.Fatalf("expected default role query, got %s", q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildQuery_Node(t *testing.T) {
|
func TestBuildQuery_Node(t *testing.T) {
|
||||||
q := buildQuery("host1", "", "", "", "enc_role", false)
|
q := buildQuery("host1", "", "", "", "", "enc_role", false)
|
||||||
if !strings.Contains(q, "certname") || !strings.Contains(q, "host1") {
|
if !strings.Contains(q, "certname") || !strings.Contains(q, "host1") {
|
||||||
t.Fatalf("unexpected query: %s", q)
|
t.Fatalf("unexpected query: %s", q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildQuery_FactAndMatch(t *testing.T) {
|
func TestBuildQuery_FactAndMatch(t *testing.T) {
|
||||||
q := buildQuery("", "region", "syd1", "", "enc_role", false)
|
q := buildQuery("", "region", "syd1", "", "", "enc_role", false)
|
||||||
if !strings.Contains(q, "region") || !strings.Contains(q, "syd1") {
|
if !strings.Contains(q, "region") || !strings.Contains(q, "syd1") {
|
||||||
t.Fatalf("unexpected query: %s", q)
|
t.Fatalf("unexpected query: %s", q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildQuery_PartialMatch(t *testing.T) {
|
func TestBuildQuery_PartialMatch(t *testing.T) {
|
||||||
q := buildQuery("", "enc_role", "", "dns", "enc_role", false)
|
q := buildQuery("", "enc_role", "", "dns", "", "enc_role", false)
|
||||||
if !strings.Contains(q, "~") || !strings.Contains(q, "dns") {
|
if !strings.Contains(q, "~") || !strings.Contains(q, "dns") {
|
||||||
t.Fatalf("expected partial match query, got %s", q)
|
t.Fatalf("expected partial match query, got %s", q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildQuery_ShowRole(t *testing.T) {
|
func TestBuildQuery_ShowRole(t *testing.T) {
|
||||||
q := buildQuery("", "", "", "", "my_role_fact", true)
|
q := buildQuery("", "", "", "", "", "my_role_fact", true)
|
||||||
if !strings.Contains(q, "my_role_fact") {
|
if !strings.Contains(q, "my_role_fact") {
|
||||||
t.Fatalf("expected role fact in query, got %s", q)
|
t.Fatalf("expected role fact in query, got %s", q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildQuery_CustomRoleFact(t *testing.T) {
|
func TestBuildQuery_CustomRoleFact(t *testing.T) {
|
||||||
q := buildQuery("", "", "", "", "custom_role", true)
|
q := buildQuery("", "", "", "", "", "custom_role", true)
|
||||||
if !strings.Contains(q, "custom_role") {
|
if !strings.Contains(q, "custom_role") {
|
||||||
t.Fatalf("expected custom role fact, got %s", q)
|
t.Fatalf("expected custom role fact, got %s", q)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user