main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "runtime"
  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. configPrefix = ""
  25. wNet = wrap.NewNet()
  26. )
  27. func main() {
  28. cli := &CLI{}
  29. ctx := kong.Parse(
  30. cli,
  31. kong.Description("Home network presence detection daemon for IFTTT"), kong.UsageOnError(),
  32. kong.Vars{
  33. "config": filepath.Join(configPrefix, "presence.yml"),
  34. "version": fmt.Sprintf("presence version %v %v %v/%v %v %v", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, commit, date),
  35. },
  36. )
  37. err := ctx.Run(cli)
  38. ctx.FatalIfErrorf(err)
  39. }
  40. func (cli *CLI) Context() (ctx context.Context) {
  41. ctx = context.Background()
  42. if cli.Debug {
  43. ctx = log.Context(ctx, log.WithDebug())
  44. } else {
  45. ctx = log.Context(ctx)
  46. }
  47. return
  48. }