-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_gormexecute.go
56 lines (45 loc) · 1.33 KB
/
func_gormexecute.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
package generator_handler
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/skyzhouzj/xhframwork/configs"
"github.com/skyzhouzj/xhframwork/internal/pkg/core"
)
type gormExecuteRequest struct {
Tables string `form:"tables"`
}
func (h *handler) GormExecute() core.HandlerFunc {
dir, _ := os.Getwd()
projectPath := strings.Replace(dir, "\\", "/", -1)
gormgenSh := projectPath + "/scripts/gormgen.sh"
gormgenBat := projectPath + "/scripts/gormgen.bat"
return func(c core.Context) {
req := new(gormExecuteRequest)
if err := c.ShouldBindPostForm(req); err != nil {
c.Payload("参数传递有误")
return
}
mysqlConf := configs.Get().MySQL.Read
shellPath := fmt.Sprintf("%s %s %s %s %s %s", gormgenSh, mysqlConf.Addr, mysqlConf.User, mysqlConf.Pass, mysqlConf.Name, req.Tables)
batPath := fmt.Sprintf("%s %s %s %s %s %s", gormgenBat, mysqlConf.Addr, mysqlConf.User, mysqlConf.Pass, mysqlConf.Name, req.Tables)
command := new(exec.Cmd)
if runtime.GOOS == "windows" {
command = exec.Command("cmd", "/C", batPath)
} else {
// runtime.GOOS = linux or darwin
command = exec.Command("/bin/bash", "-c", shellPath)
}
var stderr bytes.Buffer
command.Stderr = &stderr
output, err := command.Output()
if err != nil {
c.Payload(stderr.String())
return
}
c.Payload(string(output))
}
}