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
}
if statusArgs.json {
if statusArgs.active {
for peer, ps := range st.Peer {
if !peerActive(ps) {
delete(st.Peer, peer)
}
}
}
j, err := json.MarshalIndent(st, "", " ")
if err != nil {
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...) }
for _, peer := range st.Peers() {
ps := st.Peer[peer]
// TODO: let server report this active bool instead
active := !ps.LastWrite.IsZero() && time.Since(ps.LastWrite) < 2*time.Minute
active := peerActive(ps)
if statusArgs.active && !active {
continue
}
@ -157,3 +163,10 @@ func runStatus(ctx context.Context, args []string) error {
os.Stdout.Write(buf.Bytes())
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 {
n.Version = version.LONG
notify(n)
} else {
b.logf("nil notify callback; dropping %+v", n)
}
}
@ -1037,7 +1039,7 @@ func (b *LocalBackend) RequestEngineStatus() {
// RequestStatus implements Backend.
func (b *LocalBackend) RequestStatus() {
st := b.Status()
b.notify(Notify{Status: st})
b.send(Notify{Status: st})
}
// stateMachine updates the state machine state based on other things

Loading…
Cancel
Save