Skip to content

Commit

Permalink
Add '-E' CLI flag (#1986)
Browse files Browse the repository at this point in the history
Add '-E' CLI flag for overwriting single config options via CLI. For example:

`-E output.console.pretty: true` enabled console output with pretty printing
enabled.
  • Loading branch information
Steffen Siering authored and ruflin committed Jul 12, 2016
1 parent f1ed821 commit 5d6083d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
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")
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

0 comments on commit 5d6083d

Please sign in to comment.