Skip to content

Commit d372b4e

Browse files
committed
Format generated Go source code in-process
This commit: - removes dependency on gofmt tool - cleans up package imports - re-generates binapi for VPP 20.01 Change-Id: Ie4347720f92a87eb278be66c9f9ed9719c7bbbc3 Signed-off-by: Ondrej Fabry <[email protected]>
1 parent 2c419b1 commit d372b4e

File tree

14 files changed

+71
-58
lines changed

14 files changed

+71
-58
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ This file lists changes for the GoVPP releases.
1111
-
1212
-->
1313

14+
## 0.3.4
15+
> _17 April 2020_
16+
17+
### Features
18+
- Format generated Go source code in-process
19+
1420
## 0.3.3
1521
> _9 April 2020_
1622

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ gen-binapi-docker: install-generator ## Generate binapi code (using Docker)
9797
@echo "# generating binapi in docker image ${VPP_IMG}"
9898
$(eval cmds := $(shell go generate -n $(BINAPI_DIR) 2>&1 | tr "\n" ";"))
9999
docker run -t --rm \
100-
-v "$(shell which gofmt):/usr/local/bin/gofmt:ro" \
101100
-v "$(shell which binapi-generator):/usr/local/bin/binapi-generator:ro" \
102101
-v "$(shell pwd):/govpp" -w /govpp \
103102
-u "$(shell id -u):$(shell id -g)" \

cmd/binapi-generator/generate.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const (
3636
inputFileExt = ".api.json" // file extension of the VPP API files
3737
outputFileExt = ".ba.go" // file extension of the Go generated files
3838

39-
govppApiImportPath = "git.fd.io/govpp.git/api" // import path of the govpp API package
40-
4139
constModuleName = "ModuleName" // module name constant
4240
constAPIVersion = "APIVersion" // API version constant
4341
constVersionCrc = "VersionCrc" // version CRC constant
@@ -244,11 +242,12 @@ func generateHeader(ctx *context, w io.Writer) {
244242

245243
func generateImports(ctx *context, w io.Writer) {
246244
fmt.Fprintln(w, "import (")
247-
fmt.Fprintf(w, "\tapi \"%s\"\n", govppApiImportPath)
248-
fmt.Fprintf(w, "\tbytes \"%s\"\n", "bytes")
249-
fmt.Fprintf(w, "\tcontext \"%s\"\n", "context")
250-
fmt.Fprintf(w, "\tio \"%s\"\n", "io")
251-
fmt.Fprintf(w, "\tstrconv \"%s\"\n", "strconv")
245+
fmt.Fprintln(w, ` "bytes"`)
246+
fmt.Fprintln(w, ` "context"`)
247+
fmt.Fprintln(w, ` "io"`)
248+
fmt.Fprintln(w, ` "strconv"`)
249+
fmt.Fprintln(w)
250+
fmt.Fprintf(w, "\tapi \"%s\"\n", "git.fd.io/govpp.git/api")
252251
fmt.Fprintf(w, "\tstruc \"%s\"\n", "github.com/lunixbochs/struc")
253252
if len(ctx.packageData.Imports) > 0 {
254253
fmt.Fprintln(w)

cmd/binapi-generator/main.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
"encoding/json"
2020
"flag"
2121
"fmt"
22+
"go/format"
2223
"io/ioutil"
2324
"os"
24-
"os/exec"
2525
"path/filepath"
2626
"strings"
2727

@@ -204,10 +204,15 @@ func generateFromFile(inputFile, outputDir string, typesPkgs []*context) error {
204204
}
205205
}
206206

207-
// generate Go package code
207+
// generate Go package
208208
var buf bytes.Buffer
209209
if err := generatePackage(ctx, &buf); err != nil {
210-
return fmt.Errorf("generating code for package %s failed: %v", ctx.packageName, err)
210+
return fmt.Errorf("generating Go package for %s failed: %v", ctx.packageName, err)
211+
}
212+
// format generated source code
213+
gosrc, err := format.Source(buf.Bytes())
214+
if err != nil {
215+
return fmt.Errorf("formatting source code for package %s failed: %v", ctx.packageName, err)
211216
}
212217

213218
// create output directory
@@ -216,16 +221,10 @@ func generateFromFile(inputFile, outputDir string, typesPkgs []*context) error {
216221
return fmt.Errorf("creating output dir %s failed: %v", packageDir, err)
217222
}
218223
// write generated code to output file
219-
if err := ioutil.WriteFile(ctx.outputFile, buf.Bytes(), 0666); err != nil {
224+
if err := ioutil.WriteFile(ctx.outputFile, gosrc, 0666); err != nil {
220225
return fmt.Errorf("writing to output file %s failed: %v", ctx.outputFile, err)
221226
}
222227

223-
// go format the output file (fail probably means the output is not compilable)
224-
cmd := exec.Command("gofmt", "-w", ctx.outputFile)
225-
if output, err := cmd.CombinedOutput(); err != nil {
226-
return fmt.Errorf("gofmt failed: %v\n%s", err, string(output))
227-
}
228-
229228
return nil
230229
}
231230

examples/binapi/af_packet/af_packet.ba.go

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

examples/binapi/ethernet_types/ethernet_types.ba.go

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

examples/binapi/interface_types/interface_types.ba.go

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

examples/binapi/interfaces/interfaces.ba.go

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

examples/binapi/ip/ip.ba.go

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

examples/binapi/ip_types/ip_types.ba.go

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

0 commit comments

Comments
 (0)