-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.go
83 lines (73 loc) · 1.67 KB
/
common.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package sellsword
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/fatih/color"
"os"
"os/user"
"path"
"path/filepath"
"strings"
)
var Logger *log.Logger
var Version = "0.0.3"
func GetTermPrinter(colorName color.Attribute) func(...interface{}) string {
newColor := color.New(colorName)
newColor.EnableColor()
return newColor.SprintFunc()
}
func GetTermPrinterF(colorName color.Attribute) func(string, ...interface{}) string {
newColor := color.New(colorName)
newColor.EnableColor()
return newColor.SprintfFunc()
}
func resolveSymlink(symlink string) (string, error) {
fi, err := os.Lstat(symlink)
if err != nil {
Logger.Debugf("Path %s does not exist\n", symlink)
return "", err
} else {
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
if realPath, err := os.Readlink(symlink); err == nil {
return realPath, nil
} else {
return "", err
}
} else {
return "", errors.New(fmt.Sprintf("Path %s exists but is not a symlink\n", symlink))
}
}
}
// Why the fuck isn't this in the golang stdlib?
func expandPath(pathName string) (string, error) {
if string(pathName[0]) == "~" {
relative := strings.Split(pathName, "~")[1]
usr, _ := user.Current()
return path.Join(usr.HomeDir, relative), nil
} else {
return filepath.Abs(pathName)
}
}
func contains(l []string, s string) bool {
for _, str := range l {
if s == str {
return true
}
}
return false
}
func appendIfMissing(slice []string, s string) []string {
if !contains(slice, s) {
return append(slice, s)
} else {
return slice
}
}
func arrayToEmptyMap(a []string) map[string]string {
m := make(map[string]string)
for i := range a {
m[a[i]] = ""
}
return m
}