-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
56 lines (51 loc) · 1.11 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
47
48
49
50
51
52
53
54
55
56
package utils
import (
"os"
"path"
"os/user"
"path/filepath"
)
// Abs returns an absolute path
//
// It expands ~ to $HOME
// If path is already absolute, it cleans up (shortest path).
// If the path is relative, it adds current directory to create it as absolute path
func Abs(name string) (string, error) {
// Check in case of paths like "/something/~/something/"
if len(name) >2 && name[:2] == "~/" {
usr, err := user.Current()
if err != nil {
return "", err
}
dir := usr.HomeDir
name = filepath.Join(dir, name[2:])
}
if path.IsAbs(name) {
return path.Clean(name), nil
}
wd, err := os.Getwd()
return path.Clean(path.Join(wd, name)), err
}
func Touch(file string) error {
if fd, err := os.Create(file); err == nil {
return fd.Close()
} else {
return err
}
}
func InStringList(element string, elements ...string) string {
for _, value := range elements {
if element == value {
return value
}
}
return ""
}
func ArrayStringDelete(a []string, element string) []string {
for index, value := range a {
if value == element {
return append(a[:index], a[index+1:]...)
}
}
return a
}