1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
package xdg
import (
"errors"
"os"
"path/filepath"
)
// ErrNotFound indicates that a file cannot be found in any directory.
var ErrNotFound = errors.New("not found")
func alternate(str, alt string) string {
if str == "" {
return alt
}
return str
}
func altUserProfile(path string, suffix ...string) string {
if path != "" {
return path
}
home := os.Getenv("USERPROFILE")
if home != "" {
return filepath.Join(append([]string{home}, suffix...)...)
}
return ""
}
func altHome(path string, suffix ...string) string {
if path != "" {
return path
}
home := os.Getenv("HOME")
if home != "" {
return filepath.Join(append([]string{home}, suffix...)...)
}
return ""
}
func findFile(paths []string, rel []string) (string, error) {
for _, dir := range paths {
if !filepath.IsAbs(dir) {
// XDG Base Directory Specification supports only Absolute paths
continue
}
fpath := filepath.Join(append([]string{dir}, rel...)...)
if isFile(fpath) {
return fpath, nil
}
}
return "", ErrNotFound
}
func isFile(p string) bool {
stat, err := os.Stat(p)
return err == nil && !stat.IsDir()
}
|