app.go 1.4 KB

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