Skip to content

Commit

Permalink
fix: move beats cfgfile.HandleFlags call after the flags are parsed (#…
Browse files Browse the repository at this point in the history
…14512)

* fix: move beats cfgfile.HandleFlags call after the flags are parsed

* fix: beats call to ConvertFlagsForBackwardsCompatibility on init

* fix: beatcmd add remaining flag into backward compatibble flags list
  • Loading branch information
1pkg authored Nov 4, 2024
1 parent ec3c4c3 commit e2c0fb5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
7 changes: 5 additions & 2 deletions internal/beatcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
20 changes: 7 additions & 13 deletions internal/beatcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}

0 comments on commit e2c0fb5

Please sign in to comment.