1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-05 00:59:19 +02:00

Switch to github.com/adrg/xdg

This commit is contained in:
Ching Pei Yang
2023-08-17 13:52:55 +02:00
committed by Stefan Haller
parent 4302018437
commit 2118ecdf8e
30 changed files with 1515 additions and 363 deletions

View File

@ -0,0 +1,78 @@
package pathutil
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// Unique eliminates the duplicate paths from the provided slice and returns
// the result. The items in the output slice are in the order in which they
// occur in the input slice. If a `home` location is provided, the paths are
// expanded using the `ExpandHome` function.
func Unique(paths []string, home string) []string {
var (
uniq []string
registry = map[string]struct{}{}
)
for _, p := range paths {
p = ExpandHome(p, home)
if p != "" && filepath.IsAbs(p) {
if _, ok := registry[p]; ok {
continue
}
registry[p] = struct{}{}
uniq = append(uniq, p)
}
}
return uniq
}
// Create returns a suitable location relative to which the file with the
// specified `name` can be written. The first path from the provided `paths`
// slice which is successfully created (or already exists) is used as a base
// path for the file. The `name` parameter should contain the name of the file
// which is going to be written in the location returned by this function, but
// it can also contain a set of parent directories, which will be created
// relative to the selected parent path.
func Create(name string, paths []string) (string, error) {
var searchedPaths []string
for _, p := range paths {
p = filepath.Join(p, name)
dir := filepath.Dir(p)
if Exists(dir) {
return p, nil
}
if err := os.MkdirAll(dir, os.ModeDir|0700); err == nil {
return p, nil
}
searchedPaths = append(searchedPaths, dir)
}
return "", fmt.Errorf("could not create any of the following paths: %s",
strings.Join(searchedPaths, ", "))
}
// Search searches for the file with the specified `name` in the provided
// slice of `paths`. The `name` parameter must contain the name of the file,
// but it can also contain a set of parent directories.
func Search(name string, paths []string) (string, error) {
var searchedPaths []string
for _, p := range paths {
p = filepath.Join(p, name)
if Exists(p) {
return p, nil
}
searchedPaths = append(searchedPaths, filepath.Dir(p))
}
return "", fmt.Errorf("could not locate `%s` in any of the following paths: %s",
filepath.Base(name), strings.Join(searchedPaths, ", "))
}

View File

@ -0,0 +1,29 @@
package pathutil
import (
"os"
"path/filepath"
"strings"
)
// Exists returns true if the specified path exists.
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// ExpandHome substitutes `~` and `$home` at the start of the specified
// `path` using the provided `home` location.
func ExpandHome(path, home string) string {
if path == "" || home == "" {
return path
}
if path[0] == '~' {
return filepath.Join(home, path[1:])
}
if strings.HasPrefix(path, "$home") {
return filepath.Join(home, path[5:])
}
return path
}

View File

@ -0,0 +1,32 @@
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || nacl || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd js,wasm nacl linux netbsd openbsd solaris
package pathutil
import (
"os"
"path/filepath"
"strings"
)
// Exists returns true if the specified path exists.
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// ExpandHome substitutes `~` and `$HOME` at the start of the specified
// `path` using the provided `home` location.
func ExpandHome(path, home string) string {
if path == "" || home == "" {
return path
}
if path[0] == '~' {
return filepath.Join(home, path[1:])
}
if strings.HasPrefix(path, "$HOME") {
return filepath.Join(home, path[5:])
}
return path
}

View File

@ -0,0 +1,64 @@
package pathutil
import (
"os"
"path/filepath"
"strings"
"golang.org/x/sys/windows"
)
// Exists returns true if the specified path exists.
func Exists(path string) bool {
fi, err := os.Lstat(path)
if fi != nil && fi.Mode()&os.ModeSymlink != 0 {
_, err = filepath.EvalSymlinks(path)
}
return err == nil || os.IsExist(err)
}
// ExpandHome substitutes `%USERPROFILE%` at the start of the specified
// `path` using the provided `home` location.
func ExpandHome(path, home string) string {
if path == "" || home == "" {
return path
}
if strings.HasPrefix(path, `%USERPROFILE%`) {
return filepath.Join(home, path[13:])
}
return path
}
// KnownFolder returns the location of the folder with the specified ID.
// If that fails, the folder location is determined by reading the provided
// environment variables (the first non-empty read value is returned).
// If that fails as well, the first non-empty fallback is returned.
// If all of the above fails, the function returns an empty string.
func KnownFolder(id *windows.KNOWNFOLDERID, envVars []string, fallbacks []string) string {
if id != nil {
flags := []uint32{windows.KF_FLAG_DEFAULT, windows.KF_FLAG_DEFAULT_PATH}
for _, flag := range flags {
p, _ := windows.KnownFolderPath(id, flag|windows.KF_FLAG_DONT_VERIFY)
if p != "" {
return p
}
}
}
for _, envVar := range envVars {
p := os.Getenv(envVar)
if p != "" {
return p
}
}
for _, fallback := range fallbacks {
if fallback != "" {
return fallback
}
}
return ""
}