X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=cmd%2Fbinapi-generator%2Fmain.go;h=fcd85ae97b2da5614a82771af8084b6305f1b4b6;hb=d372b4efe26650dbc83908ca0bbee38d90aed3e9;hp=e0e2f0815cf5a44ce4f81d60d2bebdca052e8f5d;hpb=2df59463fcbb1a5aec2173712b32fb9740157a9d;p=govpp.git diff --git a/cmd/binapi-generator/main.go b/cmd/binapi-generator/main.go index e0e2f08..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" @@ -41,6 +41,7 @@ var ( includeServices = flag.Bool("include-services", true, "Include RPC service api and client implementation.") includeComments = flag.Bool("include-comments", false, "Include JSON API source in comments for each object.") includeBinapiNames = flag.Bool("include-binapi-names", false, "Include binary API names in struct tag.") + importPrefix = flag.String("import-prefix", "", "Define import path prefix to be used to import types.") continueOnError = flag.Bool("continue-onerror", false, "Continue with next file on error.") debugMode = flag.Bool("debug", os.Getenv("GOVPP_DEBUG") != "", "Enable debug mode.") @@ -179,6 +180,7 @@ func generateFromFile(inputFile, outputDir string, typesPkgs []*context) error { ctx.includeComments = *includeComments ctx.includeBinapiNames = *includeBinapiNames ctx.includeServices = *includeServices + ctx.importPrefix = *importPrefix // read API definition from input file ctx.inputData, err = ioutil.ReadFile(ctx.inputFile) @@ -202,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 @@ -214,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 }