main.go 1.6 KB

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