-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathformat.go
42 lines (36 loc) · 880 Bytes
/
format.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
package main
import (
"github.com/kpango/glg"
"golang.org/x/tools/imports"
"mvdan.cc/gofumpt/format"
)
// FormatGo takes an output GO string and formats it using gofumpt and goimports returning the formatted string.
func FormatGo(s string, ctx *Context) string {
var err error
sBytes := []byte(s)
sBytes, err = format.Source(sBytes, format.Options{
ModulePath: ctx.preset.PackagePath,
ExtraRules: true,
})
if err != nil {
if ctx.flags.Verbose {
glg.Debugf(string(s))
}
glg.Fatalf("Unable to format go code: %v", err)
}
sBytes, err = imports.Process("", sBytes, &imports.Options{
Fragment: false,
AllErrors: true,
Comments: true,
TabIndent: true,
TabWidth: 8,
FormatOnly: false,
})
if err != nil {
if ctx.flags.Verbose {
glg.Debugf(string(s))
}
glg.Fatalf("Unable to goimports code: %v", err)
}
return string(sBytes)
}