/
path_unix.go
45 lines (39 loc) · 1.14 KB
/
path_unix.go
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
//go:build !windows
package chezmoi
import (
"path/filepath"
"strings"
)
var devNullAbsPath = NewAbsPath("/dev/null")
// NewAbsPathFromExtPath returns a new AbsPath by converting extPath to use
// slashes, performing tilde expansion, and making the path absolute.
func NewAbsPathFromExtPath(extPath string, homeDirAbsPath AbsPath) (AbsPath, error) {
switch {
case extPath == "~":
return homeDirAbsPath, nil
case strings.HasPrefix(extPath, "~/"):
return homeDirAbsPath.JoinString(extPath[2:]), nil
case filepath.IsAbs(extPath):
return NewAbsPath(extPath), nil
default:
absPath, err := filepath.Abs(extPath)
if err != nil {
return EmptyAbsPath, err
}
return NewAbsPath(absPath), nil
}
}
// NormalizePath returns path normalized. On non-Windows systems, normalized
// paths are absolute paths.
func NormalizePath(path string) (AbsPath, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return EmptyAbsPath, err
}
return NewAbsPath(absPath), nil
}
// normalizeLinkname returns linkname normalized. On non-Windows systems, it
// returns linkname unchanged.
func normalizeLinkname(linkname string) string {
return linkname
}