app.go 2.3 KB

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