JoinPaths it'll filter the paths that are already in the runtimePaths, and append them to the end of the finalPaths.
(envkey string, paths ...string)
| 9 | // JoinPaths it'll filter the paths that are already in the runtimePaths, |
| 10 | // and append them to the end of the finalPaths. |
| 11 | func JoinPaths(envkey string, paths ...string) string { |
| 12 | if strings.TrimSpace(envkey) == "" { |
| 13 | panic("envkey is empty when JoinPaths.") |
| 14 | } |
| 15 | separator := string(os.PathListSeparator) |
| 16 | |
| 17 | var finalPaths []string |
| 18 | var currentPaths []string |
| 19 | |
| 20 | path := os.Getenv(envkey) |
| 21 | if path != "" { |
| 22 | currentPaths = strings.Split(path, separator) |
| 23 | } |
| 24 | |
| 25 | // Filter paths that are not in runtimePaths. |
| 26 | for _, path := range paths { |
| 27 | if !slices.Contains(currentPaths, path) { |
| 28 | finalPaths = append(finalPaths, path) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Merge runtimePaths to the end of finalPaths. |
| 33 | if len(currentPaths) > 0 { |
| 34 | finalPaths = append(finalPaths, currentPaths...) |
| 35 | } |
| 36 | return strings.Join(finalPaths, separator) |
| 37 | } |
| 38 | |
| 39 | // JoinSpace Joins the paths with space. |
| 40 | func JoinSpace(paths ...string) string { |
no outgoing calls
no test coverage detected