From a32eb7455bc38078ee04ae39619151c381bf548e Mon Sep 17 00:00:00 2001 From: Steffen Siering Date: Fri, 8 Jul 2016 18:37:51 +0200 Subject: [PATCH] The '-c' flag can be used multiple times (#1985) Change '-c' flag to load and merge all config files given on command line via '-c' flag. --- CHANGELOG.asciidoc | 3 ++- libbeat/cfgfile/cfgfile.go | 17 ++++++--------- libbeat/cfgfile/flags.go | 43 ++++++++++++++++++++++++++++++++++++++ libbeat/common/config.go | 12 +++++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 libbeat/cfgfile/flags.go diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index b919aa4f32a8..9c8f4d310bf0 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -8,7 +8,7 @@ // Template, add newest changes here === Beats version HEAD -https://github.com/elastic/beats/compare/v5.0.0-alpha3...master[Check the HEAD diff] +https://github.com/elastic/beats/compare/v5.0.0-alpha4...master[Check the HEAD diff] ==== Breaking changes @@ -50,6 +50,7 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha3...master[Check the HEAD d - Periodically log internal metrics. {pull}1955[1955] - Add enable-setting to all output modules. {pull}1987[1987] +- Command line flag -c can be used multiple times. {pull}1985[1985] *Metricbeat* diff --git a/libbeat/cfgfile/cfgfile.go b/libbeat/cfgfile/cfgfile.go index f26822000d29..27b6fda982cd 100644 --- a/libbeat/cfgfile/cfgfile.go +++ b/libbeat/cfgfile/cfgfile.go @@ -14,18 +14,13 @@ 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(). - configfile = flag.String("c", "beat.yml", "Configuration file") - testConfig = flag.Bool("configtest", false, "Test configuration and exit.") + configfiles = flagArgList("c", "beat.yml", "Configuration file") + testConfig = flag.Bool("configtest", false, "Test configuration and exit.") ) // ChangeDefaultCfgfileFlag replaces the value and default value for the `-c` // flag so that it reflects the beat name. func ChangeDefaultCfgfileFlag(beatName string) error { - cliflag := flag.Lookup("c") - if cliflag == nil { - return fmt.Errorf("Flag -c not found") - } - path, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { return fmt.Errorf("Failed to set default config file location because "+ @@ -33,9 +28,9 @@ func ChangeDefaultCfgfileFlag(beatName string) error { os.Args[0], err) } - cliflag.DefValue = filepath.Join(path, beatName+".yml") - - return cliflag.Value.Set(cliflag.DefValue) + path = filepath.Join(path, beatName+".yml") + configfiles.SetDefault(path) + return nil } // Deprecated: Please use Load(). @@ -57,7 +52,7 @@ func Read(out interface{}, path string) error { // line flag. func Load(path string) (*common.Config, error) { if path == "" { - path = *configfile + return common.LoadFiles(configfiles.list...) } return common.LoadFile(path) } diff --git a/libbeat/cfgfile/flags.go b/libbeat/cfgfile/flags.go new file mode 100644 index 000000000000..8976dbf21c02 --- /dev/null +++ b/libbeat/cfgfile/flags.go @@ -0,0 +1,43 @@ +package cfgfile + +import ( + "flag" + "strings" +) + +type argList struct { + list []string + isDefault bool +} + +func flagArgList(name string, def string, usage string) *argList { + l := &argList{ + list: []string{def}, + isDefault: true, + } + flag.Var(l, name, usage) + return l +} + +func (l *argList) SetDefault(v string) { + l.list = []string{v} + l.isDefault = true +} + +func (l *argList) String() string { + return strings.Join(l.list, ", ") +} + +func (l *argList) Set(v string) error { + if l.isDefault { + l.list = []string{v} + } else { + l.list = append(l.list, v) + } + l.isDefault = false + return nil +} + +func (l *argList) Get() interface{} { + return l.list +} diff --git a/libbeat/common/config.go b/libbeat/common/config.go index 5c532cb86662..420ad1f4a73e 100644 --- a/libbeat/common/config.go +++ b/libbeat/common/config.go @@ -2,6 +2,7 @@ package common import ( "github.com/elastic/go-ucfg" + "github.com/elastic/go-ucfg/cfgutil" "github.com/elastic/go-ucfg/yaml" ) @@ -38,6 +39,17 @@ func LoadFile(path string) (*Config, error) { return fromConfig(c), err } +func LoadFiles(paths ...string) (*Config, error) { + merger := cfgutil.NewCollector(nil, configOpts...) + for _, path := range paths { + err := merger.Add(yaml.NewConfigWithFile(path, configOpts...)) + if err != nil { + return nil, err + } + } + return fromConfig(merger.Config()), nil +} + func (c *Config) Merge(from interface{}) error { return c.access().Merge(from, configOpts...) }