Skip to content

Commit

Permalink
Merge pull request elastic#99 from tsg/use_beat_name_for_config
Browse files Browse the repository at this point in the history
Use the Beat name in the default configuration file path
  • Loading branch information
ruflin committed Sep 23, 2015
2 parents 31f6dff + f29478a commit 515b660
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions beat/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 515b660

Please sign in to comment.