mirror of
https://github.com/89luca89/distrobox.git
synced 2026-11-02 09:48:50 -06:00
refactor(containermanager): use slices.Contains for PATH segment checks
The (:|^)…(:|$) regexes only tested whether a path was an exact colon-delimited segment — what slices.Contains over strings.Split does directly, without recompiling a regex on every loop iteration. Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
This commit is contained in:
1 file changed
+5
-6
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -192,10 +193,10 @@ func BuildContainerPath(cleanPath bool, hostPath string, containerPath string) s
|
||||
}
|
||||
|
||||
// Add standard paths not in host PATH
|
||||
hostSegments := strings.Split(hostPath, ":")
|
||||
var additionalPaths []string
|
||||
for _, sp := range standardPaths {
|
||||
pattern := regexp.MustCompile(`(:|^)` + regexp.QuoteMeta(sp) + `(:|$)`)
|
||||
if !pattern.MatchString(hostPath) {
|
||||
if !slices.Contains(hostSegments, sp) {
|
||||
additionalPaths = append(additionalPaths, sp)
|
||||
}
|
||||
}
|
||||
@@ -231,8 +232,7 @@ func reorderFHSPath(path string) string {
|
||||
// If /usr/bin or /usr/sbin were absent, their local counterparts were
|
||||
// skipped above; re-add any that went missing (prepended, like the shell).
|
||||
for _, lp := range []string{"/usr/local/bin", "/usr/local/sbin"} {
|
||||
pattern := regexp.MustCompile(`(:|^)` + regexp.QuoteMeta(lp) + `(:|$)`)
|
||||
if !pattern.MatchString(result) {
|
||||
if !slices.Contains(strings.Split(result, ":"), lp) {
|
||||
result = lp + ":" + result
|
||||
}
|
||||
}
|
||||
@@ -244,10 +244,9 @@ func BuildXDGPaths(envVar string, standardPaths []string) string {
|
||||
containerPaths := os.Getenv(envVar)
|
||||
|
||||
for _, sp := range standardPaths {
|
||||
pattern := regexp.MustCompile(`(:|^)` + regexp.QuoteMeta(sp) + `(:|$)`)
|
||||
if containerPaths == "" {
|
||||
containerPaths = sp
|
||||
} else if !pattern.MatchString(containerPaths) {
|
||||
} else if !slices.Contains(strings.Split(containerPaths, ":"), sp) {
|
||||
containerPaths = containerPaths + ":" + sp
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user