app.go 2.3 KB

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