/
utils.go
46 lines (42 loc) · 1.05 KB
/
utils.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
46
package nix
import (
"github.com/nlewo/nix2container/types"
"path"
"path/filepath"
"regexp"
"strings"
)
func removeNixCaseHackSuffix(filepath string) string {
caseHackSuffix := "~nix~case~hack~"
parts := strings.Split(filepath, "/")
cleaned := make([]string, len(parts))
for i, part := range parts {
idx := strings.Index(part, caseHackSuffix)
if idx != -1 {
cleaned[i] = part[0:idx]
} else {
cleaned[i] = part
}
}
var prefix string
if strings.HasPrefix(filepath, "/") {
prefix = "/"
}
return prefix + path.Join(cleaned...)
}
func splitPath(path string) []string {
cleaned := filepath.Clean(path)
parts := strings.Split(cleaned, "/")
if len(parts) == 2 && parts[0] == "" && parts[1] == "" {
return []string{""}
}
return parts
}
func filePathToTarPath(filepath string, options *types.PathOptions) string {
tarPath := filepath
if options != nil && options.Rewrite.Regex != "" {
re := regexp.MustCompile(options.Rewrite.Regex)
tarPath = string(re.ReplaceAll([]byte(filepath), []byte(options.Rewrite.Repl)))
}
return tarPath
}