Browse Source

Move current top-level package back to neighbors and add some command logging

Douglas Thrift 1 year ago
parent
commit
40ab627a25

+ 7 - 8
cmd/presence/detect.go

@@ -1,11 +1,9 @@
 package main
 
 import (
-	"context"
-
 	"goa.design/clue/log"
 
-	"douglasthrift.net/presence"
+	"douglasthrift.net/presence/neighbors"
 )
 
 type (
@@ -16,14 +14,15 @@ type (
 )
 
 func (d *Detect) Run(cli *CLI) error {
-	ifs := presence.Interfaces{d.Interface: true}
-	hws := make(presence.HardwareAddrStates, len(d.HardwareAddrs))
+	ctx := cli.Context()
+
+	ifs := neighbors.Interfaces{d.Interface: true}
+	hws := make(neighbors.HardwareAddrStates, len(d.HardwareAddrs))
 	for _, hw := range d.HardwareAddrs {
-		hws[hw] = presence.NewState()
+		hws[hw] = neighbors.NewState()
 	}
 
-	ctx := log.Context(context.Background(), log.WithDisableBuffering(func(context.Context) bool { return true }))
-	a, err := presence.NewARP(1)
+	a, err := neighbors.NewARP(1)
 	if err != nil {
 		return err
 	}

+ 13 - 0
cmd/presence/main.go

@@ -1,14 +1,17 @@
 package main
 
 import (
+	"context"
 	"fmt"
 	"runtime"
 
 	"github.com/alecthomas/kong"
+	"goa.design/clue/log"
 )
 
 type (
 	CLI struct {
+		Debug   bool             `help:"Show debug information in log." short:"d"`
 		Version kong.VersionFlag `help:"Show version information." short:"v"`
 
 		Detect Detect `cmd:"" help:"Detect network presence and push state changes to IFTTT."`
@@ -34,3 +37,13 @@ func main() {
 	err := ctx.Run(cli)
 	ctx.FatalIfErrorf(err)
 }
+
+func (cli *CLI) Context() (ctx context.Context) {
+	ctx = context.Background()
+	if cli.Debug {
+		ctx = log.Context(ctx, log.WithDebug())
+	} else {
+		ctx = log.Context(ctx)
+	}
+	return
+}

+ 1 - 1
arp.go → neighbors/arp.go

@@ -1,4 +1,4 @@
-package presence
+package neighbors
 
 import (
 	"context"

+ 4 - 1
arp_freebsd.go → neighbors/arp_freebsd.go

@@ -1,4 +1,4 @@
-package presence
+package neighbors
 
 import (
 	"context"
@@ -6,6 +6,8 @@ import (
 	"fmt"
 	"net"
 	"os/exec"
+
+	"goa.design/clue/log"
 )
 
 const (
@@ -53,6 +55,7 @@ func (a *arp) Present(ctx context.Context, ifs Interfaces, hws HardwareAddrState
 	}
 
 	cmd := exec.CommandContext(ctx, a.cmd, "--libxo=json", "-an")
+	log.Debug(ctx, log.KV{K: "cmd", V: cmd})
 	b, err := cmd.Output()
 	if err != nil {
 		return

+ 4 - 1
arp_linux.go → neighbors/arp_linux.go

@@ -1,10 +1,12 @@
-package presence
+package neighbors
 
 import (
 	"context"
 	"encoding/json"
 	"net"
 	"os/exec"
+
+	"goa.design/clue/log"
 )
 
 type (
@@ -41,6 +43,7 @@ func (a *arp) Present(ctx context.Context, ifs Interfaces, hws HardwareAddrState
 	}
 
 	cmd := exec.CommandContext(ctx, a.cmd, "-family", "inet", "-json", "neighbor", "show", "nud", "reachable")
+	log.Debug(ctx, log.KV{K: "cmd", V: cmd})
 	b, err := cmd.Output()
 	if err != nil {
 		return

+ 4 - 1
arping.go → neighbors/arping.go

@@ -1,4 +1,4 @@
-package presence
+package neighbors
 
 import (
 	"bufio"
@@ -8,6 +8,8 @@ import (
 	"fmt"
 	"os/exec"
 	"regexp"
+
+	"goa.design/clue/log"
 )
 
 type (
@@ -61,6 +63,7 @@ func NewARPing(count uint) (ARPing, error) {
 
 func (a *arping) Ping(ctx context.Context, ifi, hw, ip string) (ok bool, err error) {
 	cmd := exec.CommandContext(ctx, a.sudoCmd, a.arpingCmd, "-c", a.count, "-i", ifi, "-t", hw, "-q", ip)
+	log.Debug(ctx, log.KV{K: "cmd", V: cmd})
 	err = cmd.Run()
 	if err == nil {
 		ok = true

+ 1 - 1
state.go → neighbors/state.go

@@ -1,4 +1,4 @@
-package presence
+package neighbors
 
 type (
 	State interface {

+ 1 - 1
state_test.go → neighbors/state_test.go

@@ -1,4 +1,4 @@
-package presence
+package neighbors
 
 import (
 	"testing"