Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '-E' CLI flag #1986

Merged
merged 1 commit into from
Jul 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha4...master[Check the HEAD d
- Add enable-setting to all output modules. {pull}1987[1987]
- Command line flag -c can be used multiple times. {pull}1985[1985]
- Add OR/AND/NOT to the condition associated with the processors. {pull}1983[1983]
- Add '-E' CLI flag for overwriting single config options via command line. {pull}1986[1986]

*Metricbeat*

Expand Down
18 changes: 16 additions & 2 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
// when this variable is created. See ChangeDefaultCfgfileFlag which should
// be called prior to flags.Parse().
configfiles = flagArgList("c", "beat.yml", "Configuration file")
overwrites = common.NewFlagConfig(nil, nil, "E", "Configuration overwrite")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the -E flag be used multiple times to overwrite multiple variables?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Functionality provided by go-ucfg. Yes I need to document ucfg capabilities ;)

testConfig = flag.Bool("configtest", false, "Test configuration and exit.")
)

Expand Down Expand Up @@ -51,10 +52,23 @@ func Read(out interface{}, path string) error {
// this method reads from the configuration file specified by the '-c' command
// line flag.
func Load(path string) (*common.Config, error) {
var config *common.Config
var err error

if path == "" {
return common.LoadFiles(configfiles.list...)
config, err = common.LoadFiles(configfiles.list...)
} else {
config, err = common.LoadFile(path)
}
if err != nil {
return nil, err
}

err = config.Merge(overwrites)
if err != nil {
return nil, err
}
return common.LoadFile(path)
return config, nil
}

// IsTestConfig returns whether or not this is configuration used for testing
Expand Down
25 changes: 25 additions & 0 deletions libbeat/common/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package common

import (
"flag"

"github.com/elastic/go-ucfg"
"github.com/elastic/go-ucfg/cfgutil"
cfgflag "github.com/elastic/go-ucfg/flag"
"github.com/elastic/go-ucfg/yaml"
)

Expand Down Expand Up @@ -34,6 +37,28 @@ func NewConfigWithYAML(in []byte, source string) (*Config, error) {
return fromConfig(c), err
}

func NewFlagConfig(
set *flag.FlagSet,
def *Config,
name string,
usage string,
) *Config {
opts := append(
[]ucfg.Option{
ucfg.MetaData(ucfg.Meta{"command line flag"}),
},
configOpts...,
)

var to *ucfg.Config
if def != nil {
to = def.access()
}

config := cfgflag.ConfigVar(set, to, name, usage, opts...)
return fromConfig(config)
}

func LoadFile(path string) (*Config, error) {
c, err := yaml.NewConfigWithFile(path, configOpts...)
return fromConfig(c), err
Expand Down
23 changes: 23 additions & 0 deletions libbeat/tests/system/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ def test_invalid_config(self):
assert exit_code == 1
assert self.log_contains("error loading config file") is True

def test_invalid_config_cli_param(self):
"""
Checks CLI overwrite actually overwrites some config variable by
writing an invalid value.
"""

self.render_config_template(
console={"pretty": "false"}
)

# first run with default config, validating config being
# actually correct.
proc = self.start_beat()
self.wait_until(lambda: self.log_contains("Setup Beat"))
proc.check_kill_and_wait()

# start beat with invalid config setting on command line
exit_code = self.run_beat(
extra_args=["-E", "output.console=invalid"])

assert exit_code == 1
assert self.log_contains("error unpacking config data") is True

def test_config_test(self):
"""
Checks if -configtest works as expected
Expand Down