From 532ff062f2fffe9ddaea3b5fd2a4d196f27294e2 Mon Sep 17 00:00:00 2001 From: Fersah Lemre <129683710+ferhalemre@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:13:41 +0300 Subject: [PATCH] fix: return an error when --whois is used with a non-domain query --whois only supports domain objects. For any other object type the CLI printed nothing and exited 0, so scripts could not tell that the output was missing. Check the response type before printing and exit 1 with an explanatory message instead. Fixes #37 Co-Authored-By: Claude Fable 5.2 --- cli.go | 9 +++++++++ cli_test.go | 20 ++++++++++++++++++++ testdata/cli/autnum-text.golden | 11 +++++++++++ testdata/cli/autnum-whois-json.golden | 5 +++++ testdata/cli/autnum-whois.golden | 5 +++++ 5 files changed, 50 insertions(+) create mode 100644 testdata/cli/autnum-text.golden create mode 100644 testdata/cli/autnum-whois-json.golden create mode 100644 testdata/cli/autnum-whois.golden diff --git a/cli.go b/cli.go index 9203ae6..484a923 100644 --- a/cli.go +++ b/cli.go @@ -542,6 +542,15 @@ func RunCLI(args []string, stdout io.Writer, stderr io.Writer, options CLIOption fmt.Fprintln(stderr, "") } + // WHOIS style output is only implemented for domains. Fail instead of + // printing nothing and exiting successfully for other object types. + if *outputFormatWhois { + if _, ok := resp.Object.(*Domain); !ok { + printError(stderr, "Error: --whois output is only supported for domain queries") + return 1 + } + } + // Output formatting. if !*outputFormatText && !*outputFormatWhois && !*outputFormatJSON && !*outputFormatRaw { *outputFormatText = true diff --git a/cli_test.go b/cli_test.go index 4444c7e..400bda4 100644 --- a/cli_test.go +++ b/cli_test.go @@ -33,12 +33,29 @@ func fixtureServer(t *testing.T) *httptest.Server { _, _ = w.Write(test.LoadFile("rdap/rdap.nic.cz/domain-example.cz.json")) }) + mux.HandleFunc("/autnum/64496", func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/rdap+json") + _, _ = w.Write([]byte(autnumFixture)) + }) + srv := httptest.NewServer(mux) t.Cleanup(srv.Close) return srv } +// autnumFixture is a minimal autnum response (AS64496 is reserved for +// documentation, RFC 5398) used to exercise non-domain queries. +const autnumFixture = `{ + "rdapConformance": ["rdap_level_0"], + "objectClassName": "autnum", + "handle": "AS64496", + "startAutnum": 64496, + "endAutnum": 64496, + "name": "EXAMPLE-AS", + "type": "DIRECT ALLOCATION" +}` + func http2FixtureServer(t *testing.T) *httptest.Server { t.Helper() @@ -99,6 +116,9 @@ func TestRunCLIGolden(t *testing.T) { {"domain-json", withServer("--json", "example.cz")}, {"domain-whois", withServer("--whois", "example.cz")}, {"domain-raw", withServer("--raw", "example.cz")}, + {"autnum-text", withServer("AS64496")}, + {"autnum-whois", withServer("--whois", "AS64496")}, + {"autnum-whois-json", withServer("--whois", "--json", "AS64496")}, {"version", []string{"--version"}}, {"no-args", []string{}}, {"unknown-type", []string{"--type", "bogus", "example.cz"}}, diff --git a/testdata/cli/autnum-text.golden b/testdata/cli/autnum-text.golden new file mode 100644 index 0000000..238ce7c --- /dev/null +++ b/testdata/cli/autnum-text.golden @@ -0,0 +1,11 @@ +exit: 0 +--- stdout --- +Autnum: + Handle: AS64496 + Name: EXAMPLE-AS + Type: DIRECT ALLOCATION + StartAutnum: 64496 + EndAutnum: 64496 + Conformance: rdap_level_0 + +--- stderr --- diff --git a/testdata/cli/autnum-whois-json.golden b/testdata/cli/autnum-whois-json.golden new file mode 100644 index 0000000..ff27bf0 --- /dev/null +++ b/testdata/cli/autnum-whois-json.golden @@ -0,0 +1,5 @@ +exit: 1 +--- stdout --- + +--- stderr --- +# Error: --whois output is only supported for domain queries diff --git a/testdata/cli/autnum-whois.golden b/testdata/cli/autnum-whois.golden new file mode 100644 index 0000000..ff27bf0 --- /dev/null +++ b/testdata/cli/autnum-whois.golden @@ -0,0 +1,5 @@ +exit: 1 +--- stdout --- + +--- stderr --- +# Error: --whois output is only supported for domain queries