-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprocess.go
82 lines (64 loc) · 1.62 KB
/
process.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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
type attr struct {
Scope string
Description string
}
type snippetItem struct {
Scope string `json:"scope"`
Description string `json:"description"`
Body []string `json:"body"`
Prefix string `json:"prefix"`
}
func jsonMarshal(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
err := encoder.Encode(t)
return buffer.Bytes(), err
}
// ParseSnpFiles read current work directory's .snp file and parse it to code snippet
func ParseSnpFiles() []byte {
cwd, _ := os.Getwd()
snippet := make(map[string]snippetItem)
err := filepath.Walk(cwd, func(fp string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(fp, ".snp") {
attribute := attr{}
rawContent, readFileErr := ioutil.ReadFile(fp)
if readFileErr != nil {
log.Fatal(err)
}
filename := filepath.Base(fp)
prefix := filename[0 : len(filename)-4]
content, parseFmErr := Unmarshal(rawContent, &attribute)
if parseFmErr != nil {
log.Fatal(parseFmErr)
}
stringContent := strings.TrimRight(strings.TrimLeft(string(content), "\n"), "\n")
body := strings.Split(stringContent, "\n")
item := snippetItem{attribute.Scope, attribute.Description, body, prefix}
snippet[prefix] = item
}
return nil
})
if err != nil {
log.Fatal(err)
}
json, jsonEncodingErr := jsonMarshal(snippet)
if jsonEncodingErr != nil {
log.Fatal(jsonEncodingErr)
}
return json
}