-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.go
55 lines (48 loc) · 1.17 KB
/
str.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
47
48
49
50
51
52
53
54
55
package str
import (
"fmt"
"path/filepath"
"sort"
)
// StringList flattens its arguments into a single []string.
// Each argument in args must have type string or []string.
func StringList(args ...any) []string {
x := []string{}
for _, arg := range args {
switch arg := arg.(type) {
case []string:
x = append(x, arg...)
case string:
x = append(x, arg)
default:
panic("stringList: invalid argument of type " + fmt.Sprintf("%T", arg))
}
}
return x
}
func RemoveDuplicatesAndSort(elements *[]string) {
seen := make(map[string]struct{})
result := []string{}
for v := range *elements {
if _, ok := seen[(*elements)[v]]; !ok {
seen[(*elements)[v]] = struct{}{}
result = append(result, (*elements)[v])
}
}
sort.Strings(result)
// Update the original slice
*elements = result
}
func ConvertToRelativePaths(elements *[]string, basepath string) error {
for i, element := range *elements {
if !filepath.IsAbs(element) {
continue // Skip relative paths
}
relpath, err := filepath.Rel(basepath, element)
if err != nil {
return fmt.Errorf("failed to convert %s to a relative path: %w", element, err)
}
(*elements)[i] = relpath
}
return nil
}