Skip to content

Commit

Permalink
Feature: merge multiple "--config" and "--config-directory" flags (#9007
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gdunstone committed May 18, 2021
1 parent df47b41 commit 3a1a44d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
43 changes: 35 additions & 8 deletions cmd/telegraf/telegraf.go
Expand Up @@ -29,6 +29,18 @@ import (
_ "github.com/influxdata/telegraf/plugins/processors/all"
)

type sliceFlags []string

func (i *sliceFlags) String() string {
s := strings.Join(*i, " ")
return "[" + s + "]"
}

func (i *sliceFlags) Set(value string) error {
*i = append(*i, value)
return nil
}

// If you update these, update usage.go and usage_windows.go
var fDebug = flag.Bool("debug", false,
"turn on debug logging")
Expand All @@ -38,9 +50,10 @@ var fQuiet = flag.Bool("quiet", false,
"run in quiet mode")
var fTest = flag.Bool("test", false, "enable test mode: gather metrics, print them out, and exit. Note: Test mode only runs inputs, not processors, aggregators, or outputs")
var fTestWait = flag.Int("test-wait", 0, "wait up to this many seconds for service inputs to complete in test mode")
var fConfig = flag.String("config", "", "configuration file to load")
var fConfigDirectory = flag.String("config-directory", "",
"directory containing additional *.conf files")

var fConfigs sliceFlags
var fConfigDirs sliceFlags

var fVersion = flag.Bool("version", false, "display the version and exit")
var fSampleConfig = flag.Bool("sample-config", false,
"print out full sample configuration")
Expand Down Expand Up @@ -133,17 +146,28 @@ func runAgent(ctx context.Context,
c := config.NewConfig()
c.OutputFilters = outputFilters
c.InputFilters = inputFilters
err := c.LoadConfig(*fConfig)
if err != nil {
return err
var err error
// providing no "config" flag should load default config
if len(fConfigs) == 0 {
err = c.LoadConfig("")
if err != nil {
return err
}
}
for _, fConfig := range fConfigs {
err = c.LoadConfig(fConfig)
if err != nil {
return err
}
}

if *fConfigDirectory != "" {
err = c.LoadDirectory(*fConfigDirectory)
for _, fConfigDirectory := range fConfigDirs {
err = c.LoadDirectory(fConfigDirectory)
if err != nil {
return err
}
}

if !*fTest && len(c.Outputs) == 0 {
return errors.New("Error: no outputs found, did you provide a valid config file?")
}
Expand Down Expand Up @@ -245,6 +269,9 @@ func formatFullVersion() string {
}

func main() {
flag.Var(&fConfigs, "config", "configuration file to load")
flag.Var(&fConfigDirs, "config-directory", "directory containing additional *.conf files")

flag.Usage = func() { usageExit(0) }
flag.Parse()
args := flag.Args()
Expand Down
13 changes: 9 additions & 4 deletions cmd/telegraf/telegraf_windows.go
Expand Up @@ -74,12 +74,17 @@ func runAsWindowsService(inputFilters, outputFilters []string) {
// Handle the --service flag here to prevent any issues with tooling that
// may not have an interactive session, e.g. installing from Ansible.
if *fService != "" {
if *fConfig != "" {
svcConfig.Arguments = []string{"--config", *fConfig}
if len(fConfigs) > 0 {
svcConfig.Arguments = []string{}
}
if *fConfigDirectory != "" {
svcConfig.Arguments = append(svcConfig.Arguments, "--config-directory", *fConfigDirectory)
for _, fConfig := range fConfigs {
svcConfig.Arguments = append(svcConfig.Arguments, "--config", fConfig)
}

for _, fConfigDirectory := range fConfigDirs {
svcConfig.Arguments = append(svcConfig.Arguments, "--config-directory", fConfigDirectory)
}

//set servicename to service cmd line, to have a custom name after relaunch as a service
svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", *fServiceName)

Expand Down

0 comments on commit 3a1a44d

Please sign in to comment.