Skip to content

feat: add -output-template flag for custom output format - #996

Merged
Mzack9999 merged 2 commits into
devfrom
feature/output-template
Jul 2, 2026
Merged

feat: add -output-template flag for custom output format#996
Mzack9999 merged 2 commits into
devfrom
feature/output-template

Conversation

@dogancanbakir

@dogancanbakir dogancanbakir commented Jun 30, 2026

Copy link
Copy Markdown
Member

Closes #992

Summary

Adds an -output-template / -ot flag that lets users define a custom output format instead of the fixed bracketed layout (example.com [A] [104.20.23.154]), mirroring katana's -output-template.

$ echo example.com | dnsx -silent -a -ot '{{host}} {{a}}'
example.com 104.20.23.154,172.66.147.243

$ echo example.com | dnsx -silent -a -ot '{{ip}} - {{host}}'
104.20.23.154,172.66.147.243 - example.com

This resolves the request to reorder/select fields, drop the brackets, and clean up output without post-processing (tr -d "[]").

Details

  • Template variables map to the JSONL field names (host, a, aaaa, cname, ns, txt, mx, cdn-name, asn, query-time, ...), plus a convenience {{ip}} alias for combined A/AAAA records.
  • Multi-value records are comma-joined; renders once per host.
  • Missing/unknown tags render as empty strings.
  • Mutually exclusive with -json / -raw (validated).
  • Uses valyala/fasttemplate (same lib katana uses).

Testing

  • New unit tests in internal/runner/output_template_test.go.
  • go build ./..., golangci-lint run ./... (0 issues), and go test ./internal/runner/ all pass.
  • Verified live against real DNS (single/multi host, A+AAAA, cdn/asn fields, unknown-tag, record-type guard, validation).

Summary by CodeRabbit

  • New Features

    • Added -ot/--output-template to customize DNS output with {{field}} placeholders, including combined ip (A+AAAA) formatting.
    • Rendering supports multi-value joining and omits missing fields; unknown tags output as empty values.
  • Bug Fixes

    • Enforced that --output-template can’t be used with --json or --raw/--debug output modes.
  • Documentation

    • Updated dnsx -h usage text and added a “Custom Output Format” section with examples and template variable list.
  • Tests

    • Added unit tests for field computation, value stringification, and template rendering.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8c54bfb6-bb3b-43c7-a89a-419d24b5b110

📥 Commits

Reviewing files that changed from the base of the PR and between 9b634ca and 4e505bc.

📒 Files selected for processing (3)
  • internal/runner/output_template.go
  • internal/runner/output_template_test.go
  • internal/runner/runner.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/runner/output_template.go
  • internal/runner/runner.go

Walkthrough

Adds -ot/--output-template support to render DNS results with fasttemplate, including option validation, runner wiring, output formatting logic, tests, and README updates.

Changes

Custom Output Template

Layer / File(s) Summary
Options, validation, and dependencies
go.mod, internal/runner/options.go
Adds fasttemplate dependencies, adds OutputTemplate to Options, registers -ot/--output-template, and blocks use with JSON or raw output.
Template rendering flow
internal/runner/output_template.go, internal/runner/runner.go
Builds template fields from DNS response data, renders custom output strings, stores the parsed template on Runner, parses it in New, and routes matching worker output through the template path.
Tests and documentation
internal/runner/output_template_test.go, README.md
Adds unit tests for field building and rendering, and documents the new flag and custom output format behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

🐇 I hopped through fields both terse and grand,
With {{ip}} neatly close at hand.
No brackets now, the output sings,
-ot gives DNS fresh wings,
And fasttemplate fits just as planned.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding a custom output template flag.
Linked Issues check ✅ Passed The PR implements configurable output formatting with reorderable template fields, bracket-free output, and direct rendering, matching the linked issue goals.
Out of Scope Changes check ✅ Passed The changes stay focused on output templating, docs, tests, and the required dependency updates.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/output-template

Comment @coderabbitai help to get the list of available commands.

@dogancanbakir dogancanbakir self-assigned this Jun 30, 2026
@dogancanbakir
dogancanbakir requested a review from Mzack9999 June 30, 2026 15:25
Comment thread internal/runner/output_template.go Fixed

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
internal/runner/runner.go (1)

794-801: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Routing is correct: the record-type gating now also covers OutputTemplate, and the template branch is placed before the JSON/Raw/text branches. One small doc nit — the comment on Lines 790-793 still says "JSON and raw output," but the gate now also applies to the template path; consider updating it to avoid future confusion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/runner/runner.go` around lines 794 - 801, Update the nearby comment
in runner.go so it matches the current gating logic: the record-type check in
the record handling flow now applies to OutputTemplate as well as JSON and Raw
output. Adjust the comment around the branch in runner.outputRecord to mention
template output too, keeping the wording aligned with the conditions used by
r.options.JSON, r.options.Raw, and r.options.OutputTemplate.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/runner/runner.go`:
- Around line 794-801: Update the nearby comment in runner.go so it matches the
current gating logic: the record-type check in the record handling flow now
applies to OutputTemplate as well as JSON and Raw output. Adjust the comment
around the branch in runner.outputRecord to mention template output too, keeping
the wording aligned with the conditions used by r.options.JSON, r.options.Raw,
and r.options.OutputTemplate.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 999439f9-2c41-47bd-a125-193d829813a3

📥 Commits

Reviewing files that changed from the base of the PR and between 280b825 and 9b634ca.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (6)
  • README.md
  • go.mod
  • internal/runner/options.go
  • internal/runner/output_template.go
  • internal/runner/output_template_test.go
  • internal/runner/runner.go

@Mzack9999
Mzack9999 merged commit 008a2bc into dev Jul 2, 2026
12 checks passed
@Mzack9999
Mzack9999 deleted the feature/output-template branch July 2, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output Format Filtering Determination

3 participants