2021-10-24 01:29:45 +00:00
|
|
|
// Package cmd provides the ntfy CLI application
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/urfave/cli/v2"
|
2022-06-01 20:57:35 +00:00
|
|
|
"github.com/urfave/cli/v2/altsrc"
|
2022-05-30 02:14:14 +00:00
|
|
|
"heckel.io/ntfy/log"
|
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)
|
|
|
|
|
2022-05-30 02:14:14 +00:00
|
|
|
var flagsDefault = []cli.Flag{
|
|
|
|
&cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, EnvVars: []string{"NTFY_DEBUG"}, Usage: "enable debug logging"},
|
2022-06-02 14:50:05 +00:00
|
|
|
&cli.BoolFlag{Name: "trace", EnvVars: []string{"NTFY_TRACE"}, Usage: "enable tracing (very verbose, be careful)"},
|
|
|
|
&cli.BoolFlag{Name: "no-log-dates", Aliases: []string{"no_log_dates"}, EnvVars: []string{"NTFY_NO_LOG_DATES"}, Usage: "disable the date/time prefix"},
|
2022-06-01 20:57:35 +00:00
|
|
|
altsrc.NewStringFlag(&cli.StringFlag{Name: "log-level", Aliases: []string{"log_level"}, Value: log.InfoLevel.String(), EnvVars: []string{"NTFY_LOG_LEVEL"}, Usage: "set log level"}),
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2022-05-30 02:14:14 +00:00
|
|
|
Flags: flagsDefault,
|
2022-06-01 20:57:35 +00:00
|
|
|
Before: initLogFunc,
|
2022-05-30 02:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 20:57:35 +00:00
|
|
|
func initLogFunc(c *cli.Context) error {
|
2022-06-02 14:50:05 +00:00
|
|
|
if c.Bool("trace") {
|
|
|
|
log.SetLevel(log.TraceLevel)
|
|
|
|
} else if c.Bool("debug") {
|
2022-06-01 20:57:35 +00:00
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(log.ToLevel(c.String("log-level")))
|
2021-10-24 01:29:45 +00:00
|
|
|
}
|
2022-06-02 14:50:05 +00:00
|
|
|
if c.Bool("no-log-dates") {
|
|
|
|
log.DisableDates()
|
|
|
|
}
|
2022-06-01 20:57:35 +00:00
|
|
|
return nil
|
2021-10-24 01:29:45 +00:00
|
|
|
}
|