file_finder.go raw

   1  //go:build finder
   2  
   3  package viper
   4  
   5  import (
   6  	"fmt"
   7  
   8  	"github.com/sagikazarmark/locafero"
   9  )
  10  
  11  // Search all configPaths for any config file.
  12  // Returns the first path that exists (and is a config file).
  13  func (v *Viper) findConfigFile() (string, error) {
  14  	var names []string
  15  
  16  	if v.configType != "" {
  17  		names = locafero.NameWithOptionalExtensions(v.configName, SupportedExts...)
  18  	} else {
  19  		names = locafero.NameWithExtensions(v.configName, SupportedExts...)
  20  	}
  21  
  22  	finder := locafero.Finder{
  23  		Paths: v.configPaths,
  24  		Names: names,
  25  		Type:  locafero.FileTypeFile,
  26  	}
  27  
  28  	results, err := finder.Find(v.fs)
  29  	if err != nil {
  30  		return "", err
  31  	}
  32  
  33  	if len(results) == 0 {
  34  		return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
  35  	}
  36  
  37  	return results[0], nil
  38  }
  39