diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index e3af92e96..d1ed8a806 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -232,10 +232,13 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) { configFiles = append(configFiles, files...) } - // providing no "config" or "config-directory" flag(s) should load default - // configuration files + // load default config paths if none are found if len(configFiles) == 0 { - configFiles = append(configFiles, "") + defaultFiles, err := config.GetDefaultConfigPath() + if err != nil { + return nil, fmt.Errorf("unable to load default config paths: %w", err) + } + configFiles = append(configFiles, defaultFiles...) } t.configFiles = configFiles diff --git a/config/config.go b/config/config.go index 29f4aa526..06be5ada0 100644 --- a/config/config.go +++ b/config/config.go @@ -441,30 +441,17 @@ func isURL(str string) bool { // LoadConfig loads the given config files and applies it to c func (c *Config) LoadConfig(path string) error { - var err error - paths := []string{} - - if path == "" { - if paths, err = GetDefaultConfigPath(); err != nil { - return err - } - } else { - paths = append(paths, path) + if !c.Agent.Quiet { + log.Printf("I! Loading config: %s", path) } - for _, path := range paths { - if !c.Agent.Quiet { - log.Printf("I! Loading config: %s", path) - } + data, _, err := LoadConfigFile(path) + if err != nil { + return fmt.Errorf("error loading config file %s: %w", path, err) + } - data, _, err := LoadConfigFile(path) - if err != nil { - return fmt.Errorf("error loading config file %s: %w", path, err) - } - - if err = c.LoadConfigData(data); err != nil { - return fmt.Errorf("error loading config file %s: %w", path, err) - } + if err = c.LoadConfigData(data); err != nil { + return fmt.Errorf("error loading config file %s: %w", path, err) } return nil diff --git a/config/config_test.go b/config/config_test.go index a9befa4d2..709c88972 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -446,6 +446,7 @@ func TestConfig_AzureMonitorNamespacePrefix(t *testing.T) { func TestGetDefaultConfigPathFromEnvURL(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("[agent]\ndebug = true")) })) defer ts.Close() @@ -454,8 +455,7 @@ func TestGetDefaultConfigPathFromEnvURL(t *testing.T) { configPath, err := config.GetDefaultConfigPath() require.NoError(t, err) require.Equal(t, []string{ts.URL}, configPath) - err = c.LoadConfig("") - require.NoError(t, err) + require.NoError(t, c.LoadConfig(configPath[0])) } func TestConfig_URLLikeFileName(t *testing.T) {