From 67bbb7093b71b544ae0dac5faa9c77da660f6d64 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:58:45 +0000 Subject: [PATCH] Revert "Don't register cfgfile flags on `flag.CommandLine` in `func init()`" (#41152) (#41161) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Revert "Don't register cfgfile flags on `flag.CommandLine` in `func init()` (…" This reverts commit 07360ca6f8df2fdd4e0a4d0e180c65967c96ff51. * linter fixes (cherry picked from commit 2ff38df906203ba041d1f5492a6007809ad0b066) Co-authored-by: Lee E Hinman <57081003+leehinman@users.noreply.github.com> --- libbeat/cfgfile/cfgfile.go | 21 +++---------------- libbeat/cmd/instance/beat_integration_test.go | 9 ++++---- libbeat/cmd/root.go | 3 --- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/libbeat/cfgfile/cfgfile.go b/libbeat/cfgfile/cfgfile.go index 2b88aaad157..ca19af8cb9f 100644 --- a/libbeat/cfgfile/cfgfile.go +++ b/libbeat/cfgfile/cfgfile.go @@ -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" @@ -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{}{ @@ -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") @@ -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 diff --git a/libbeat/cmd/instance/beat_integration_test.go b/libbeat/cmd/instance/beat_integration_test.go index 4c2aa94bd1d..baf7657665d 100644 --- a/libbeat/cmd/instance/beat_integration_test.go +++ b/libbeat/cmd/instance/beat_integration_test.go @@ -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" @@ -79,7 +78,6 @@ func (mb mockbeat) Stop() { } func TestMonitoringNameFromConfig(t *testing.T) { - mockBeat := mockbeat{ done: make(chan struct{}), initDone: make(chan struct{}), @@ -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) { @@ -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 { diff --git a/libbeat/cmd/root.go b/libbeat/cmd/root.go index 671b72fde1e..589d706fc61 100644 --- a/libbeat/cmd/root.go +++ b/libbeat/cmd/root.go @@ -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)