JoinPathLists joins and cleans PATH-style strings of [os.ListSeparator] delimited paths. To clean a path list, it splits it and does the following for each element: 1. Applies [filepath.Clean]. 2. Removes the path if it's relative (must begin with '/' and not be '.'). 3. Removes the path if it's a
(pathLists ...string)
| 14 | // 2. Removes the path if it's relative (must begin with '/' and not be '.'). |
| 15 | // 3. Removes the path if it's a duplicate. |
| 16 | func JoinPathLists(pathLists ...string) string { |
| 17 | if len(pathLists) == 0 { |
| 18 | return "" |
| 19 | } |
| 20 | |
| 21 | seen := make(map[string]bool) |
| 22 | var cleaned []string |
| 23 | for _, path := range pathLists { |
| 24 | for _, path := range filepath.SplitList(path) { |
| 25 | path = filepath.Clean(path) |
| 26 | if path == "." || path[0] != '/' { |
| 27 | // Remove empty paths and don't allow relative |
| 28 | // paths for security reasons. |
| 29 | continue |
| 30 | } |
| 31 | if !seen[path] { |
| 32 | cleaned = append(cleaned, path) |
| 33 | } |
| 34 | seen[path] = true |
| 35 | } |
| 36 | } |
| 37 | return strings.Join(cleaned, string(filepath.ListSeparator)) |
| 38 | } |
| 39 | |
| 40 | func RemoveFromPath(path, pathToRemove string) string { |
| 41 | paths := filepath.SplitList(path) |
no outgoing calls