mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-18 02:48:40 +00:00
cmd/tailscale: improve ping error message when logged out
Refactor out the pretty status printing code from status, use it in ping. Fixes #3549 Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
parent
8f43ddf1a2
commit
4512e213d5
@ -11,6 +11,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -64,6 +65,16 @@ var pingArgs struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runPing(ctx context.Context, args []string) error {
|
func runPing(ctx context.Context, args []string) error {
|
||||||
|
st, err := tailscale.Status(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fixTailscaledConnectError(err)
|
||||||
|
}
|
||||||
|
description, ok := isRunningOrStarting(st)
|
||||||
|
if !ok {
|
||||||
|
printf("%s\n", description)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
c, bc, ctx, cancel := connect(ctx)
|
c, bc, ctx, cancel := connect(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
@ -121,24 +121,10 @@ func runStatus(ctx context.Context, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch st.BackendState {
|
description, ok := isRunningOrStarting(st)
|
||||||
default:
|
if !ok {
|
||||||
fmt.Fprintf(Stderr, "unexpected state: %s\n", st.BackendState)
|
outln(description)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
case ipn.Stopped.String():
|
|
||||||
outln("Tailscale is stopped.")
|
|
||||||
os.Exit(1)
|
|
||||||
case ipn.NeedsLogin.String():
|
|
||||||
outln("Logged out.")
|
|
||||||
if st.AuthURL != "" {
|
|
||||||
printf("\nLog in at: %s\n", st.AuthURL)
|
|
||||||
}
|
|
||||||
os.Exit(1)
|
|
||||||
case ipn.NeedsMachineAuth.String():
|
|
||||||
outln("Machine is not yet authorized by tailnet admin.")
|
|
||||||
os.Exit(1)
|
|
||||||
case ipn.Running.String(), ipn.Starting.String():
|
|
||||||
// Run below.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(st.Health) > 0 {
|
if len(st.Health) > 0 {
|
||||||
@ -222,6 +208,27 @@ func runStatus(ctx context.Context, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isRunningOrStarting reports whether st is in state Running or Starting.
|
||||||
|
// It also returns a description of the status suitable to display to a user.
|
||||||
|
func isRunningOrStarting(st *ipnstate.Status) (description string, ok bool) {
|
||||||
|
switch st.BackendState {
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("unexpected state: %s", st.BackendState), false
|
||||||
|
case ipn.Stopped.String():
|
||||||
|
return "Tailscale is stopped.", false
|
||||||
|
case ipn.NeedsLogin.String():
|
||||||
|
s := "Logged out."
|
||||||
|
if st.AuthURL != "" {
|
||||||
|
s += fmt.Sprintf("\nLog in at: %s", st.AuthURL)
|
||||||
|
}
|
||||||
|
return s, false
|
||||||
|
case ipn.NeedsMachineAuth.String():
|
||||||
|
return "Machine is not yet authorized by tailnet admin.", false
|
||||||
|
case ipn.Running.String(), ipn.Starting.String():
|
||||||
|
return st.BackendState, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func dnsOrQuoteHostname(st *ipnstate.Status, ps *ipnstate.PeerStatus) string {
|
func dnsOrQuoteHostname(st *ipnstate.Status, ps *ipnstate.PeerStatus) string {
|
||||||
baseName := dnsname.TrimSuffix(ps.DNSName, st.MagicDNSSuffix)
|
baseName := dnsname.TrimSuffix(ps.DNSName, st.MagicDNSSuffix)
|
||||||
if baseName != "" {
|
if baseName != "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user