From d372b4efe26650dbc83908ca0bbee38d90aed3e9 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Fri, 17 Apr 2020 14:29:26 +0200 Subject: 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 --- cmd/binapi-generator/generate.go | 13 ++++++------- cmd/binapi-generator/main.go | 19 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) (limited to 'cmd') 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 } -- cgit 1.2.3-korg