This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
273 lines (251 loc) · 5.36 KB
/
commands.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package pica
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"bytes"
"path/filepath"
"text/template"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"github.com/howeyc/fsnotify"
"github.com/jerloo/funny"
_ "github.com/jerloo/pica/statik"
"github.com/rakyll/statik/fs"
"github.com/shurcooL/github_flavored_markdown"
"gopkg.in/AlecAivazis/survey.v1"
)
type InitInfo struct {
Name string
Description string
Author string
Version string
BaseUrl string
}
func Init(filename, templateName string) error {
if _, err := os.Stat(filename); err == nil {
override := false
var q = &survey.Confirm{
Message: "Api file already exists. Override ?",
}
survey.AskOne(q, &override, nil)
if !override {
return nil
}
}
data, err := ioutil.ReadFile(templateName)
if err != nil {
sfs, err := fs.New()
if err != nil {
return err
}
file, err := sfs.Open("/api_file_template.fun")
if err != nil {
return err
}
DEFAULT_API_FILE_TEMPLATE, err := ioutil.ReadAll(file)
if err != nil {
return err
}
data = []byte(DEFAULT_API_FILE_TEMPLATE)
}
info := InitInfo{}
// the questions to ask
dir, _ := os.Getwd()
l := filepath.SplitList(dir)
dir = l[len(l)-1]
var qs = []*survey.Question{
{
Name: "Name",
Prompt: &survey.Input{
Message: "What is your api file name?",
Default: filepath.Base(dir),
},
Validate: survey.Required,
},
{
Name: "Description",
Prompt: &survey.Input{
Message: "What is then description of your apis ?",
},
},
{
Name: "Author",
Prompt: &survey.Input{
Message: "Who are you ?",
},
},
{
Name: "Version",
Prompt: &survey.Input{
Message: "What version to start ?",
},
},
{
Name: "BaseUrl",
Prompt: &survey.Input{
Message: "What is the baseUrl of your apis ?",
},
},
}
err = survey.Ask(qs, &info)
if err != nil {
panic(err)
}
t := template.Must(template.New("api").Parse(string(data)))
buffer := new(bytes.Buffer)
err = t.Execute(buffer, &info)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(filename, buffer.Bytes(), os.ModePerm)
if err != nil {
return err
}
return nil
}
func Format(filename string, save, print bool) (string, error) {
fw := strings.Builder{}
output := func(text string) {
if print {
fmt.Printf("%s", text)
}
if save {
fw.WriteString(text)
}
}
buffer, err := ioutil.ReadFile(filename)
if err != nil {
return "", fmt.Errorf("parse error %v", err.Error())
}
parser := funny.NewParser(buffer, filename)
parser.Consume("")
flag := 0
for {
item := parser.ReadStatement()
if item == nil {
break
}
switch item.(type) {
case *funny.NewLine:
flag += 1
if flag < 1 {
continue
}
break
}
output(fmt.Sprintf("%s", item.String()))
}
if save {
ioutil.WriteFile(filename, []byte(fw.String()), os.ModePerm)
}
return fw.String(), nil
}
func Serve(filename string, port int) error {
if !strings.HasSuffix(filename, ".md") {
return fmt.Errorf("unknow type of doc files support [md]")
}
input, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
output := github_flavored_markdown.Markdown(input)
r := gin.Default()
template := BuildHTML(input)
r.GET("/", func(c *gin.Context) {
rs := strings.Replace(template, "[body]", string(output), -1)
c.Data(200, "text/html", []byte(rs))
})
srv := &http.Server{
Addr: ":9090",
Handler: r,
}
run := func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}
go run()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
done := make(chan bool)
// Process events
go func() {
for {
select {
case <-watcher.Event:
fmt.Println("update")
input, err = ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
output = github_flavored_markdown.Markdown(input)
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
err = watcher.Watch(filename)
if err != nil {
log.Fatal(err)
}
// Hang so program doesn't exit
<-done
/* ... do stuff ... */
watcher.Close()
return nil
}
func GenDocument(apiRunner *APIRunner, output string) error {
if !strings.HasSuffix(output, ".md") && !strings.HasSuffix(output, ".html") {
return errors.New("only .md and .html supported")
}
data, err := ioutil.ReadFile("")
if err != nil {
data = []byte(DEFAULT_DOC_TEMPLATE)
}
generator := NewMarkdownDocGenerator(apiRunner, string(data), output)
results, err := generator.Get()
if err != nil {
return err
}
fmt.Printf("%s\n", results)
// if _, err := os.Stat(p.Output); err == nil {
if strings.HasSuffix(output, ".html") {
results = []byte(BuildHTML(results))
}
err = ioutil.WriteFile(output, results, os.ModePerm)
if err != nil {
return err
}
// }
// return errors.New("file already exists")
return nil
}
func VersionCommit(commitFile, commitMsg string) {
ctrl := NewApiVersionController(commitFile)
hash, err := ctrl.Commit(commitMsg)
if err != nil {
panic(err)
}
fmt.Println(hash)
}
func VersionLog(filename string) {
ctrl := NewApiVersionController(filename)
commits, err := ctrl.GetCommits()
if err != nil {
panic(err)
}
for index, item := range commits {
color.Green(item.String())
if index != len(commits)-1 {
fmt.Println("==========================================================")
}
}
}