-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
148 lines (132 loc) · 3.1 KB
/
util.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"sort"
"strings"
)
func pathExists(path string) bool {
_, err := os.Lstat(path)
return err == nil
}
var reNonAlnum = regexp.MustCompile(`[^[:alnum:]]`)
func newTempPath(suffix, ext string) (string, error) {
suffix = reNonAlnum.ReplaceAllString(suffix, "_")
if ext != "" && ext[0] == '.' {
ext = ext[1:]
}
f, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("kc_*_%s.%s", suffix, ext))
if err != nil {
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
return f.Name(), nil
}
func write(path string, mode int, fn func(*os.File) error) error {
f, err := os.OpenFile(path, os.O_WRONLY|mode, 0666)
if err != nil {
return err
}
defer f.Close()
return fn(f)
}
func hasAnyPrefix(s string, xyz string) bool {
if len(s) == 0 {
return false
}
for _, b := range xyz {
if byte(b) == s[0] {
return true
}
}
return false
}
func pluralize(word string, n int) string {
if n == 1 {
return word
}
return word + "s"
}
func keys(m interface{}) (keys []string) {
v := reflect.ValueOf(m)
if v.Kind() != reflect.Map {
panicf("keys: input type not a map")
}
for _, k := range v.MapKeys() {
if k.Kind() != reflect.String {
panicf("keys: illegal map key: %s", k.Kind())
}
keys = append(keys, k.Interface().(string))
}
sort.Strings(keys)
return
}
func matchPattern(vals []string, pattern string) []string {
if isGlob(pattern) {
return matchGlob(vals, pattern)
}
return prefix(pattern).match(vals)
}
func matchGlob(vals []string, glob string) (ms []string) {
for _, val := range vals {
if ok, _ := filepath.Match(strings.ToLower(glob), strings.ToLower(val)); ok {
ms = append(ms, val)
}
}
return
}
// taken from filepath/match.go
func isGlob(path string) bool {
magicChars := `*?[`
if runtime.GOOS != "windows" {
magicChars = `*?[\`
}
return strings.ContainsAny(path, magicChars)
}
type prefix string
func (p prefix) match(vals []string) (res []string) {
for _, val := range vals {
if strings.HasPrefix(strings.ToLower(val), strings.ToLower(string(p))) {
res = append(res, val)
}
}
return
}
func (p prefix) matchAs(vals []string, typ string) (string, error) {
if p == "" {
return "", fmt.Errorf("unspecified %s must match one of: %s", typ, strings.Join(vals, ", "))
}
ms := p.match(vals)
switch len(ms) {
case 0:
return "", fmt.Errorf("no such %s: %s, try: %s", typ, p, strings.Join(vals, " | "))
case 1:
return ms[0], nil
default:
for i := range ms {
ms[i] = strings.Replace(ms[i], string(p), string(p)+"*", 1)
}
return "", fmt.Errorf("ambiguous %s match for %q: %s", typ, p, strings.Join(ms, ", "))
}
}
// relativeParentDir returns a relative path to the parent of dir and whether
// that path is valid.
func relativeParentDir(dir string) (string, bool) {
abs, err := filepath.Abs(dir)
if err != nil {
return "", false
}
// If the absolute path of the current directory matches that of its
// "parent", dir already is the root directory, so abort.
if abs == filepath.Join(abs, "..") {
return "", false
}
return filepath.Join(dir, ".."), true
}