vendor/github.com/spf13/viper/util.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
child 262 8d3354485fc3
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    16 	"path/filepath"
    16 	"path/filepath"
    17 	"runtime"
    17 	"runtime"
    18 	"strings"
    18 	"strings"
    19 	"unicode"
    19 	"unicode"
    20 
    20 
    21 	"github.com/spf13/afero"
       
    22 	"github.com/spf13/cast"
    21 	"github.com/spf13/cast"
    23 	jww "github.com/spf13/jwalterweatherman"
       
    24 )
    22 )
    25 
    23 
    26 // ConfigParseError denotes failing to parse configuration file.
    24 // ConfigParseError denotes failing to parse configuration file.
    27 type ConfigParseError struct {
    25 type ConfigParseError struct {
    28 	err error
    26 	err error
    86 		// update map
    84 		// update map
    87 		m[lower] = val
    85 		m[lower] = val
    88 	}
    86 	}
    89 }
    87 }
    90 
    88 
    91 func absPathify(inPath string) string {
    89 func absPathify(logger Logger, inPath string) string {
    92 	jww.INFO.Println("Trying to resolve absolute path to", inPath)
    90 	logger.Info("trying to resolve absolute path", "path", inPath)
    93 
    91 
    94 	if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
    92 	if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
    95 		inPath = userHomeDir() + inPath[5:]
    93 		inPath = userHomeDir() + inPath[5:]
    96 	}
    94 	}
    97 
    95 
    98 	if strings.HasPrefix(inPath, "$") {
    96 	inPath = os.ExpandEnv(inPath)
    99 		end := strings.Index(inPath, string(os.PathSeparator))
       
   100 
       
   101 		var value, suffix string
       
   102 		if end == -1 {
       
   103 			value = os.Getenv(inPath[1:])
       
   104 		} else {
       
   105 			value = os.Getenv(inPath[1:end])
       
   106 			suffix = inPath[end:]
       
   107 		}
       
   108 
       
   109 		inPath = value + suffix
       
   110 	}
       
   111 
    97 
   112 	if filepath.IsAbs(inPath) {
    98 	if filepath.IsAbs(inPath) {
   113 		return filepath.Clean(inPath)
    99 		return filepath.Clean(inPath)
   114 	}
   100 	}
   115 
   101 
   116 	p, err := filepath.Abs(inPath)
   102 	p, err := filepath.Abs(inPath)
   117 	if err == nil {
   103 	if err == nil {
   118 		return filepath.Clean(p)
   104 		return filepath.Clean(p)
   119 	}
   105 	}
   120 
   106 
   121 	jww.ERROR.Println("Couldn't discover absolute path")
   107 	logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error())
   122 	jww.ERROR.Println(err)
   108 
   123 	return ""
   109 	return ""
   124 }
       
   125 
       
   126 // Check if file Exists
       
   127 func exists(fs afero.Fs, path string) (bool, error) {
       
   128 	stat, err := fs.Stat(path)
       
   129 	if err == nil {
       
   130 		return !stat.IsDir(), nil
       
   131 	}
       
   132 	if os.IsNotExist(err) {
       
   133 		return false, nil
       
   134 	}
       
   135 	return false, err
       
   136 }
   110 }
   137 
   111 
   138 func stringInSlice(a string, list []string) bool {
   112 func stringInSlice(a string, list []string) bool {
   139 	for _, b := range list {
   113 	for _, b := range list {
   140 		if b == a {
   114 		if b == a {