1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-23 10:12:38 +01:00
ntfy/cmd/app.go

91 lines
3.2 KiB
Go
Raw Normal View History

// Package cmd provides the ntfy CLI application
package cmd
import (
2023-02-04 04:21:50 +01:00
"fmt"
"github.com/urfave/cli/v2"
2022-06-01 22:57:35 +02:00
"github.com/urfave/cli/v2/altsrc"
2023-11-17 02:54:58 +01:00
"heckel.io/ntfy/v2/log"
"os"
2023-02-04 04:21:50 +01:00
"regexp"
)
2022-01-23 07:00:38 +01:00
const (
2022-01-23 21:30:30 +01:00
categoryClient = "Client commands"
categoryServer = "Server commands"
2022-01-23 07:00:38 +01:00
)
2022-05-09 17:03:40 +02:00
var commands = make([]*cli.Command, 0)
2022-05-30 04:14:14 +02:00
var flagsDefault = []cli.Flag{
&cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, EnvVars: []string{"NTFY_DEBUG"}, Usage: "enable debug logging"},
&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 22:57:35 +02: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"}),
2023-02-04 04:21:50 +01:00
altsrc.NewStringSliceFlag(&cli.StringSliceFlag{Name: "log-level-overrides", Aliases: []string{"log_level_overrides"}, EnvVars: []string{"NTFY_LOG_LEVEL_OVERRIDES"}, Usage: "set log level overrides"}),
2023-02-06 05:34:27 +01:00
altsrc.NewStringFlag(&cli.StringFlag{Name: "log-format", Aliases: []string{"log_format"}, Value: log.TextFormat.String(), EnvVars: []string{"NTFY_LOG_FORMAT"}, Usage: "set log format"}),
altsrc.NewStringFlag(&cli.StringFlag{Name: "log-file", Aliases: []string{"log_file"}, EnvVars: []string{"NTFY_LOG_FILE"}, Usage: "set log file, default is STDOUT"}),
2022-05-30 04:14:14 +02:00
}
2023-02-04 04:21:50 +01:00
var (
2023-02-10 02:49:45 +01:00
logLevelOverrideRegex = regexp.MustCompile(`(?i)^([^=\s]+)(?:\s*=\s*(\S+))?\s*->\s*(TRACE|DEBUG|INFO|WARN|ERROR)$`)
2023-02-04 04:21:50 +01: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 17:03:40 +02:00
Commands: commands,
2022-05-30 04:14:14 +02:00
Flags: flagsDefault,
2022-06-01 22:57:35 +02:00
Before: initLogFunc,
2022-05-30 04:14:14 +02:00
}
}
2022-06-01 22:57:35 +02:00
func initLogFunc(c *cli.Context) error {
2023-02-04 04:21:50 +01:00
log.SetLevel(log.ToLevel(c.String("log-level")))
log.SetFormat(log.ToFormat(c.String("log-format")))
if c.Bool("trace") {
log.SetLevel(log.TraceLevel)
} else if c.Bool("debug") {
2022-06-01 22:57:35 +02:00
log.SetLevel(log.DebugLevel)
}
if c.Bool("no-log-dates") {
log.DisableDates()
}
2023-02-04 04:21:50 +01:00
if err := applyLogLevelOverrides(c.StringSlice("log-level-overrides")); err != nil {
return err
}
2023-02-06 05:34:27 +01:00
logFile := c.String("log-file")
if logFile != "" {
w, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return err
}
log.SetOutput(w)
}
2023-02-04 04:21:50 +01:00
return nil
}
func applyLogLevelOverrides(rawOverrides []string) error {
for _, override := range rawOverrides {
m := logLevelOverrideRegex.FindStringSubmatch(override)
2023-02-10 02:49:45 +01:00
if len(m) == 4 {
field, value, level := m[1], m[2], m[3]
log.SetLevelOverride(field, value, log.ToLevel(level))
} else if len(m) == 3 {
field, level := m[1], m[2]
log.SetLevelOverride(field, "", log.ToLevel(level)) // Matches any value
} else {
2023-02-04 04:21:50 +01:00
return fmt.Errorf(`invalid log level override "%s", must be "field=value -> loglevel", e.g. "user_id=u_123 -> DEBUG"`, override)
}
}
2022-06-01 22:57:35 +02:00
return nil
}