Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 35 additions & 8 deletions cmd/telegraf/telegraf.go
Expand Up @@ -28,6 +28,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 @@ -37,9 +49,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 @@ -124,17 +137,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 @@ -236,6 +260,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 @@ -84,12 +84,17 @@ func runAsWindowsService(inputFilters, outputFilters, aggregatorFilters, process
// 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{}
gdunstone marked this conversation as resolved.
Show resolved Hide resolved
}
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