app.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // New creates a new CLI application
  15. func New() *cli.App {
  16. return &cli.App{
  17. Name: "ntfy",
  18. Usage: "Simple pub-sub notification service",
  19. UsageText: "ntfy [OPTION..]",
  20. HideVersion: true,
  21. UseShortOptionHandling: true,
  22. Reader: os.Stdin,
  23. Writer: os.Stdout,
  24. ErrWriter: os.Stderr,
  25. Action: execMainApp,
  26. Before: initConfigFileInputSource("config", flagsServe), // DEPRECATED, see deprecation notice
  27. Flags: flagsServe, // DEPRECATED, see deprecation notice
  28. Commands: []*cli.Command{
  29. cmdServe,
  30. cmdPublish,
  31. cmdSubscribe,
  32. },
  33. }
  34. }
  35. func execMainApp(c *cli.Context) error {
  36. fmt.Fprintln(c.App.ErrWriter, "\x1b[1;33mDeprecation notice: Please run the server using 'ntfy serve'; see 'ntfy -h' for help.\x1b[0m")
  37. 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")
  38. return execServe(c)
  39. }
  40. // initConfigFileInputSource is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks
  41. // if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails.
  42. func initConfigFileInputSource(configFlag string, flags []cli.Flag) cli.BeforeFunc {
  43. return func(context *cli.Context) error {
  44. configFile := context.String(configFlag)
  45. if context.IsSet(configFlag) && !util.FileExists(configFile) {
  46. return fmt.Errorf("config file %s does not exist", configFile)
  47. } else if !context.IsSet(configFlag) && !util.FileExists(configFile) {
  48. return nil
  49. }
  50. inputSource, err := altsrc.NewYamlSourceFromFile(configFile)
  51. if err != nil {
  52. return err
  53. }
  54. return altsrc.ApplyInputSourceValues(context, inputSource, flags)
  55. }
  56. }