Skip to content

Commit 0b01232

Browse files
authored
all: remove external executable dependency 'goimports' (#11)
1 parent 44ff43a commit 0b01232

File tree

22 files changed

+13691
-30
lines changed

22 files changed

+13691
-30
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ require (
66
github.com/gin-gonic/gin v1.6.2
77
github.com/google/uuid v1.1.1
88
github.com/sirupsen/logrus v1.5.0
9+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e
910
gopkg.in/yaml.v2 v2.2.8
1011
)

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
4343
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
4444
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
4545
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
46-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
4746
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4847
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
4948
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5049
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
50+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
5151
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5252
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
5353
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

src/config/config.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
package config
66

77
import (
8-
"bytes"
98
"flag"
109
"fmt"
1110
"io/ioutil"
1211
"os"
13-
"os/exec"
1412

1513
"github.com/gin-gonic/gin"
1614
"github.com/sirupsen/logrus"
@@ -59,21 +57,9 @@ Usage:
5957
if err != nil {
6058
logrus.Fatalf("fatal: fail to parse configuration file: %v", err)
6159
}
62-
commandCheck()
6360
gin.SetMode(conf.Mode)
6461

6562
logrus.SetFormatter(&logrus.TextFormatter{})
6663
logrus.SetReportCaller(false)
6764
logrus.Infof("load config file: %q", f)
6865
}
69-
70-
func commandCheck() {
71-
// check goimports
72-
cmd := exec.Command("goimports", "-h")
73-
cmd.Stderr = &bytes.Buffer{}
74-
err := cmd.Run()
75-
if err != nil && cmd.Stderr.(*bytes.Buffer).String() == "" {
76-
logrus.Fatalf("fatal: goimports commond not found: %v", err)
77-
return
78-
}
79-
}

src/route/api.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/gin-gonic/gin"
2121
"github.com/google/uuid"
2222
"golang.design/x/ssaplayground/src/config"
23+
"golang.org/x/tools/imports"
2324
)
2425

2526
// PingInput is a a reserved structure
@@ -92,20 +93,21 @@ func BuildSSA(c *gin.Context) {
9293
} else {
9394
buildFile = filepath.Join(path, "/main_test.go")
9495
}
95-
err = ioutil.WriteFile(buildFile, []byte(in.Code), os.ModePerm)
96+
97+
// 3.1 goimports
98+
importedCode, err := autoimports([]byte(in.Code))
9699
if err != nil {
97100
os.Remove(path)
98-
out.Msg = fmt.Sprintf("cannot save your code, err: \n%v", err)
99-
c.JSON(http.StatusInternalServerError, out)
101+
out.Msg = fmt.Sprintf("cannot run autoimports for your code, err: \n%v", err)
102+
c.JSON(http.StatusBadRequest, out)
100103
return
101104
}
102105

103-
// 3.1 goimports
104-
err = autoimports(buildFile)
106+
err = ioutil.WriteFile(buildFile, importedCode, os.ModePerm)
105107
if err != nil {
106108
os.Remove(path)
107-
out.Msg = fmt.Sprintf("cannot run autoimports for your code, err: \n%v", err)
108-
c.JSON(http.StatusBadRequest, out)
109+
out.Msg = fmt.Sprintf("cannot save your code, err: \n%v", err)
110+
c.JSON(http.StatusInternalServerError, out)
109111
return
110112
}
111113

@@ -189,16 +191,18 @@ func isPackageTest(code string) bool {
189191
return re.FindString(code) != ""
190192
}
191193

192-
func autoimports(outf string) error {
193-
cmd := exec.Command("goimports", "-w", outf)
194-
cmd.Stderr = &bytes.Buffer{}
195-
err := cmd.Run()
194+
func autoimports(code []byte) ([]byte, error) {
195+
out, err := imports.Process("", code, &imports.Options{
196+
Fragment: true,
197+
AllErrors: true,
198+
Comments: true,
199+
TabIndent: true,
200+
TabWidth: 8,
201+
})
196202
if err != nil {
197-
msg := cmd.Stderr.(*bytes.Buffer).String()
198-
msg = strings.ReplaceAll(msg, filepath.Dir(outf), "$GOSSAPATH")
199-
return errors.New(msg)
203+
return nil, err
200204
}
201-
return nil
205+
return out, nil
202206
}
203207

204208
func initModules(path string) error {

vendor/golang.org/x/tools/AUTHORS

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/tools/CONTRIBUTORS

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/tools/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/tools/PATENTS

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)