Skip to content

feat: support searching for config in XDG_CONFIG_HOME and $HOME/.config #4974

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ func (l *Loader) setConfigFile() error {
l.viper.SetConfigType("yaml")
}
} else {
l.setupConfigFileSearch()
l.setupRecursiveConfigFileSearch()

// If recursive search didn't find any config file, try searching in the global config paths.
if err := l.viper.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
if errors.As(err, &configFileNotFoundError) {
l.setupGlobalConfigFileSearch()
}
}
}

return nil
Expand All @@ -136,10 +144,10 @@ func (l *Loader) evaluateOptions() (string, error) {
return configFile, nil
}

func (l *Loader) setupConfigFileSearch() {
func (l *Loader) setupRecursiveConfigFileSearch() {
l.viper.SetConfigName(".golangci")

configSearchPaths := l.getConfigSearchPaths()
configSearchPaths := l.getRecursiveConfigSearchPaths()

l.log.Infof("Config search paths: %s", configSearchPaths)

Expand All @@ -148,7 +156,19 @@ func (l *Loader) setupConfigFileSearch() {
}
}

func (l *Loader) getConfigSearchPaths() []string {
func (l *Loader) setupGlobalConfigFileSearch() {
l.viper.SetConfigName("golangci")

configSearchPaths := l.getGlobalConfigSearchPaths()

l.log.Infof("Global config search paths: %s", configSearchPaths)

for _, p := range configSearchPaths {
l.viper.AddConfigPath(p)
}
}

func (l *Loader) getRecursiveConfigSearchPaths() []string {
firstArg := "./..."
if len(l.args) > 0 {
firstArg = l.args[0]
Expand Down Expand Up @@ -192,6 +212,29 @@ func (l *Loader) getConfigSearchPaths() []string {
return searchPaths
}

func (l *Loader) getGlobalConfigSearchPaths() []string {
searchPaths := []string{}

// Find in XDG_CONFIG_HOME/golangci or $HOME/.config/golangci for global config
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome == "" {
l.log.Warnf("XDG_CONFIG_HOME is not set")
} else {
searchPaths = append(searchPaths, filepath.Join(xdgConfigHome, "golangci"))
}

// Find the user's home directory
if home, err := homedir.Dir(); err != nil {
l.log.Warnf("Can't get user's home directory: %v", err)
} else {
homeGlobalConfigPath := filepath.Join(home, ".config", "golangci")
if !slices.Contains(searchPaths, homeGlobalConfigPath) {
searchPaths = append(searchPaths, filepath.Join(home, ".config", "golangci"))
}
}

return searchPaths
}

func (l *Loader) parseConfig() error {
if err := l.viper.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
Expand Down