Repository navigation
Expand file tree
/
Copy pathtemplate_path.go
More file actions
68 lines (57 loc) · 1.62 KB
/
Copy pathtemplate_path.go
File metadata and controls
68 lines (57 loc) · 1.62 KB
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
61
62
63
64
65
66
67
68
package main
import (
"runtime"
"strings"
"github.com/powerman/winfilepath"
)
// parseTemplatePaths parses colon-separated template paths with OS-specific logic.
// On POSIX colons are always separators (so files with colon in name are not supported).
// On Windows colons in drive letters are not separators.
//
// If only one path is given, the dst is empty.
// If two paths are given, the first is src and the second is dst.
// If none or more than two paths are given, both src and dst are empty (this means error).
func parseTemplatePaths(srcdst string) (src, dst string) {
var paths []string
if runtime.GOOS == "windows" {
paths = splitWindowsPaths(srcdst)
} else {
paths = strings.Split(srcdst, ":")
}
switch len(paths) {
case 1:
return paths[0], ""
case 2: //nolint:mnd // Clearer than a named constant.
return paths[0], paths[1]
}
return "", ""
}
// splitWindowsPaths splits a PATH-like colon-separated string into its components,
// taking into account that Windows drive letters contain colons.
//
// Device names (like `\\.\NUL`) are not supported yet.
func splitWindowsPaths(s string) []string {
if s == "" {
return []string{}
}
var paths []string
start := 0
for i := range len(s) {
if s[i] != ':' {
continue
}
// Check if this colon is part of a Windows volume name.
currentSegment := s[start : i+1]
volumeName := winfilepath.VolumeName(currentSegment)
if volumeName != "" && volumeName == currentSegment {
continue
}
// Otherwise this is a path separator.
paths = append(paths, s[start:i])
start = i + 1
}
if start <= len(s) {
paths = append(paths, s[start:])
}
return paths
}