From 48f9b949e7aabbe7c3e5981fee802e2d884802d9 Mon Sep 17 00:00:00 2001 From: SystemGlitch Date: Fri, 21 Aug 2020 16:32:32 +0200 Subject: [PATCH 1/4] Add home directory to config file search paths #1324 --- pkg/config/reader.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/config/reader.go b/pkg/config/reader.go index e6b18a7a690c..8a0b4ae26d89 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -170,6 +170,7 @@ func (r *FileReader) setupConfigFileSearch() { // find all dirs from it up to the root configSearchPaths := []string{"./"} + for { configSearchPaths = append(configSearchPaths, curDir) newCurDir := filepath.Dir(curDir) @@ -179,6 +180,14 @@ func (r *FileReader) setupConfigFileSearch() { curDir = newCurDir } + // find home directory for global config + home, err := homedir.Dir() + if err != nil { + r.log.Warnf("Can't get user's home directory: %s", home) + } else { + configSearchPaths = append(configSearchPaths, home) + } + r.log.Infof("Config search paths: %s", configSearchPaths) viper.SetConfigName(".golangci") for _, p := range configSearchPaths { From 4255b97f2829efae305188fa03324cd74b5c9ffa Mon Sep 17 00:00:00 2001 From: SystemGlitch Date: Fri, 21 Aug 2020 16:44:52 +0200 Subject: [PATCH 2/4] Document home directory config search path --- docs/src/docs/usage/configuration.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/docs/usage/configuration.mdx b/docs/src/docs/usage/configuration.mdx index af6db1336f27..40db5ee12704 100644 --- a/docs/src/docs/usage/configuration.mdx +++ b/docs/src/docs/usage/configuration.mdx @@ -28,6 +28,7 @@ GolangCI-Lint looks for config files in the following paths from the current wor - `.golangci.json` GolangCI-Lint also searches for config files in all directories from the directory of the first analyzed path up to the root. +If no configuration file has been found, GolangCI-Lint will try to find one in your home directory. To see which config file is being used and where it was sourced from run golangci-lint with `-v` option. Config options inside the file are identical to command-line options. From b5ca52de4a96d08869900d387607b8244d5e9986 Mon Sep 17 00:00:00 2001 From: SystemGlitch Date: Fri, 21 Aug 2020 17:24:17 +0200 Subject: [PATCH 3/4] Prevent duplicate home path in config search paths --- pkg/config/reader.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/config/reader.go b/pkg/config/reader.go index 8a0b4ae26d89..75a2b2e2735e 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -184,7 +184,7 @@ func (r *FileReader) setupConfigFileSearch() { home, err := homedir.Dir() if err != nil { r.log.Warnf("Can't get user's home directory: %s", home) - } else { + } else if !sliceContains(configSearchPaths, home) { configSearchPaths = append(configSearchPaths, home) } @@ -195,6 +195,15 @@ func (r *FileReader) setupConfigFileSearch() { } } +func sliceContains(slice []string, value string) bool { + for _, v := range slice { + if v == value { + return true + } + } + return false +} + var errConfigDisabled = errors.New("config is disabled by --no-config") func (r *FileReader) parseConfigOption() (string, error) { From 2481cdfeb7bc2b5809fb65c734f4edfb4682d6ca Mon Sep 17 00:00:00 2001 From: SystemGlitch Date: Sat, 22 Aug 2020 18:23:21 +0200 Subject: [PATCH 4/4] Print home directory error instead of empty string --- pkg/config/reader.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/config/reader.go b/pkg/config/reader.go index 75a2b2e2735e..66583ada085d 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -181,9 +181,8 @@ func (r *FileReader) setupConfigFileSearch() { } // find home directory for global config - home, err := homedir.Dir() - if err != nil { - r.log.Warnf("Can't get user's home directory: %s", home) + if home, err := homedir.Dir(); err != nil { + r.log.Warnf("Can't get user's home directory: %s", err.Error()) } else if !sliceContains(configSearchPaths, home) { configSearchPaths = append(configSearchPaths, home) }