diff --git a/beat/beat.go b/beat/beat.go index 7fd46c453d7..7485d1adfe9 100644 --- a/beat/beat.go +++ b/beat/beat.go @@ -59,6 +59,13 @@ func NewBeat(name string, version string, bt Beater) *Beat { // To set additional cmd line args use the beat.CmdLine type before calling the function func (beat *Beat) CommandLineSetup() { + // The -c flag is treated separately because it needs the Beat name + err := cfgfile.ChangeDefaultCfgfileFlag(beat.Name) + if err != nil { + fmt.Printf("Failed to fix the -c flag: %v\n", err) + os.Exit(1) + } + flag.Parse() if *printVersion { diff --git a/cfgfile/cfgfile.go b/cfgfile/cfgfile.go index 1c441410117..097f51fcef6 100644 --- a/cfgfile/cfgfile.go +++ b/cfgfile/cfgfile.go @@ -13,11 +13,23 @@ var configfile *string var testConfig *bool func init() { - // The default config cannot include the beat name as it is not initialised when this function is called + // The default config cannot include the beat name as it is not initialised when this + // function is called, but see ChangeDefaultCfgfileFlag configfile = flag.String("c", "/etc/beat/beat.yml", "Configuration file") testConfig = flag.Bool("test", 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") + } + cliflag.DefValue = fmt.Sprintf("/etc/%s/%s.yml", beatName, beatName) + return cliflag.Value.Set(cliflag.DefValue) +} + // Read reads the configuration from a yaml file into the given interface structure. // In case path is not set this method reads from the default configuration file for the beat. func Read(out interface{}, path string) error {