2021-10-24 01:29:45 +00: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 17:58:14 +00:00
|
|
|
"heckel.io/ntfy/util"
|
2021-10-24 01:29:45 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-01-23 06:00:38 +00:00
|
|
|
const (
|
2022-01-23 20:30:30 +00:00
|
|
|
categoryClient = "Client commands"
|
|
|
|
categoryServer = "Server commands"
|
2022-01-23 06:00:38 +00:00
|
|
|
)
|
|
|
|
|
2022-05-09 15:03:40 +00:00
|
|
|
var commands = make([]*cli.Command, 0)
|
|
|
|
|
2021-10-24 01:29:45 +00: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,
|
2022-05-09 15:03:40 +00:00
|
|
|
Commands: commands,
|
2021-10-24 01:29:45 +00: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 17:58:14 +00:00
|
|
|
if context.IsSet(configFlag) && !util.FileExists(configFile) {
|
2021-10-24 01:29:45 +00:00
|
|
|
return fmt.Errorf("config file %s does not exist", configFile)
|
2021-10-29 17:58:14 +00:00
|
|
|
} else if !context.IsSet(configFlag) && !util.FileExists(configFile) {
|
2021-10-24 01:29:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
inputSource, err := altsrc.NewYamlSourceFromFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return altsrc.ApplyInputSourceValues(context, inputSource, flags)
|
|
|
|
}
|
|
|
|
}
|