app.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Package cmd provides the ntfy CLI application
  2. package cmd
  3. import (
  4. "fmt"
  5. "github.com/urfave/cli/v2"
  6. "github.com/urfave/cli/v2/altsrc"
  7. "heckel.io/ntfy/log"
  8. "os"
  9. "regexp"
  10. )
  11. const (
  12. categoryClient = "Client commands"
  13. categoryServer = "Server commands"
  14. )
  15. var commands = make([]*cli.Command, 0)
  16. var flagsDefault = []cli.Flag{
  17. &cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, EnvVars: []string{"NTFY_DEBUG"}, Usage: "enable debug logging"},
  18. &cli.BoolFlag{Name: "trace", EnvVars: []string{"NTFY_TRACE"}, Usage: "enable tracing (very verbose, be careful)"},
  19. &cli.BoolFlag{Name: "no-log-dates", Aliases: []string{"no_log_dates"}, EnvVars: []string{"NTFY_NO_LOG_DATES"}, Usage: "disable the date/time prefix"},
  20. altsrc.NewStringFlag(&cli.StringFlag{Name: "log-level", Aliases: []string{"log_level"}, Value: log.InfoLevel.String(), EnvVars: []string{"NTFY_LOG_LEVEL"}, Usage: "set log level"}),
  21. altsrc.NewStringSliceFlag(&cli.StringSliceFlag{Name: "log-level-overrides", Aliases: []string{"log_level_overrides"}, EnvVars: []string{"NTFY_LOG_LEVEL_OVERRIDES"}, Usage: "set log level overrides"}),
  22. altsrc.NewStringFlag(&cli.StringFlag{Name: "log-format", Aliases: []string{"log_format"}, Value: log.TextFormat.String(), EnvVars: []string{"NTFY_LOG_FORMAT"}, Usage: "set log format"}),
  23. altsrc.NewStringFlag(&cli.StringFlag{Name: "log-file", Aliases: []string{"log_file"}, EnvVars: []string{"NTFY_LOG_FILE"}, Usage: "set log file, default is STDOUT"}),
  24. }
  25. var (
  26. logLevelOverrideRegex = regexp.MustCompile(`(?i)^([^=\s]+)(?:\s*=\s*(\S+))?\s*->\s*(TRACE|DEBUG|INFO|WARN|ERROR)$`)
  27. )
  28. // New creates a new CLI application
  29. func New() *cli.App {
  30. return &cli.App{
  31. Name: "ntfy",
  32. Usage: "Simple pub-sub notification service",
  33. UsageText: "ntfy [OPTION..]",
  34. HideVersion: true,
  35. UseShortOptionHandling: true,
  36. Reader: os.Stdin,
  37. Writer: os.Stdout,
  38. ErrWriter: os.Stderr,
  39. Commands: commands,
  40. Flags: flagsDefault,
  41. Before: initLogFunc,
  42. }
  43. }
  44. func initLogFunc(c *cli.Context) error {
  45. log.SetLevel(log.ToLevel(c.String("log-level")))
  46. log.SetFormat(log.ToFormat(c.String("log-format")))
  47. if c.Bool("trace") {
  48. log.SetLevel(log.TraceLevel)
  49. } else if c.Bool("debug") {
  50. log.SetLevel(log.DebugLevel)
  51. }
  52. if c.Bool("no-log-dates") {
  53. log.DisableDates()
  54. }
  55. if err := applyLogLevelOverrides(c.StringSlice("log-level-overrides")); err != nil {
  56. return err
  57. }
  58. logFile := c.String("log-file")
  59. if logFile != "" {
  60. w, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
  61. if err != nil {
  62. return err
  63. }
  64. log.SetOutput(w)
  65. }
  66. return nil
  67. }
  68. func applyLogLevelOverrides(rawOverrides []string) error {
  69. for _, override := range rawOverrides {
  70. m := logLevelOverrideRegex.FindStringSubmatch(override)
  71. if len(m) == 4 {
  72. field, value, level := m[1], m[2], m[3]
  73. log.SetLevelOverride(field, value, log.ToLevel(level))
  74. } else if len(m) == 3 {
  75. field, level := m[1], m[2]
  76. log.SetLevelOverride(field, "", log.ToLevel(level)) // Matches any value
  77. } else {
  78. return fmt.Errorf(`invalid log level override "%s", must be "field=value -> loglevel", e.g. "user_id=u_123 -> DEBUG"`, override)
  79. }
  80. }
  81. return nil
  82. }