summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md6
-rw-r--r--Makefile1
-rw-r--r--cmd/binapi-generator/generate.go13
-rw-r--r--cmd/binapi-generator/main.go19
-rw-r--r--examples/binapi/af_packet/af_packet.ba.go9
-rw-r--r--examples/binapi/ethernet_types/ethernet_types.ba.go9
-rw-r--r--examples/binapi/interface_types/interface_types.ba.go9
-rw-r--r--examples/binapi/interfaces/interfaces.ba.go9
-rw-r--r--examples/binapi/ip/ip.ba.go9
-rw-r--r--examples/binapi/ip_types/ip_types.ba.go9
-rw-r--r--examples/binapi/memclnt/memclnt.ba.go9
-rw-r--r--examples/binapi/memif/memif.ba.go9
-rw-r--r--examples/binapi/vpe/vpe.ba.go9
-rw-r--r--examples/binapi/vpe_types/vpe_types.ba.go9
14 files changed, 71 insertions, 58 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 979c304..599d549 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,12 @@ This file lists changes for the GoVPP releases.
-
-->
+## 0.3.4
+> _17 April 2020_
+
+### Features
+- Format generated Go source code in-process
+
## 0.3.3
> _9 April 2020_
diff --git a/Makefile b/Makefile
index f6e2b9c..4297746 100644
--- a/Makefile
+++ b/Makefile
@@ -97,7 +97,6 @@ gen-binapi-docker: install-generator ## Generate binapi code (using Docker)
@echo "# generating binapi in docker image ${VPP_IMG}"
$(eval cmds := $(shell go generate -n $(BINAPI_DIR) 2>&1 | tr "\n" ";"))
docker run -t --rm \
- -v "$(shell which gofmt):/usr/local/bin/gofmt:ro" \
-v "$(shell which binapi-generator):/usr/local/bin/binapi-generator:ro" \
-v "$(shell pwd):/govpp" -w /govpp \
-u "$(shell id -u):$(shell id -g)" \
diff --git a/cmd/binapi-generator/generate.go b/cmd/binapi-generator/generate.go
index a8de5d5..715836d 100644
--- a/cmd/binapi-generator/generate.go
+++ b/cmd/binapi-generator/generate.go
@@ -36,8 +36,6 @@ const (
inputFileExt = ".api.json" // file extension of the VPP API files
outputFileExt = ".ba.go" // file extension of the Go generated files
- govppApiImportPath = "git.fd.io/govpp.git/api" // import path of the govpp API package
-
constModuleName = "ModuleName" // module name constant
constAPIVersion = "APIVersion" // API version constant
constVersionCrc = "VersionCrc" // version CRC constant
@@ -244,11 +242,12 @@ func generateHeader(ctx *context, w io.Writer) {
func generateImports(ctx *context, w io.Writer) {
fmt.Fprintln(w, "import (")
- fmt.Fprintf(w, "\tapi \"%s\"\n", govppApiImportPath)
- fmt.Fprintf(w, "\tbytes \"%s\"\n", "bytes")
- fmt.Fprintf(w, "\tcontext \"%s\"\n", "context")
- fmt.Fprintf(w, "\tio \"%s\"\n", "io")
- fmt.Fprintf(w, "\tstrconv \"%s\"\n", "strconv")
+ fmt.Fprintln(w, ` "bytes"`)
+ fmt.Fprintln(w, ` "context"`)
+ fmt.Fprintln(w, ` "io"`)
+ fmt.Fprintln(w, ` "strconv"`)
+ fmt.Fprintln(w)
+ fmt.Fprintf(w, "\tapi \"%s\"\n", "git.fd.io/govpp.git/api")
fmt.Fprintf(w, "\tstruc \"%s\"\n", "github.com/lunixbochs/struc")
if len(ctx.packageData.Imports) > 0 {
fmt.Fprintln(w)
diff --git a/cmd/binapi-generator/main.go b/cmd/binapi-generator/main.go
index 8bf765b..fcd85ae 100644
--- a/cmd/binapi-generator/main.go
+++ b/cmd/binapi-generator/main.go
@@ -19,9 +19,9 @@ import (
"encoding/json"
"flag"
"fmt"
+ "go/format"
"io/ioutil"
"os"
- "os/exec"
"path/filepath"
"strings"
@@ -204,10 +204,15 @@ func generateFromFile(inputFile, outputDir string, typesPkgs []*context) error {
}
}
- // generate Go package code
+ // generate Go package
var buf bytes.Buffer
if err := generatePackage(ctx, &buf); err != nil {
- return fmt.Errorf("generating code for package %s failed: %v", ctx.packageName, err)
+ return fmt.Errorf("generating Go package for %s failed: %v", ctx.packageName, err)
+ }
+ // format generated source code
+ gosrc, err := format.Source(buf.Bytes())
+ if err != nil {
+ return fmt.Errorf("formatting source code for package %s failed: %v", ctx.packageName, err)
}
// create output directory
@@ -216,16 +221,10 @@ func generateFromFile(inputFile, outputDir string, typesPkgs []*context) error {
return fmt.Errorf("creating output dir %s failed: %v", packageDir, err)
}
// write generated code to output file
- if err := ioutil.WriteFile(ctx.outputFile, buf.Bytes(), 0666); err != nil {
+ if err := ioutil.WriteFile(ctx.outputFile, gosrc, 0666); err != nil {
return fmt.Errorf("writing to output file %s failed: %v", ctx.outputFile, err)
}
- // go format the output file (fail probably means the output is not compilable)
- cmd := exec.Command("gofmt", "-w", ctx.outputFile)
- if output, err := cmd.CombinedOutput(); err != nil {
- return fmt.Errorf("gofmt failed: %v\n%s", err, string(output))
- }
-
return nil
}
diff --git a/examples/binapi/af_packet/af_packet.ba.go b/examples/binapi/af_packet/af_packet.ba.go
index 6f50f22..9660114 100644
--- a/examples/binapi/af_packet/af_packet.ba.go
+++ b/examples/binapi/af_packet/af_packet.ba.go
@@ -13,12 +13,13 @@ It consists of:
package af_packet
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
ethernet_types "git.fd.io/govpp.git/examples/binapi/ethernet_types"
interface_types "git.fd.io/govpp.git/examples/binapi/interface_types"
diff --git a/examples/binapi/ethernet_types/ethernet_types.ba.go b/examples/binapi/ethernet_types/ethernet_types.ba.go
index c7f836b..fffa6b1 100644
--- a/examples/binapi/ethernet_types/ethernet_types.ba.go
+++ b/examples/binapi/ethernet_types/ethernet_types.ba.go
@@ -10,12 +10,13 @@ It consists of:
package ethernet_types
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
)
const (
diff --git a/examples/binapi/interface_types/interface_types.ba.go b/examples/binapi/interface_types/interface_types.ba.go
index c1becad..a9a34d1 100644
--- a/examples/binapi/interface_types/interface_types.ba.go
+++ b/examples/binapi/interface_types/interface_types.ba.go
@@ -11,12 +11,13 @@ It consists of:
package interface_types
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
)
const (
diff --git a/examples/binapi/interfaces/interfaces.ba.go b/examples/binapi/interfaces/interfaces.ba.go
index 45fd4d2..7d4031f 100644
--- a/examples/binapi/interfaces/interfaces.ba.go
+++ b/examples/binapi/interfaces/interfaces.ba.go
@@ -15,12 +15,13 @@ It consists of:
package interfaces
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
ethernet_types "git.fd.io/govpp.git/examples/binapi/ethernet_types"
interface_types "git.fd.io/govpp.git/examples/binapi/interface_types"
diff --git a/examples/binapi/ip/ip.ba.go b/examples/binapi/ip/ip.ba.go
index 0661dc5..f02cc9e 100644
--- a/examples/binapi/ip/ip.ba.go
+++ b/examples/binapi/ip/ip.ba.go
@@ -15,12 +15,13 @@ It consists of:
package ip
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
ethernet_types "git.fd.io/govpp.git/examples/binapi/ethernet_types"
interface_types "git.fd.io/govpp.git/examples/binapi/interface_types"
diff --git a/examples/binapi/ip_types/ip_types.ba.go b/examples/binapi/ip_types/ip_types.ba.go
index 1812746..e812b8b 100644
--- a/examples/binapi/ip_types/ip_types.ba.go
+++ b/examples/binapi/ip_types/ip_types.ba.go
@@ -13,12 +13,13 @@ It consists of:
package ip_types
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
)
const (
diff --git a/examples/binapi/memclnt/memclnt.ba.go b/examples/binapi/memclnt/memclnt.ba.go
index 5c8d1eb..a3ca389 100644
--- a/examples/binapi/memclnt/memclnt.ba.go
+++ b/examples/binapi/memclnt/memclnt.ba.go
@@ -12,12 +12,13 @@ It consists of:
package memclnt
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
)
const (
diff --git a/examples/binapi/memif/memif.ba.go b/examples/binapi/memif/memif.ba.go
index c4434c4..2388cc2 100644
--- a/examples/binapi/memif/memif.ba.go
+++ b/examples/binapi/memif/memif.ba.go
@@ -13,12 +13,13 @@ It consists of:
package memif
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
ethernet_types "git.fd.io/govpp.git/examples/binapi/ethernet_types"
interface_types "git.fd.io/govpp.git/examples/binapi/interface_types"
diff --git a/examples/binapi/vpe/vpe.ba.go b/examples/binapi/vpe/vpe.ba.go
index e95cf30..48b6389 100644
--- a/examples/binapi/vpe/vpe.ba.go
+++ b/examples/binapi/vpe/vpe.ba.go
@@ -14,12 +14,13 @@ It consists of:
package vpe
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
vpe_types "git.fd.io/govpp.git/examples/binapi/vpe_types"
)
diff --git a/examples/binapi/vpe_types/vpe_types.ba.go b/examples/binapi/vpe_types/vpe_types.ba.go
index c937c8f..dbe81e0 100644
--- a/examples/binapi/vpe_types/vpe_types.ba.go
+++ b/examples/binapi/vpe_types/vpe_types.ba.go
@@ -12,12 +12,13 @@ It consists of:
package vpe_types
import (
- bytes "bytes"
- context "context"
+ "bytes"
+ "context"
+ "io"
+ "strconv"
+
api "git.fd.io/govpp.git/api"
struc "github.com/lunixbochs/struc"
- io "io"
- strconv "strconv"
)
const (