Other: Make all command line flags as const strings

This commit is contained in:
Alexander Bilyak
2021-01-08 20:42:27 +01:00
parent 6fd614595d
commit 56bce8e06f
3 changed files with 48 additions and 27 deletions

View File

@ -61,6 +61,20 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
const (
flagCPUProfile = "cpu-prof"
flagCPUProfileShort = "p"
flagMemProfile = "mem-prof"
flagMemProfileShort = "m"
flagLogLevel = "log-level"
flagLogLevelShort = "l"
// FlagCLI indicate to start with command line interface
FlagCLI = "cli"
flagCLIShort = "c"
flagRestart = "restart"
flagLauncher = "launcher"
)
type Base struct { type Base struct {
CrashHandler *crash.Handler CrashHandler *crash.Handler
Locations *locations.Locations Locations *locations.Locations
@ -252,32 +266,32 @@ func (b *Base) NewApp(action func(*Base, *cli.Context) error) *cli.App {
app.Action = b.run(action) app.Action = b.run(action)
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "cpu-prof", Name: flagCPUProfile,
Aliases: []string{"p"}, Aliases: []string{flagCPUProfileShort},
Usage: "Generate CPU profile", Usage: "Generate CPU profile",
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "mem-prof", Name: flagMemProfile,
Aliases: []string{"m"}, Aliases: []string{flagMemProfileShort},
Usage: "Generate memory profile", Usage: "Generate memory profile",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "log-level", Name: flagLogLevel,
Aliases: []string{"l"}, Aliases: []string{flagLogLevelShort},
Usage: "Set the log level (one of panic, fatal, error, warn, info, debug)", Usage: "Set the log level (one of panic, fatal, error, warn, info, debug)",
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "cli", Name: FlagCLI,
Aliases: []string{"c"}, Aliases: []string{flagCLIShort},
Usage: "Use command line interface", Usage: "Use command line interface",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "restart", Name: flagRestart,
Usage: "The number of times the application has already restarted", Usage: "The number of times the application has already restarted",
Hidden: true, Hidden: true,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "launcher", Name: flagLauncher,
Usage: "The launcher to use to restart the application", Usage: "The launcher to use to restart the application",
Hidden: true, Hidden: true,
}, },
@ -302,21 +316,21 @@ func (b *Base) run(appMainLoop func(*Base, *cli.Context) error) cli.ActionFunc {
defer func() { _ = b.Lock.Close() }() defer func() { _ = b.Lock.Close() }()
// If launcher was used to start the app, use that for restart/autostart. // If launcher was used to start the app, use that for restart/autostart.
if launcher := c.String("launcher"); launcher != "" { if launcher := c.String(flagLauncher); launcher != "" {
b.Autostart.Exec = []string{launcher} b.Autostart.Exec = []string{launcher}
b.command = launcher b.command = launcher
} }
if doCPUProfile := c.Bool("cpu-prof"); doCPUProfile { if c.Bool(flagCPUProfile) {
startCPUProfile() startCPUProfile()
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
if doMemoryProfile := c.Bool("mem-prof"); doMemoryProfile { if c.Bool(flagMemProfile) {
defer makeMemoryProfile() defer makeMemoryProfile()
} }
logging.SetLevel(c.String("log-level")) logging.SetLevel(c.String(flagLogLevel))
logrus. logrus.
WithField("appName", b.Name). WithField("appName", b.Name).
@ -328,7 +342,7 @@ func (b *Base) run(appMainLoop func(*Base, *cli.Context) error) cli.ActionFunc {
Info("Run app") Info("Run app")
b.CrashHandler.AddRecoveryAction(func(interface{}) error { b.CrashHandler.AddRecoveryAction(func(interface{}) error {
if c.Int("restart") > maxAllowedRestarts { if c.Int(flagRestart) > maxAllowedRestarts {
logrus. logrus.
WithField("restart", c.Int("restart")). WithField("restart", c.Int("restart")).
Warn("Not restarting, already restarted too many times") Warn("Not restarting, already restarted too many times")

View File

@ -38,21 +38,28 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
const (
flagLogIMAP = "log-imap"
flagLogSMTP = "log-smtp"
flagNoWindow = "no-window"
flagNonInteractive = "noninteractive"
)
func New(base *base.Base) *cli.App { func New(base *base.Base) *cli.App {
app := base.NewApp(run) app := base.NewApp(run)
app.Flags = append(app.Flags, []cli.Flag{ app.Flags = append(app.Flags, []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "log-imap", Name: flagLogIMAP,
Usage: "Enable logging of IMAP communications (all|client|server) (may contain decrypted data!)"}, Usage: "Enable logging of IMAP communications (all|client|server) (may contain decrypted data!)"},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "log-smtp", Name: flagLogSMTP,
Usage: "Enable logging of SMTP communications (may contain decrypted data!)"}, Usage: "Enable logging of SMTP communications (may contain decrypted data!)"},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "no-window", Name: flagNoWindow,
Usage: "Don't show window after start"}, Usage: "Don't show window after start"},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "noninteractive", Name: flagNonInteractive,
Usage: "Start Bridge entirely noninteractively"}, Usage: "Start Bridge entirely noninteractively"},
}...) }...)
@ -79,8 +86,8 @@ func run(b *base.Base, c *cli.Context) error { // nolint[funlen]
imapPort := b.Settings.GetInt(settings.IMAPPortKey) imapPort := b.Settings.GetInt(settings.IMAPPortKey)
imap.NewIMAPServer( imap.NewIMAPServer(
b.CrashHandler, b.CrashHandler,
c.String("log-imap") == "client" || c.String("log-imap") == "all", c.String(flagLogIMAP) == "client" || c.String(flagLogIMAP) == "all",
c.String("log-imap") == "server" || c.String("log-imap") == "all", c.String(flagLogIMAP) == "server" || c.String(flagLogIMAP) == "all",
imapPort, tlsConfig, imapBackend, b.Listener).ListenAndServe() imapPort, tlsConfig, imapBackend, b.Listener).ListenAndServe()
}() }()
@ -89,12 +96,12 @@ func run(b *base.Base, c *cli.Context) error { // nolint[funlen]
smtpPort := b.Settings.GetInt(settings.SMTPPortKey) smtpPort := b.Settings.GetInt(settings.SMTPPortKey)
useSSL := b.Settings.GetBool(settings.SMTPSSLKey) useSSL := b.Settings.GetBool(settings.SMTPSSLKey)
smtp.NewSMTPServer( smtp.NewSMTPServer(
c.Bool("log-smtp"), c.Bool(flagLogSMTP),
smtpPort, useSSL, tlsConfig, smtpBackend, b.Listener).ListenAndServe() smtpPort, useSSL, tlsConfig, smtpBackend, b.Listener).ListenAndServe()
}() }()
// Bridge supports no-window option which we should use for autostart. // Bridge supports no-window option which we should use for autostart.
b.Autostart.Exec = append(b.Autostart.Exec, "--no-window") b.Autostart.Exec = append(b.Autostart.Exec, "--"+flagNoWindow)
// We want to remove old versions if the app exits successfully. // We want to remove old versions if the app exits successfully.
b.AddTeardownAction(b.Versioner.RemoveOldVersions) b.AddTeardownAction(b.Versioner.RemoveOldVersions)
@ -105,9 +112,9 @@ func run(b *base.Base, c *cli.Context) error { // nolint[funlen]
var frontendMode string var frontendMode string
switch { switch {
case c.Bool("cli"): case c.Bool(base.FlagCLI):
frontendMode = "cli" frontendMode = "cli"
case c.Bool("noninteractive"): case c.Bool(flagNonInteractive):
return <-(make(chan error)) // Block forever. return <-(make(chan error)) // Block forever.
default: default:
frontendMode = "qt" frontendMode = "qt"
@ -118,7 +125,7 @@ func run(b *base.Base, c *cli.Context) error { // nolint[funlen]
constants.BuildVersion, constants.BuildVersion,
b.Name, b.Name,
frontendMode, frontendMode,
!c.Bool("no-window"), !c.Bool(flagNoWindow),
b.CrashHandler, b.CrashHandler,
b.Locations, b.Locations,
b.Settings, b.Settings,

View File

@ -49,7 +49,7 @@ func run(b *base.Base, c *cli.Context) error {
var frontendMode string var frontendMode string
switch { switch {
case c.Bool("cli"): case c.Bool(base.FlagCLI):
frontendMode = "cli" frontendMode = "cli"
default: default:
frontendMode = "qt" frontendMode = "qt"