Skip to content

Commit

Permalink
Revert "Don't register cfgfile flags on flag.CommandLine in `func i…
Browse files Browse the repository at this point in the history
…nit()`" (#41152) (#41161)

* Revert "Don't register cfgfile flags on `flag.CommandLine` in `func init()` (…"

This reverts commit 07360ca.

* linter fixes

(cherry picked from commit 2ff38df)

Co-authored-by: Lee E Hinman <57081003+leehinman@users.noreply.github.com>
  • Loading branch information
mergify[bot] and leehinman authored Oct 7, 2024
1 parent 3853273 commit 67bbb70
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
21 changes: 3 additions & 18 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
package cfgfile

import (
"flag"
"fmt"
"os"
"path/filepath"
"sync"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/fleetmode"
Expand All @@ -35,10 +33,8 @@ var (
// The default config cannot include the beat name as it is not initialized
// when this variable is created. See ChangeDefaultCfgfileFlag which should
// be called prior to flags.Parse().
commandLine flag.FlagSet
commandLineOnce sync.Once
configfiles = config.StringArrFlag(&commandLine, "c", "beat.yml", "Configuration file, relative to path.config")
overwrites = config.SettingFlag(&commandLine, "E", "Configuration overwrite")
configfiles = config.StringArrFlag(nil, "c", "beat.yml", "Configuration file, relative to path.config")
overwrites = config.SettingFlag(nil, "E", "Configuration overwrite")

// Additional default settings, that must be available for variable expansion
defaults = config.MustNewConfigFrom(map[string]interface{}{
Expand All @@ -58,7 +54,7 @@ var (
func init() {
// add '-path.x' options overwriting paths in 'overwrites' config
makePathFlag := func(name, usage string) *string {
return config.ConfigOverwriteFlag(&commandLine, overwrites, name, name, "", usage)
return config.ConfigOverwriteFlag(nil, overwrites, name, name, "", usage)
}

homePath = makePathFlag("path.home", "Home path")
Expand All @@ -67,17 +63,6 @@ func init() {
makePathFlag("path.logs", "Logs path")
}

// InitFlags is for explicitly initializing the flags.
// It may get called repeatedly for different flagsets, but not
// twice for the same one.
func InitFlags() {
commandLineOnce.Do(func() {
commandLine.VisitAll(func(f *flag.Flag) {
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
})
})
}

// OverrideChecker checks if a config should be overwritten.
type OverrideChecker func(*config.C) bool

Expand Down
9 changes: 5 additions & 4 deletions libbeat/cmd/instance/beat_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"time"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/mock"
"github.com/elastic/elastic-agent-libs/config"
Expand Down Expand Up @@ -79,7 +78,6 @@ func (mb mockbeat) Stop() {
}

func TestMonitoringNameFromConfig(t *testing.T) {

mockBeat := mockbeat{
done: make(chan struct{}),
initDone: make(chan struct{}),
Expand All @@ -93,8 +91,6 @@ func TestMonitoringNameFromConfig(t *testing.T) {
go func() {
defer wg.Done()

// Initialize cfgfile flags
cfgfile.InitFlags()
// Set the configuration file path flag so the beat can read it
_ = flag.Set("c", "testdata/mockbeat.yml")
_ = instance.Run(mock.Settings, func(_ *beat.Beat, _ *config.C) (beat.Beater, error) {
Expand All @@ -118,10 +114,15 @@ func TestMonitoringNameFromConfig(t *testing.T) {
if err != nil {
t.Fatalf("error creating request: %v", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("calling state endpoint: %v", err)
}

if err != nil {
t.Fatal("calling state endpoint: ", err.Error())
}
defer resp.Body.Close()

beatName := struct {
Expand Down
3 changes: 0 additions & 3 deletions libbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings
panic(fmt.Errorf("failed to set default config file path: %w", err))
}

// Initialize the configuration flags.
cfgfile.InitFlags()

// must be updated prior to CLI flag handling.

rootCmd.RunCmd = genRunCmd(settings, beatCreator)
Expand Down

0 comments on commit 67bbb70

Please sign in to comment.