-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get started with command line interface
- Loading branch information
1 parent
070172f
commit 7a147e1
Showing
5 changed files
with
891 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.