app.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/util"
  8. "os"
  9. )
  10. var (
  11. defaultClientRootConfigFile = "/etc/ntfy/client.yml"
  12. defaultClientUserConfigFile = "~/.config/ntfy/client.yml"
  13. )
  14. const (
  15. categoryClient = "Client commands"
  16. categoryServer = "Server commands"
  17. )
  18. // New creates a new CLI application
  19. func New() *cli.App {
  20. return &cli.App{
  21. Name: "ntfy",
  22. Usage: "Simple pub-sub notification service",
  23. UsageText: "ntfy [OPTION..]",
  24. HideVersion: true,
  25. UseShortOptionHandling: true,
  26. Reader: os.Stdin,
  27. Writer: os.Stdout,
  28. ErrWriter: os.Stderr,
  29. Action: execMainApp,
  30. Before: initConfigFileInputSource("config", flagsServe), // DEPRECATED, see deprecation notice
  31. Flags: flagsServe, // DEPRECATED, see deprecation notice
  32. Commands: []*cli.Command{
  33. // Server commands
  34. cmdServe,
  35. cmdUser,
  36. cmdAllow,
  37. cmdDeny,
  38. // Client commands
  39. cmdPublish,
  40. cmdSubscribe,
  41. },
  42. }
  43. }
  44. func execMainApp(c *cli.Context) error {
  45. fmt.Fprintln(c.App.ErrWriter, "\x1b[1;33mDeprecation notice: Please run the server using 'ntfy serve'; see 'ntfy -h' for help.\x1b[0m")
  46. fmt.Fprintln(c.App.ErrWriter, "\x1b[1;33mThis way of running the server will be removed March 2022. See https://ntfy.sh/docs/deprecations/ for details.\x1b[0m")
  47. return execServe(c)
  48. }
  49. // initConfigFileInputSource is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks
  50. // if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails.
  51. func initConfigFileInputSource(configFlag string, flags []cli.Flag) cli.BeforeFunc {
  52. return func(context *cli.Context) error {
  53. configFile := context.String(configFlag)
  54. if context.IsSet(configFlag) && !util.FileExists(configFile) {
  55. return fmt.Errorf("config file %s does not exist", configFile)
  56. } else if !context.IsSet(configFlag) && !util.FileExists(configFile) {
  57. return nil
  58. }
  59. inputSource, err := altsrc.NewYamlSourceFromFile(configFile)
  60. if err != nil {
  61. return err
  62. }
  63. return altsrc.ApplyInputSourceValues(context, inputSource, flags)
  64. }
  65. }