From 08b903a646c5af242a1f009a8ae57d247c43184b Mon Sep 17 00:00:00 2001 From: Mattias Jiderhamn Date: Tue, 27 Aug 2019 22:47:01 +0200 Subject: [PATCH] Use environment variables to locate Program Files on Windows (#6317) --- cmd/telegraf/telegraf.go | 6 +++++- internal/config/config.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 6eceb5cdc..a1303dc6c 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -403,12 +403,16 @@ func main() { } if runtime.GOOS == "windows" && windowsRunAsService() { + programFiles := os.Getenv("ProgramFiles") + if programFiles == "" { // Should never happen + programFiles = "C:\\Program Files" + } svcConfig := &service.Config{ Name: *fServiceName, DisplayName: *fServiceDisplayName, Description: "Collects data using a series of plugins and publishes it to" + "another series of plugins.", - Arguments: []string{"--config", "C:\\Program Files\\Telegraf\\telegraf.conf"}, + Arguments: []string{"--config", programFiles + "\\Telegraf\\telegraf.conf"}, } prg := &program{ diff --git a/internal/config/config.go b/internal/config/config.go index f2617e8b3..2357685a1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -641,7 +641,11 @@ func getDefaultConfigPath() (string, error) { homefile := os.ExpandEnv("${HOME}/.telegraf/telegraf.conf") etcfile := "/etc/telegraf/telegraf.conf" if runtime.GOOS == "windows" { - etcfile = `C:\Program Files\Telegraf\telegraf.conf` + programFiles := os.Getenv("ProgramFiles") + if programFiles == "" { // Should never happen + programFiles = `C:\Program Files` + } + etcfile = programFiles + `\Telegraf\telegraf.conf` } for _, path := range []string{envfile, homefile, etcfile} { if _, err := os.Stat(path); err == nil {