main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. )
  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. configPrefix = ""
  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": filepath.Join(configPrefix, "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, log.WithDisableBuffering(func(context.Context) bool { return true }))
  44. }
  45. return
  46. }