Skip to content

Commit

Permalink
refactor beats argument parsing (#41277)
Browse files Browse the repository at this point in the history
* modify beats argument parsing to not happen during init
* allowlist for backwards compatible args, to conver -arg to --arg

Co-authored-by: Tiago Queiroz <[email protected]>

(cherry picked from commit 15eae5f)
  • Loading branch information
leehinman authored and mergify[bot] committed Oct 22, 2024
1 parent 7895d5e commit a104518
Show file tree
Hide file tree
Showing 40 changed files with 338 additions and 146 deletions.
74 changes: 37 additions & 37 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25581,6 +25581,43 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/term
Version: v0.24.0
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/golang.org/x/[email protected]/LICENSE:

Copyright 2009 The Go Authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/text
Version: v0.18.0
Expand Down Expand Up @@ -57052,43 +57089,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/term
Version: v0.24.0
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/golang.org/x/[email protected]/LICENSE:

Copyright 2009 The Go Authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/xerrors
Version: v0.0.0-20231012003039-104605ab7028
Expand Down
4 changes: 4 additions & 0 deletions auditbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/elastic/beats/v7/auditbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
)

Expand All @@ -34,11 +35,14 @@ func init() {
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")

cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
func TestSystem(*testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
7 changes: 7 additions & 0 deletions filebeat/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/elastic/beats/v7/filebeat/generator/fields"
"github.com/elastic/beats/v7/filebeat/generator/fileset"
"github.com/elastic/beats/v7/filebeat/generator/module"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/common/cli"
"github.com/elastic/elastic-agent-libs/paths"
)
Expand Down Expand Up @@ -63,7 +64,9 @@ func genGenerateModuleCmd() *cobra.Command {
}

genModuleCmd.Flags().String("modules-path", defaultHomePath, "Path to modules directory")
cfgfile.AddAllowedBackwardsCompatibleFlag("modules-path")
genModuleCmd.Flags().String("es-beats", defaultHomePath, "Path to Elastic Beats")
cfgfile.AddAllowedBackwardsCompatibleFlag("es-beats")

return genModuleCmd
}
Expand All @@ -88,7 +91,9 @@ func genGenerateFilesetCmd() *cobra.Command {
}

genFilesetCmd.Flags().String("modules-path", defaultHomePath, "Path to modules directory")
cfgfile.AddAllowedBackwardsCompatibleFlag("modules-path")
genFilesetCmd.Flags().String("es-beats", defaultHomePath, "Path to Elastic Beats")
cfgfile.AddAllowedBackwardsCompatibleFlag("es-beats")

return genFilesetCmd
}
Expand All @@ -113,7 +118,9 @@ func genGenerateFieldsCmd() *cobra.Command {
}

genFieldsCmd.Flags().String("es-beats", defaultHomePath, "Path to Elastic Beats")
cfgfile.AddAllowedBackwardsCompatibleFlag("es-beats")
genFieldsCmd.Flags().Bool("without-documentation", false, "Do not add description fields")
cfgfile.AddAllowedBackwardsCompatibleFlag("without-documentation")

return genFieldsCmd
}
5 changes: 5 additions & 0 deletions filebeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/elastic/beats/v7/filebeat/fileset"
"github.com/elastic/beats/v7/filebeat/include"
"github.com/elastic/beats/v7/filebeat/input"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"

Expand All @@ -49,7 +50,9 @@ func FilebeatSettings(moduleNameSpace string) instance.Settings {
}
runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("once"))
cfgfile.AddAllowedBackwardsCompatibleFlag("once")
runFlags.AddGoFlag(flag.CommandLine.Lookup("modules"))
cfgfile.AddAllowedBackwardsCompatibleFlag("modules")
return instance.Settings{
RunFlags: runFlags,
Name: Name,
Expand All @@ -66,8 +69,10 @@ func FilebeatSettings(moduleNameSpace string) instance.Settings {
func Filebeat(inputs beater.PluginFactory, settings instance.Settings) *cmd.BeatsRootCmd {
command := cmd.GenRootCmdWithSettings(beater.New(inputs), settings)
command.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("M"))
cfgfile.AddAllowedBackwardsCompatibleFlag("M")
command.TestCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
command.SetupCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
cfgfile.AddAllowedBackwardsCompatibleFlag("modules")
command.AddCommand(cmd.GenModulesCmd(Name, "", buildModulesManager))
command.AddCommand(genGenerateCmd())
return command
Expand Down
4 changes: 4 additions & 0 deletions filebeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

fbcmd "github.com/elastic/beats/v7/filebeat/cmd"
inputs "github.com/elastic/beats/v7/filebeat/input/default-inputs"
"github.com/elastic/beats/v7/libbeat/cfgfile"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
)
Expand All @@ -40,11 +41,14 @@ func init() {
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
fbCommand = fbcmd.Filebeat(inputs.Init, fbcmd.FilebeatSettings(""))
fbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
fbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
if err := fbCommand.Execute(); err != nil {
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ require (
go.opentelemetry.io/collector/consumer v0.109.0
go.opentelemetry.io/collector/pdata v1.15.0
go.opentelemetry.io/collector/receiver v0.109.0
golang.org/x/term v0.24.0
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)
Expand Down Expand Up @@ -382,7 +383,6 @@ require (
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.uber.org/ratelimit v0.3.1 // indirect
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions heartbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/elastic/beats/v7/heartbeat/beater"
"github.com/elastic/beats/v7/heartbeat/include"
"github.com/elastic/beats/v7/libbeat/cfgfile"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/ecs"
Expand Down Expand Up @@ -81,6 +82,7 @@ func Initialize(settings instance.Settings) *cmd.BeatsRootCmd {
`
setup.ResetFlags()
setup.Flags().Bool(cmd.IndexManagementKey, false, "Setup all components related to Elasticsearch index management, including template, ilm policy and rollover alias")
cfgfile.AddAllowedBackwardsCompatibleFlag(cmd.IndexManagementKey)

return rootCmd
}
Expand Down
4 changes: 4 additions & 0 deletions heartbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/elastic/beats/v7/heartbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
)

Expand All @@ -33,11 +34,14 @@ func init() {
testing.Init()
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
func TestSystem(_ *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
}
Expand Down
Loading

0 comments on commit a104518

Please sign in to comment.