Feature: merge multiple "--config" and "--config-directory" flags (#9007)

This commit is contained in:
Gareth Dunstone 2021-05-19 07:20:13 +10:00 committed by GitHub
parent df47b41668
commit 3a1a44d67e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 14 deletions

View File

@ -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")
@ -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")
@ -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
}
if *fConfigDirectory != "" {
err = c.LoadDirectory(*fConfigDirectory)
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
}
}
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?")
}
@ -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()

View File

@ -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)