-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.go
More file actions
44 lines (38 loc) · 837 Bytes
/
execute.go
File metadata and controls
44 lines (38 loc) · 837 Bytes
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
package main
import (
"bytes"
"errors"
"fmt"
"os/exec"
"path"
)
// execute go run goshell.go, if failed, deprecated these codes
func execute() {
flush()
err := tryExecute()
if err != nil {
undo()
} else {
save()
}
}
// try goimports to remove unused imports and add requried imports
// try go run goshell.go, if fail, return err
func tryExecute() error {
// run goimports in advance
goimports := exec.Command("goimports", "-w", path.Join(codeDir, codeFile))
goimports.Run()
// run go file
cmd := exec.Command("go", "run", path.Join(codeDir, codeFile))
var outBuffer bytes.Buffer
var errBuffer bytes.Buffer
cmd.Stdout = &outBuffer
cmd.Stderr = &errBuffer
err := cmd.Run()
fmt.Print(outBuffer.String())
fmt.Print(errBuffer.String())
if errBuffer.Len() != 0 {
err = errors.New("run err")
}
return err
}