app.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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-side commands"
  16. categoryServer = "Server-side 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. // Client commands
  37. cmdPublish,
  38. cmdSubscribe,
  39. },
  40. }
  41. }
  42. func execMainApp(c *cli.Context) error {
  43. fmt.Fprintln(c.App.ErrWriter, "\x1b[1;33mDeprecation notice: Please run the server using 'ntfy serve'; see 'ntfy -h' for help.\x1b[0m")
  44. 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")
  45. return execServe(c)
  46. }
  47. // initConfigFileInputSource is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks
  48. // if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails.
  49. func initConfigFileInputSource(configFlag string, flags []cli.Flag) cli.BeforeFunc {
  50. return func(context *cli.Context) error {
  51. configFile := context.String(configFlag)
  52. if context.IsSet(configFlag) && !util.FileExists(configFile) {
  53. return fmt.Errorf("config file %s does not exist", configFile)
  54. } else if !context.IsSet(configFlag) && !util.FileExists(configFile) {
  55. return nil
  56. }
  57. inputSource, err := altsrc.NewYamlSourceFromFile(configFile)
  58. if err != nil {
  59. return err
  60. }
  61. return altsrc.ApplyInputSourceValues(context, inputSource, flags)
  62. }
  63. }