main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "runtime"
  6. "github.com/alecthomas/kong"
  7. "goa.design/clue/log"
  8. "douglasthrift.net/presence/wrap"
  9. )
  10. type (
  11. CLI struct {
  12. Config string `default:"${config}" help:"Set the configuration file." short:"c" type:"existingfile"`
  13. Debug bool `help:"Show debug information in log." short:"d"`
  14. Version kong.VersionFlag `help:"Show version information." short:"v"`
  15. Detect Detect `cmd:"" help:"Detect network presence and push state changes to IFTTT."`
  16. Check Check `cmd:"" help:"Check configuration."`
  17. }
  18. )
  19. var (
  20. version = "dev"
  21. commit = "none"
  22. date = "unknown"
  23. wNet = wrap.NewNet()
  24. )
  25. func main() {
  26. cli := &CLI{}
  27. ctx := kong.Parse(
  28. cli,
  29. kong.Description("Home network presence detection daemon for IFTTT"), kong.UsageOnError(),
  30. kong.Vars{
  31. "config": "presence.yml",
  32. "version": fmt.Sprintf("presence version %v %v %v/%v %v %v", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, commit, date),
  33. },
  34. )
  35. err := ctx.Run(cli)
  36. ctx.FatalIfErrorf(err)
  37. }
  38. func (cli *CLI) Context() (ctx context.Context) {
  39. ctx = context.Background()
  40. if cli.Debug {
  41. ctx = log.Context(ctx, log.WithDebug())
  42. } else {
  43. ctx = log.Context(ctx)
  44. }
  45. log.Print(ctx,
  46. log.KV{K: "presence version", V: version},
  47. log.KV{K: "go version", V: runtime.Version()},
  48. log.KV{K: "os", V: runtime.GOOS},
  49. log.KV{K: "arch", V: runtime.GOARCH},
  50. log.KV{K: "commit", V: commit},
  51. log.KV{K: "date", V: date},
  52. )
  53. return
  54. }