-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.go
72 lines (61 loc) · 1.51 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
package main
import (
"bytes"
"go/format"
"os"
"path/filepath"
)
func sliceContains(slice []string, entry string) bool {
for _, s := range slice {
if entry == s {
return true
}
}
return false
}
func determineLocalName(suggestedName string, currentNames []string) string {
if !sliceContains(currentNames, suggestedName) {
return suggestedName
}
f := func(prefix string) string {
p := []byte(prefix)
for i := 97; i < 97+26; i++ {
b := append([]byte{}, p...)
b = append(b, byte(i))
if !sliceContains(currentNames, string(b)) {
return string(b)
}
}
return ""
}
for sliceContains(currentNames, suggestedName) {
suggestedName = suggestedName + "a"
suggestedName = f(suggestedName)
}
return suggestedName
}
func formatBuffer(buf bytes.Buffer, filename string) []byte {
src, err := format.Source(buf.Bytes())
if err != nil {
log.Printf("Warning: internal Error: invalid go generated in file %s: %s", filename, err)
log.Printf("Warning: compile the package to analyze the error: %s", err)
return buf.Bytes()
}
return src
}
func openFile(dirname, filename string) *os.File {
_, err := os.Stat(dirname)
err = os.MkdirAll(dirname, 0744)
if err != nil && !os.IsExist(err) {
log.Fatalf("Unable to Make Directory: %s: %s", dirname, err)
}
fname := filepath.Join(dirname, filename)
file, err := os.Create(fname)
if err != nil && os.IsExist(err) {
file, err = os.Open(fname)
}
if err != nil {
log.Fatalf("Unable to open or create file %s: %s", fname, err)
}
return file
}