mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-25 19:21:18 -08:00
cmd/tailscale/cli: route join-status not-bound diagnostic to stderr
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 <[email protected]>
This commit is contained in:
1 parent
78e1e4a4e9
commit
0e8a68fc8d
2 files changed
+28
-40
No files matched your search
@@ -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=<id> --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=<id> --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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user