Skip to content

Commit

Permalink
Move current top-level package back to neighbors and add some command…
Browse files Browse the repository at this point in the history
… logging
  • Loading branch information
douglaswth committed Jun 26, 2022
1 parent 7a147e1 commit 40ab627
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 14 deletions.
15 changes: 7 additions & 8 deletions cmd/presence/detect.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package main

import (
"context"

"goa.design/clue/log"

"douglasthrift.net/presence"
"douglasthrift.net/presence/neighbors"
)

type (
Expand All @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/presence/main.go
Original file line number Diff line number Diff line change
@@ -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."`
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion arp.go → neighbors/arp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package presence
package neighbors

import (
"context"
Expand Down
5 changes: 4 additions & 1 deletion arp_freebsd.go → neighbors/arp_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package presence
package neighbors

import (
"context"
"encoding/json"
"fmt"
"net"
"os/exec"

"goa.design/clue/log"
)

const (
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion arp_linux.go → neighbors/arp_linux.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package presence
package neighbors

import (
"context"
"encoding/json"
"net"
"os/exec"

"goa.design/clue/log"
)

type (
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion arping.go → neighbors/arping.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package presence
package neighbors

import (
"bufio"
Expand All @@ -8,6 +8,8 @@ import (
"fmt"
"os/exec"
"regexp"

"goa.design/clue/log"
)

type (
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion state.go → neighbors/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package presence
package neighbors

type (
State interface {
Expand Down
2 changes: 1 addition & 1 deletion state_test.go → neighbors/state_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package presence
package neighbors

import (
"testing"
Expand Down

0 comments on commit 40ab627

Please sign in to comment.