From 56bce8e06f79555852cdf43325ece4212a0dc6be Mon Sep 17 00:00:00 2001 From: Alexander Bilyak Date: Fri, 8 Jan 2021 20:42:27 +0100 Subject: [PATCH] Other: Make all command line flags as const strings --- internal/app/base/base.go | 44 +++++++++++++++++++++++------------ internal/app/bridge/bridge.go | 29 ++++++++++++++--------- internal/app/ie/ie.go | 2 +- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/internal/app/base/base.go b/internal/app/base/base.go index 09724d51..65893546 100644 --- a/internal/app/base/base.go +++ b/internal/app/base/base.go @@ -61,6 +61,20 @@ import ( "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 { CrashHandler *crash.Handler 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.Flags = []cli.Flag{ &cli.BoolFlag{ - Name: "cpu-prof", - Aliases: []string{"p"}, + Name: flagCPUProfile, + Aliases: []string{flagCPUProfileShort}, Usage: "Generate CPU profile", }, &cli.BoolFlag{ - Name: "mem-prof", - Aliases: []string{"m"}, + Name: flagMemProfile, + Aliases: []string{flagMemProfileShort}, Usage: "Generate memory profile", }, &cli.StringFlag{ - Name: "log-level", - Aliases: []string{"l"}, + Name: flagLogLevel, + Aliases: []string{flagLogLevelShort}, Usage: "Set the log level (one of panic, fatal, error, warn, info, debug)", }, &cli.BoolFlag{ - Name: "cli", - Aliases: []string{"c"}, + Name: FlagCLI, + Aliases: []string{flagCLIShort}, Usage: "Use command line interface", }, &cli.StringFlag{ - Name: "restart", + Name: flagRestart, Usage: "The number of times the application has already restarted", Hidden: true, }, &cli.StringFlag{ - Name: "launcher", + Name: flagLauncher, Usage: "The launcher to use to restart the application", Hidden: true, }, @@ -302,21 +316,21 @@ func (b *Base) run(appMainLoop func(*Base, *cli.Context) error) cli.ActionFunc { defer func() { _ = b.Lock.Close() }() // 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.command = launcher } - if doCPUProfile := c.Bool("cpu-prof"); doCPUProfile { + if c.Bool(flagCPUProfile) { startCPUProfile() defer pprof.StopCPUProfile() } - if doMemoryProfile := c.Bool("mem-prof"); doMemoryProfile { + if c.Bool(flagMemProfile) { defer makeMemoryProfile() } - logging.SetLevel(c.String("log-level")) + logging.SetLevel(c.String(flagLogLevel)) logrus. WithField("appName", b.Name). @@ -328,7 +342,7 @@ func (b *Base) run(appMainLoop func(*Base, *cli.Context) error) cli.ActionFunc { Info("Run app") b.CrashHandler.AddRecoveryAction(func(interface{}) error { - if c.Int("restart") > maxAllowedRestarts { + if c.Int(flagRestart) > maxAllowedRestarts { logrus. WithField("restart", c.Int("restart")). Warn("Not restarting, already restarted too many times") diff --git a/internal/app/bridge/bridge.go b/internal/app/bridge/bridge.go index caa74d9d..678cdb4c 100644 --- a/internal/app/bridge/bridge.go +++ b/internal/app/bridge/bridge.go @@ -38,21 +38,28 @@ import ( "github.com/urfave/cli/v2" ) +const ( + flagLogIMAP = "log-imap" + flagLogSMTP = "log-smtp" + flagNoWindow = "no-window" + flagNonInteractive = "noninteractive" +) + func New(base *base.Base) *cli.App { app := base.NewApp(run) app.Flags = append(app.Flags, []cli.Flag{ &cli.StringFlag{ - Name: "log-imap", + Name: flagLogIMAP, Usage: "Enable logging of IMAP communications (all|client|server) (may contain decrypted data!)"}, &cli.BoolFlag{ - Name: "log-smtp", + Name: flagLogSMTP, Usage: "Enable logging of SMTP communications (may contain decrypted data!)"}, &cli.BoolFlag{ - Name: "no-window", + Name: flagNoWindow, Usage: "Don't show window after start"}, &cli.BoolFlag{ - Name: "noninteractive", + Name: flagNonInteractive, 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) imap.NewIMAPServer( b.CrashHandler, - c.String("log-imap") == "client" || c.String("log-imap") == "all", - c.String("log-imap") == "server" || c.String("log-imap") == "all", + c.String(flagLogIMAP) == "client" || c.String(flagLogIMAP) == "all", + c.String(flagLogIMAP) == "server" || c.String(flagLogIMAP) == "all", 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) useSSL := b.Settings.GetBool(settings.SMTPSSLKey) smtp.NewSMTPServer( - c.Bool("log-smtp"), + c.Bool(flagLogSMTP), smtpPort, useSSL, tlsConfig, smtpBackend, b.Listener).ListenAndServe() }() // 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. b.AddTeardownAction(b.Versioner.RemoveOldVersions) @@ -105,9 +112,9 @@ func run(b *base.Base, c *cli.Context) error { // nolint[funlen] var frontendMode string switch { - case c.Bool("cli"): + case c.Bool(base.FlagCLI): frontendMode = "cli" - case c.Bool("noninteractive"): + case c.Bool(flagNonInteractive): return <-(make(chan error)) // Block forever. default: frontendMode = "qt" @@ -118,7 +125,7 @@ func run(b *base.Base, c *cli.Context) error { // nolint[funlen] constants.BuildVersion, b.Name, frontendMode, - !c.Bool("no-window"), + !c.Bool(flagNoWindow), b.CrashHandler, b.Locations, b.Settings, diff --git a/internal/app/ie/ie.go b/internal/app/ie/ie.go index 47494d27..7f632b61 100644 --- a/internal/app/ie/ie.go +++ b/internal/app/ie/ie.go @@ -49,7 +49,7 @@ func run(b *base.Base, c *cli.Context) error { var frontendMode string switch { - case c.Bool("cli"): + case c.Bool(base.FlagCLI): frontendMode = "cli" default: frontendMode = "qt"