app.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. const (
  11. categoryClient = "Client commands"
  12. categoryServer = "Server commands"
  13. )
  14. var commands = make([]*cli.Command, 0)
  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. Commands: commands,
  27. }
  28. }
  29. // initConfigFileInputSource is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks
  30. // if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails.
  31. func initConfigFileInputSource(configFlag string, flags []cli.Flag) cli.BeforeFunc {
  32. return func(context *cli.Context) error {
  33. configFile := context.String(configFlag)
  34. if context.IsSet(configFlag) && !util.FileExists(configFile) {
  35. return fmt.Errorf("config file %s does not exist", configFile)
  36. } else if !context.IsSet(configFlag) && !util.FileExists(configFile) {
  37. return nil
  38. }
  39. inputSource, err := altsrc.NewYamlSourceFromFile(configFile)
  40. if err != nil {
  41. return err
  42. }
  43. return altsrc.ApplyInputSourceValues(context, inputSource, flags)
  44. }
  45. }