transformPath transforms a path string according to the chosen TransformAlgo. Each path segment is transformed separately, to preserve path separators. If baseOnly is true, only the base will be transformed (useful for renaming while walking a dir tree recursively.) for example, "some/nested/path" -
(s string, t transform, baseOnly bool)
| 83 | // for example, "some/nested/path" -> "some/nested/CONVERTEDPATH" |
| 84 | // otherwise, the entire is path is transformed. |
| 85 | func transformPath(s string, t transform, baseOnly bool) (string, error) { |
| 86 | if s == "" || s == "/" || s == "\\" || s == "." { |
| 87 | return "", nil |
| 88 | } |
| 89 | |
| 90 | if baseOnly { |
| 91 | transformedBase, err := transformPathSegment(path.Base(s), t) |
| 92 | if err := validateSegment(transformedBase); err != nil { |
| 93 | return "", err |
| 94 | } |
| 95 | return path.Join(path.Dir(s), transformedBase), err |
| 96 | } |
| 97 | |
| 98 | segments := strings.Split(s, "/") |
| 99 | transformedSegments := make([]string, len(segments)) |
| 100 | for _, seg := range segments { |
| 101 | convSeg, err := transformPathSegment(seg, t) |
| 102 | if err != nil { |
| 103 | return "", err |
| 104 | } |
| 105 | if err := validateSegment(convSeg); err != nil { |
| 106 | return "", err |
| 107 | } |
| 108 | transformedSegments = append(transformedSegments, convSeg) |
| 109 | } |
| 110 | return path.Join(transformedSegments...), nil |
| 111 | } |
| 112 | |
| 113 | // transform all but the last path segment |
| 114 | func transformDir(s string, t transform) (string, error) { |
no test coverage detected
searching dependent graphs…