ntfy/cmd/app.go

54 lines
1.4 KiB
Go
Raw Normal View History

// Package cmd provides the ntfy CLI application
package cmd
import (
"github.com/urfave/cli/v2"
2022-05-30 04:14:14 +02:00
"heckel.io/ntfy/log"
"os"
)
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.StringFlag{Name: "log-level", Aliases: []string{"log_level"}, Value: log.InfoLevel.String(), EnvVars: []string{"NTFY_LOG_LEVEL"}, Usage: "set log level"},
}
// 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,
Before: initLogFunc(nil),
}
}
func initLogFunc(next cli.BeforeFunc) cli.BeforeFunc {
return func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.ToLevel(c.String("log-level")))
}
if next != nil {
if err := next(c); err != nil {
return err
}
}
return nil
}
}