88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
// Package tools contains other helper functions too small to justify their own package
|
|
// NOTE: Subject to change, do not rely on this package from outside git-lfs source
|
|
package tools
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// FileOrDirExists determines if a file/dir exists, returns IsDir() results too.
|
|
func FileOrDirExists(path string) (exists bool, isDir bool) {
|
|
fi, err := os.Stat(path)
|
|
if err != nil {
|
|
return false, false
|
|
} else {
|
|
return true, fi.IsDir()
|
|
}
|
|
}
|
|
|
|
// FileExists determines if a file (NOT dir) exists.
|
|
func FileExists(path string) bool {
|
|
ret, isDir := FileOrDirExists(path)
|
|
return ret && !isDir
|
|
}
|
|
|
|
// DirExists determines if a dir (NOT file) exists.
|
|
func DirExists(path string) bool {
|
|
ret, isDir := FileOrDirExists(path)
|
|
return ret && isDir
|
|
}
|
|
|
|
// FileExistsOfSize determines if a file exists and is of a specific size.
|
|
func FileExistsOfSize(path string, sz int64) bool {
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return !fi.IsDir() && fi.Size() == sz
|
|
}
|
|
|
|
// ResolveSymlinks ensures that if the path supplied is a symlink, it is
|
|
// resolved to the actual concrete path
|
|
func ResolveSymlinks(path string) string {
|
|
if len(path) == 0 {
|
|
return path
|
|
}
|
|
|
|
if resolved, err := filepath.EvalSymlinks(path); err == nil {
|
|
return resolved
|
|
}
|
|
return path
|
|
}
|
|
|
|
// CleanPaths splits the given `paths` argument by the delimiter argument, and
|
|
// then "cleans" that path according to the filepath.Clean function (see
|
|
// https://golang.org/pkg/file/filepath#Clean).
|
|
func CleanPaths(paths, delim string) (cleaned []string) {
|
|
// If paths is an empty string, splitting it will yield [""], which will
|
|
// become the filepath ".". To avoid this, bail out if trimmed paths
|
|
// argument is empty.
|
|
if paths = strings.TrimSpace(paths); len(paths) == 0 {
|
|
return
|
|
}
|
|
|
|
for _, part := range strings.Split(paths, delim) {
|
|
part = strings.TrimSpace(part)
|
|
|
|
cleaned = append(cleaned, filepath.Clean(part))
|
|
}
|
|
|
|
return cleaned
|
|
}
|
|
|
|
// CleanPathsDefault cleans the paths contained in the given `paths` argument
|
|
// delimited by the `delim`, argument. If an empty set is returned from that
|
|
// split, then the fallback argument is returned instead.
|
|
func CleanPathsDefault(paths, delim string, fallback []string) []string {
|
|
cleaned := CleanPaths(paths, delim)
|
|
if len(cleaned) == 0 {
|
|
return fallback
|
|
}
|
|
|
|
return cleaned
|
|
}
|