2021-10-24 04:29:45 +03:00
// Package cmd provides the ntfy CLI application
package cmd
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
2021-10-29 20:58:14 +03:00
"heckel.io/ntfy/util"
2021-10-24 04:29:45 +03:00
"os"
)
2021-12-18 22:43:27 +03:00
var (
defaultClientRootConfigFile = "/etc/ntfy/client.yml"
defaultClientUserConfigFile = "~/.config/ntfy/client.yml"
)
2021-10-24 04:29:45 +03:00
// New creates a new CLI application
func New ( ) * cli . App {
return & cli . App {
Name : "ntfy" ,
Usage : "Simple pub-sub notification service" ,
UsageText : "ntfy [OPTION..]" ,
HideVersion : true ,
UseShortOptionHandling : true ,
Reader : os . Stdin ,
Writer : os . Stdout ,
ErrWriter : os . Stderr ,
2021-12-17 04:33:01 +03:00
Action : execMainApp ,
Before : initConfigFileInputSource ( "config" , flagsServe ) , // DEPRECATED, see deprecation notice
Flags : flagsServe , // DEPRECATED, see deprecation notice
Commands : [ ] * cli . Command {
cmdServe ,
cmdPublish ,
cmdSubscribe ,
} ,
2021-10-24 04:29:45 +03:00
}
}
2021-12-17 04:33:01 +03:00
func execMainApp ( c * cli . Context ) error {
2021-12-18 22:43:27 +03:00
fmt . Fprintln ( c . App . ErrWriter , "\x1b[1;33mDeprecation notice: Please run the server using 'ntfy serve'; see 'ntfy -h' for help.\x1b[0m" )
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" )
2021-12-17 04:33:01 +03:00
return execServe ( c )
2021-10-24 04:29:45 +03:00
}
// initConfigFileInputSource is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks
// if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails.
func initConfigFileInputSource ( configFlag string , flags [ ] cli . Flag ) cli . BeforeFunc {
return func ( context * cli . Context ) error {
configFile := context . String ( configFlag )
2021-10-29 20:58:14 +03:00
if context . IsSet ( configFlag ) && ! util . FileExists ( configFile ) {
2021-10-24 04:29:45 +03:00
return fmt . Errorf ( "config file %s does not exist" , configFile )
2021-10-29 20:58:14 +03:00
} else if ! context . IsSet ( configFlag ) && ! util . FileExists ( configFile ) {
2021-10-24 04:29:45 +03:00
return nil
}
inputSource , err := altsrc . NewYamlSourceFromFile ( configFile )
if err != nil {
return err
}
return altsrc . ApplyInputSourceValues ( context , inputSource , flags )
}
}