Skip to content

Commit

Permalink
Get started with command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
douglaswth committed Jun 26, 2022
1 parent 070172f commit 7a147e1
Show file tree
Hide file tree
Showing 5 changed files with 891 additions and 27 deletions.
9 changes: 9 additions & 0 deletions cmd/presence/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

type (
Check struct{}
)

func (c *Check) Run(cli *CLI) error {
return nil
}
40 changes: 40 additions & 0 deletions cmd/presence/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"context"

"goa.design/clue/log"

"douglasthrift.net/presence"
)

type (
Detect struct {
Interface string `arg:""`
HardwareAddrs []string `arg:""`
}
)

func (d *Detect) Run(cli *CLI) error {
ifs := presence.Interfaces{d.Interface: true}
hws := make(presence.HardwareAddrStates, len(d.HardwareAddrs))
for _, hw := range d.HardwareAddrs {
hws[hw] = presence.NewState()
}

ctx := log.Context(context.Background(), log.WithDisableBuffering(func(context.Context) bool { return true }))
a, err := presence.NewARP(1)
if err != nil {
return err
}

ok, err := a.Present(ctx, ifs, hws)
if err != nil {
return err
}
log.Info(ctx, log.KV{K: "present", V: ok})
for hw, state := range hws {
log.Info(ctx, log.KV{K: "hw", V: hw}, log.KV{K: "present", V: state.Present()}, log.KV{K: "changed", V: state.Changed()})
}
return nil
}
48 changes: 26 additions & 22 deletions cmd/presence/main.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package main

import (
"context"
"log"
"os"
"fmt"
"runtime"

"douglasthrift.net/presence"
"github.com/alecthomas/kong"
)

func main() {
ifs := presence.Interfaces{os.Args[1]: true}
hws := make(presence.HardwareAddrStates, len(os.Args[2:]))
for _, hw := range os.Args[2:] {
hws[hw] = presence.NewState()
}
type (
CLI struct {
Version kong.VersionFlag `help:"Show version information." short:"v"`

ctx := context.Background()
a, err := presence.NewARP(1)
if err != nil {
log.Fatal(err)
Detect Detect `cmd:"" help:"Detect network presence and push state changes to IFTTT."`
Check Check `cmd:"" help:"Check configuration."`
}
)

ok, err := a.Present(ctx, ifs, hws)
if err != nil {
log.Fatal(err)
}
log.Printf("present=%v", ok)
for hw, state := range hws {
log.Printf("%v present=%v changed=%v", hw, state.Present(), state.Changed())
}
var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
cli := &CLI{}
ctx := kong.Parse(
cli,
kong.Description("Home network presence detection daemon for IFTTT"), kong.UsageOnError(),
kong.Vars{
"version": fmt.Sprintf("presence version %v %v %v/%v %v %v", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, commit, date),
},
)
err := ctx.Run(cli)
ctx.FatalIfErrorf(err)
}
24 changes: 22 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@ module douglasthrift.net/presence

go 1.17

require github.com/stretchr/testify v1.7.2
require (
github.com/alecthomas/kong v0.6.1
github.com/stretchr/testify v1.7.5
goa.design/clue v0.7.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
goa.design/goa/v3 v3.7.5 // indirect
golang.org/x/net v0.0.0-20220531201128-c960675eff93 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220531173845-685668d2de03 // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

0 comments on commit 7a147e1

Please sign in to comment.