From 13c2a340282e24783627bca0572ea86abce0a894 Mon Sep 17 00:00:00 2001 From: SystemGlitch Date: Mon, 24 Aug 2020 08:44:33 +0200 Subject: [PATCH] Add home directory to config file search paths (#1325) --- docs/src/docs/usage/configuration.mdx | 1 + pkg/config/reader.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/docs/src/docs/usage/configuration.mdx b/docs/src/docs/usage/configuration.mdx index af6db133..40db5ee1 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. diff --git a/pkg/config/reader.go b/pkg/config/reader.go index e6b18a7a..66583ada 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,13 @@ func (r *FileReader) setupConfigFileSearch() { curDir = newCurDir } + // find home directory for global config + 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) + } + r.log.Infof("Config search paths: %s", configSearchPaths) viper.SetConfigName(".golangci") for _, p := range configSearchPaths { @@ -186,6 +194,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) {