Format generated Go source code in-process 67/26567/1 v0.3.4
authorOndrej Fabry <ofabry@cisco.com>
Fri, 17 Apr 2020 12:29:26 +0000 (14:29 +0200)
committerOndrej Fabry <ofabry@cisco.com>
Fri, 17 Apr 2020 12:32:55 +0000 (14:32 +0200)
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 <ofabry@cisco.com>
14 files changed:
CHANGELOG.md
Makefile
cmd/binapi-generator/generate.go
cmd/binapi-generator/main.go
examples/binapi/af_packet/af_packet.ba.go
examples/binapi/ethernet_types/ethernet_types.ba.go
examples/binapi/interface_types/interface_types.ba.go
examples/binapi/interfaces/interfaces.ba.go
examples/binapi/ip/ip.ba.go
examples/binapi/ip_types/ip_types.ba.go
examples/binapi/memclnt/memclnt.ba.go
examples/binapi/memif/memif.ba.go
examples/binapi/vpe/vpe.ba.go
examples/binapi/vpe_types/vpe_types.ba.go

index 979c304..599d549 100644 (file)
@@ -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_
 
index f6e2b9c..4297746 100644 (file)
--- 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)" \
index a8de5d5..715836d 100644 (file)
@@ -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)
index 8bf765b..fcd85ae 100644 (file)
@@ -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
 }
 
index 6f50f22..9660114 100644 (file)
@@ -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"
index c7f836b..fffa6b1 100644 (file)
@@ -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 (
index c1becad..a9a34d1 100644 (file)
@@ -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 (
index 45fd4d2..7d4031f 100644 (file)
@@ -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"
index 0661dc5..f02cc9e 100644 (file)
@@ -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"
index 1812746..e812b8b 100644 (file)
@@ -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 (
index 5c8d1eb..a3ca389 100644 (file)
@@ -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 (
index c4434c4..2388cc2 100644 (file)
@@ -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"
index e95cf30..48b6389 100644 (file)
@@ -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"
 )
index c937c8f..dbe81e0 100644 (file)
@@ -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 (