app.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Package cmd provides the ntfy CLI application
  2. package cmd
  3. import (
  4. "github.com/urfave/cli/v2"
  5. "github.com/urfave/cli/v2/altsrc"
  6. "heckel.io/ntfy/log"
  7. "os"
  8. )
  9. const (
  10. categoryClient = "Client commands"
  11. categoryServer = "Server commands"
  12. )
  13. var commands = make([]*cli.Command, 0)
  14. var flagsDefault = []cli.Flag{
  15. &cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, EnvVars: []string{"NTFY_DEBUG"}, Usage: "enable debug logging"},
  16. &cli.BoolFlag{Name: "trace", EnvVars: []string{"NTFY_TRACE"}, Usage: "enable tracing (very verbose, be careful)"},
  17. &cli.BoolFlag{Name: "no-log-dates", Aliases: []string{"no_log_dates"}, EnvVars: []string{"NTFY_NO_LOG_DATES"}, Usage: "disable the date/time prefix"},
  18. altsrc.NewStringFlag(&cli.StringFlag{Name: "log-level", Aliases: []string{"log_level"}, Value: log.InfoLevel.String(), EnvVars: []string{"NTFY_LOG_LEVEL"}, Usage: "set log level"}),
  19. }
  20. // New creates a new CLI application
  21. func New() *cli.App {
  22. return &cli.App{
  23. Name: "ntfy",
  24. Usage: "Simple pub-sub notification service",
  25. UsageText: "ntfy [OPTION..]",
  26. HideVersion: true,
  27. UseShortOptionHandling: true,
  28. Reader: os.Stdin,
  29. Writer: os.Stdout,
  30. ErrWriter: os.Stderr,
  31. Commands: commands,
  32. Flags: flagsDefault,
  33. Before: initLogFunc,
  34. }
  35. }
  36. func initLogFunc(c *cli.Context) error {
  37. if c.Bool("trace") {
  38. log.SetLevel(log.TraceLevel)
  39. } else if c.Bool("debug") {
  40. log.SetLevel(log.DebugLevel)
  41. } else {
  42. log.SetLevel(log.ToLevel(c.String("log-level")))
  43. }
  44. if c.Bool("no-log-dates") {
  45. log.DisableDates()
  46. }
  47. return nil
  48. }