From 0e8a68fc8ddac7ddd19ebff5cbc50371c653ccbd Mon Sep 17 00:00:00 2001 From: Kabir Sikand Date: Wed, 27 May 2026 20:21:27 +0000 Subject: [PATCH] cmd/tailscale/cli: route join-status not-bound diagnostic to stderr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "this node is not blueprint-bound" exit-1 message was being written to the same io.Writer as the projection block — which in production means os.Stdout. The v2 design specifies stderr for that diagnostic, matching every other CLI command in the package that exits 1. Pull the not-bound check up into runJoinStatus and write it via os.Stderr directly, then call os.Exit(1). The render helpers assume a bound status and always return 0; their test surface shrinks accordingly. Updates #cleanup Signed-off-by: Kabir Sikand --- cmd/tailscale/cli/join_status.go | 32 +++++++++--------------- cmd/tailscale/cli/join_status_test.go | 36 ++++++++++++--------------- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/cmd/tailscale/cli/join_status.go b/cmd/tailscale/cli/join_status.go index 6898e09c8..a78fbcedd 100644 --- a/cmd/tailscale/cli/join_status.go +++ b/cmd/tailscale/cli/join_status.go @@ -67,26 +67,23 @@ func runJoinStatus(ctx context.Context, args []string) error { if err != nil { return fixTailscaledConnectError(err) } - var rc int - if joinStatusArgs.json { - rc = renderJoinStatusJSON(os.Stdout, st) - } else { - rc = renderJoinStatus(os.Stdout, st) + if st == nil || st.Self == nil || st.Self.BlueprintID == "" { + fmt.Fprintln(os.Stderr, "this node is not blueprint-bound; run 'tailscale join --blueprint= --auth-key=...' to bind it") + os.Exit(1) } - if rc != 0 { - os.Exit(rc) + if joinStatusArgs.json { + renderJoinStatusJSON(os.Stdout, st) + } else { + renderJoinStatus(os.Stdout, st) } return nil } // renderJoinStatus writes the text representation of st's blueprint -// state to w and returns a process exit code (0 ok, 1 not-bound). +// state to w and returns 0. The caller is responsible for checking +// whether the node is bound before calling; see runJoinStatus. // Pure: no I/O beyond w. func renderJoinStatus(w io.Writer, st *ipnstate.Status) int { - if st == nil || st.Self == nil || st.Self.BlueprintID == "" { - fmt.Fprintln(w, "this node is not blueprint-bound; run 'tailscale join --blueprint= --auth-key=...' to bind it") - return 1 - } id := st.Self.BlueprintID renderBlueprintConfig(w, id, st.Self.BlueprintConfig) @@ -118,15 +115,10 @@ func renderJoinStatus(w io.Writer, st *ipnstate.Status) int { return 0 } -// renderJoinStatusJSON writes the JSON shape to w; returns exit code. +// renderJoinStatusJSON writes the JSON shape to w; returns 0. The caller +// is responsible for checking whether the node is bound before calling; +// see runJoinStatus. func renderJoinStatusJSON(w io.Writer, st *ipnstate.Status) int { - if st == nil || st.Self == nil || st.Self.BlueprintID == "" { - // Match the text exit-code behavior. Still emit a small JSON - // object so scripted callers can distinguish empty-but-parseable - // from connect failure. - json.NewEncoder(w).Encode(joinStatusJSON{}) - return 1 - } out := joinStatusJSON{ BlueprintID: st.Self.BlueprintID, BlueprintConfig: st.Self.BlueprintConfig, diff --git a/cmd/tailscale/cli/join_status_test.go b/cmd/tailscale/cli/join_status_test.go index e1c46288c..df198e202 100644 --- a/cmd/tailscale/cli/join_status_test.go +++ b/cmd/tailscale/cli/join_status_test.go @@ -26,21 +26,6 @@ func mkPeer(host, bpID string, ip string) (key.NodePublic, *ipnstate.PeerStatus) return key.NewNode().Public(), ps } -func TestRenderJoinStatus_NotBound(t *testing.T) { - st := &ipnstate.Status{ - BackendState: ipn.Running.String(), - Self: &ipnstate.PeerStatus{}, - } - var sb strings.Builder - rc := renderJoinStatus(&sb, st) - if rc != 1 { - t.Errorf("returncode = %d; want 1", rc) - } - if !strings.Contains(sb.String(), "not blueprint-bound") { - t.Errorf("output missing 'not blueprint-bound'; got:\n%s", sb.String()) - } -} - func TestRenderJoinStatus_BoundWithPeers(t *testing.T) { k1, p1 := mkPeer("node-a", "github-connector", "100.64.0.5") k2, p2 := mkPeer("node-b", "github-connector", "100.64.0.7") @@ -151,11 +136,22 @@ func TestRenderJoinStatusJSON(t *testing.T) { } } -func TestRenderJoinStatusJSON_NotBound(t *testing.T) { - st := &ipnstate.Status{Self: &ipnstate.PeerStatus{}} +// TestRenderJoinStatusAlwaysReturnsZero verifies that the render helpers +// always return 0 when called with a bound status. The not-bound case is +// handled by runJoinStatus before the renderers are ever called. +func TestRenderJoinStatusAlwaysReturnsZero(t *testing.T) { + st := &ipnstate.Status{ + BackendState: ipn.Running.String(), + Self: &ipnstate.PeerStatus{ + BlueprintID: "foo", + }, + } var sb strings.Builder - rc := renderJoinStatusJSON(&sb, st) - if rc != 1 { - t.Errorf("returncode = %d; want 1", rc) + if rc := renderJoinStatus(&sb, st); rc != 0 { + t.Errorf("renderJoinStatus returncode = %d; want 0", rc) + } + sb.Reset() + if rc := renderJoinStatusJSON(&sb, st); rc != 0 { + t.Errorf("renderJoinStatusJSON returncode = %d; want 0", rc) } }