Feature: merge multiple "--config" and "--config-directory" flags (#9007)
This commit is contained in:
parent
df47b41668
commit
3a1a44d67e
|
|
@ -29,6 +29,18 @@ import (
|
||||||
_ "github.com/influxdata/telegraf/plugins/processors/all"
|
_ "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
|
// If you update these, update usage.go and usage_windows.go
|
||||||
var fDebug = flag.Bool("debug", false,
|
var fDebug = flag.Bool("debug", false,
|
||||||
"turn on debug logging")
|
"turn on debug logging")
|
||||||
|
|
@ -38,9 +50,10 @@ var fQuiet = flag.Bool("quiet", false,
|
||||||
"run in quiet mode")
|
"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 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 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", "",
|
var fConfigs sliceFlags
|
||||||
"directory containing additional *.conf files")
|
var fConfigDirs sliceFlags
|
||||||
|
|
||||||
var fVersion = flag.Bool("version", false, "display the version and exit")
|
var fVersion = flag.Bool("version", false, "display the version and exit")
|
||||||
var fSampleConfig = flag.Bool("sample-config", false,
|
var fSampleConfig = flag.Bool("sample-config", false,
|
||||||
"print out full sample configuration")
|
"print out full sample configuration")
|
||||||
|
|
@ -133,17 +146,28 @@ func runAgent(ctx context.Context,
|
||||||
c := config.NewConfig()
|
c := config.NewConfig()
|
||||||
c.OutputFilters = outputFilters
|
c.OutputFilters = outputFilters
|
||||||
c.InputFilters = inputFilters
|
c.InputFilters = inputFilters
|
||||||
err := c.LoadConfig(*fConfig)
|
var err error
|
||||||
if err != nil {
|
// providing no "config" flag should load default config
|
||||||
return err
|
if len(fConfigs) == 0 {
|
||||||
}
|
err = c.LoadConfig("")
|
||||||
|
|
||||||
if *fConfigDirectory != "" {
|
|
||||||
err = c.LoadDirectory(*fConfigDirectory)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if !*fTest && len(c.Outputs) == 0 {
|
||||||
return errors.New("Error: no outputs found, did you provide a valid config file?")
|
return errors.New("Error: no outputs found, did you provide a valid config file?")
|
||||||
}
|
}
|
||||||
|
|
@ -245,6 +269,9 @@ func formatFullVersion() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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.Usage = func() { usageExit(0) }
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
|
||||||
|
|
@ -74,12 +74,17 @@ func runAsWindowsService(inputFilters, outputFilters []string) {
|
||||||
// Handle the --service flag here to prevent any issues with tooling that
|
// Handle the --service flag here to prevent any issues with tooling that
|
||||||
// may not have an interactive session, e.g. installing from Ansible.
|
// may not have an interactive session, e.g. installing from Ansible.
|
||||||
if *fService != "" {
|
if *fService != "" {
|
||||||
if *fConfig != "" {
|
if len(fConfigs) > 0 {
|
||||||
svcConfig.Arguments = []string{"--config", *fConfig}
|
svcConfig.Arguments = []string{}
|
||||||
}
|
}
|
||||||
if *fConfigDirectory != "" {
|
for _, fConfig := range fConfigs {
|
||||||
svcConfig.Arguments = append(svcConfig.Arguments, "--config-directory", *fConfigDirectory)
|
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
|
//set servicename to service cmd line, to have a custom name after relaunch as a service
|
||||||
svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", *fServiceName)
|
svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", *fServiceName)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue