Skip to content

Commit 2ff38df

Browse files
authored
Revert "Don't register cfgfile flags on flag.CommandLine in func init()" (#41152)
* Revert "Don't register cfgfile flags on `flag.CommandLine` in `func init()` (…" This reverts commit 07360ca. * linter fixes
1 parent 6d4fbfc commit 2ff38df

File tree

3 files changed

+8
-25
lines changed

3 files changed

+8
-25
lines changed

libbeat/cfgfile/cfgfile.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
package cfgfile
1919

2020
import (
21-
"flag"
2221
"fmt"
2322
"os"
2423
"path/filepath"
25-
"sync"
2624

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

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

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

70-
// InitFlags is for explicitly initializing the flags.
71-
// It may get called repeatedly for different flagsets, but not
72-
// twice for the same one.
73-
func InitFlags() {
74-
commandLineOnce.Do(func() {
75-
commandLine.VisitAll(func(f *flag.Flag) {
76-
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
77-
})
78-
})
79-
}
80-
8166
// OverrideChecker checks if a config should be overwritten.
8267
type OverrideChecker func(*config.C) bool
8368

libbeat/cmd/instance/beat_integration_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"time"
2828

2929
"github.com/elastic/beats/v7/libbeat/beat"
30-
"github.com/elastic/beats/v7/libbeat/cfgfile"
3130
"github.com/elastic/beats/v7/libbeat/cmd/instance"
3231
"github.com/elastic/beats/v7/libbeat/mock"
3332
"github.com/elastic/elastic-agent-libs/config"
@@ -79,7 +78,6 @@ func (mb mockbeat) Stop() {
7978
}
8079

8180
func TestMonitoringNameFromConfig(t *testing.T) {
82-
8381
mockBeat := mockbeat{
8482
done: make(chan struct{}),
8583
initDone: make(chan struct{}),
@@ -93,8 +91,6 @@ func TestMonitoringNameFromConfig(t *testing.T) {
9391
go func() {
9492
defer wg.Done()
9593

96-
// Initialize cfgfile flags
97-
cfgfile.InitFlags()
9894
// Set the configuration file path flag so the beat can read it
9995
_ = flag.Set("c", "testdata/mockbeat.yml")
10096
_ = instance.Run(mock.Settings, func(_ *beat.Beat, _ *config.C) (beat.Beater, error) {
@@ -118,10 +114,15 @@ func TestMonitoringNameFromConfig(t *testing.T) {
118114
if err != nil {
119115
t.Fatalf("error creating request: %v", err)
120116
}
117+
121118
resp, err := http.DefaultClient.Do(req)
122119
if err != nil {
123120
t.Fatalf("calling state endpoint: %v", err)
124121
}
122+
123+
if err != nil {
124+
t.Fatal("calling state endpoint: ", err.Error())
125+
}
125126
defer resp.Body.Close()
126127

127128
beatName := struct {

libbeat/cmd/root.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings
8181
panic(fmt.Errorf("failed to set default config file path: %w", err))
8282
}
8383

84-
// Initialize the configuration flags.
85-
cfgfile.InitFlags()
86-
8784
// must be updated prior to CLI flag handling.
8885

8986
rootCmd.RunCmd = genRunCmd(settings, beatCreator)

0 commit comments

Comments
 (0)