app.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. Commands: []*cli.Command{
  30. // Server commands
  31. cmdServe,
  32. cmdUser,
  33. cmdAccess,
  34. // Client commands
  35. cmdPublish,
  36. cmdSubscribe,
  37. },
  38. }
  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. }