cmd/tailscale: make tailscale status -active also filter in -json mode

reviewable/pr560/r1
Brad Fitzpatrick 4 years ago
parent 19867b2b6d
commit 7ba148e54e

@ -78,6 +78,13 @@ func runStatus(ctx context.Context, args []string) error {
return err return err
} }
if statusArgs.json { if statusArgs.json {
if statusArgs.active {
for peer, ps := range st.Peer {
if !peerActive(ps) {
delete(st.Peer, peer)
}
}
}
j, err := json.MarshalIndent(st, "", " ") j, err := json.MarshalIndent(st, "", " ")
if err != nil { if err != nil {
return err return err
@ -122,8 +129,7 @@ func runStatus(ctx context.Context, args []string) error {
f := func(format string, a ...interface{}) { fmt.Fprintf(&buf, format, a...) } f := func(format string, a ...interface{}) { fmt.Fprintf(&buf, format, a...) }
for _, peer := range st.Peers() { for _, peer := range st.Peers() {
ps := st.Peer[peer] ps := st.Peer[peer]
// TODO: let server report this active bool instead active := peerActive(ps)
active := !ps.LastWrite.IsZero() && time.Since(ps.LastWrite) < 2*time.Minute
if statusArgs.active && !active { if statusArgs.active && !active {
continue continue
} }
@ -157,3 +163,10 @@ func runStatus(ctx context.Context, args []string) error {
os.Stdout.Write(buf.Bytes()) os.Stdout.Write(buf.Bytes())
return nil return nil
} }
// peerActive reports whether ps has recent activity.
//
// TODO: have the server report this bool instead.
func peerActive(ps *ipnstate.PeerStatus) bool {
return !ps.LastWrite.IsZero() && time.Since(ps.LastWrite) < 2*time.Minute
}

@ -526,6 +526,8 @@ func (b *LocalBackend) send(n Notify) {
if notify != nil { if notify != nil {
n.Version = version.LONG n.Version = version.LONG
notify(n) notify(n)
} else {
b.logf("nil notify callback; dropping %+v", n)
} }
} }
@ -1037,7 +1039,7 @@ func (b *LocalBackend) RequestEngineStatus() {
// RequestStatus implements Backend. // RequestStatus implements Backend.
func (b *LocalBackend) RequestStatus() { func (b *LocalBackend) RequestStatus() {
st := b.Status() st := b.Status()
b.notify(Notify{Status: st}) b.send(Notify{Status: st})
} }
// stateMachine updates the state machine state based on other things // stateMachine updates the state machine state based on other things

Loading…
Cancel
Save