diff --git a/internal/beatcmd/cmd.go b/internal/beatcmd/cmd.go index a312ebecdf..ddef906301 100644 --- a/internal/beatcmd/cmd.go +++ b/internal/beatcmd/cmd.go @@ -47,8 +47,11 @@ func NewRootCommand(beatParams BeatParams) *cobra.Command { // root command is an alias for "run" runCommand := genRunCmd(beatParams) rootCommand := &cobra.Command{ - Use: "apm-server", - RunE: runCommand.RunE, + Use: "apm-server", + RunE: func(cmd *cobra.Command, args []string) error { + cfgfile.HandleFlags() + return runCommand.RunE(cmd, args) + }, CompletionOptions: cobra.CompletionOptions{ DisableDefaultCmd: true, }, diff --git a/internal/beatcmd/init.go b/internal/beatcmd/init.go index bbf657006a..5afa7b2d5c 100644 --- a/internal/beatcmd/init.go +++ b/internal/beatcmd/init.go @@ -19,12 +19,9 @@ package beatcmd import ( cryptorand "crypto/rand" - "log" "math" "math/big" "math/rand" - "os" - "strings" "time" "github.com/elastic/beats/v7/libbeat/cfgfile" @@ -55,14 +52,11 @@ func initRand() { } func initFlags() { - // For backwards compatibility, convert -flags to --flags. - for i, arg := range os.Args[1:] { - if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 { - os.Args[1+i] = "-" + arg - } - } - - if err := cfgfile.HandleFlags(); err != nil { - log.Fatal(err) - } + // For backwards compatibility, initialize and convert known -flags to --flags. + cfgfile.Initialize() + cfgfile.AddAllowedBackwardsCompatibleFlag("v") + cfgfile.AddAllowedBackwardsCompatibleFlag("e") + cfgfile.AddAllowedBackwardsCompatibleFlag("d") + cfgfile.AddAllowedBackwardsCompatibleFlag("environment") + cfgfile.ConvertFlagsForBackwardsCompatibility() }