package main
import (
- "bytes"
- "encoding/json"
"flag"
"fmt"
- "io/ioutil"
"os"
- "os/exec"
- "path/filepath"
- "strings"
- "github.com/bennyscetbun/jsongo"
"github.com/sirupsen/logrus"
-)
-var (
- inputFile = flag.String("input-file", "", "Input file with VPP API in JSON format.")
- inputDir = flag.String("input-dir", ".", "Input directory with VPP API files in JSON format.")
- outputDir = flag.String("output-dir", ".", "Output directory where package folders will be generated.")
- includeAPIVer = flag.Bool("include-apiver", true, "Include APIVersion constant for each module.")
- 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.")
- includeServices = flag.Bool("include-services", false, "Include service interface with client implementation.")
- continueOnError = flag.Bool("continue-onerror", false, "Continue with next file on error.")
- debug = flag.Bool("debug", debugMode, "Enable debug mode.")
+ "git.fd.io/govpp.git/binapigen"
+ "git.fd.io/govpp.git/binapigen/vppapi"
+ "git.fd.io/govpp.git/version"
)
-var debugMode = os.Getenv("DEBUG_BINAPI_GENERATOR") != ""
+func init() {
+ flag.Usage = func() {
+ fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTION]... [API]...\n", os.Args[0])
+ fmt.Fprintln(flag.CommandLine.Output(), "Generate code for each API.")
+ fmt.Fprintf(flag.CommandLine.Output(), "Example: %s -output-dir=binapi acl interface l2\n", os.Args[0])
+ fmt.Fprintln(flag.CommandLine.Output())
+ fmt.Fprintln(flag.CommandLine.Output(), "Options:")
+ flag.CommandLine.PrintDefaults()
+ }
+}
func main() {
+ var (
+ theInputFile = flag.String("input-file", "", "Input VPP API file. (DEPRECATED: Use program arguments to define VPP API files)")
+ theApiDir = flag.String("input-dir", vppapi.DefaultAPIDir, "Directory with VPP API files.")
+ theOutputDir = flag.String("output-dir", ".", "Output directory where code will be generated.")
+
+ importPrefix = flag.String("import-prefix", "", "Define import path prefix to be used to import types.")
+ importTypes = flag.Bool("import-types", false, "Generate packages for imported types.")
+ includeAPIVer = flag.Bool("include-apiver", true, "Include APIVersion constant for each module.")
+ 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", true, "Include binary API names in struct tag.")
+ includeVppVersion = flag.Bool("include-vpp-version", true, "Include version of the VPP that provided input files.")
+
+ debugMode = flag.Bool("debug", os.Getenv("DEBUG_GOVPP") != "", "Enable debug mode.")
+ printVersion = flag.Bool("version", false, "Prints version and exits.")
+ )
flag.Parse()
- if *debug {
- logrus.SetLevel(logrus.DebugLevel)
+
+ if *printVersion {
+ fmt.Fprintln(os.Stdout, version.Info())
+ os.Exit(0)
}
- if *inputFile == "" && *inputDir == "" {
- fmt.Fprintln(os.Stderr, "ERROR: input-file or input-dir must be specified")
- os.Exit(1)
+ if flag.NArg() == 1 && flag.Arg(0) == "version" {
+ fmt.Fprintln(os.Stdout, version.Verbose())
+ os.Exit(0)
}
- if *inputFile != "" {
- // process one input file
- if err := generateFromFile(*inputFile, *outputDir); err != nil {
- fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", *inputFile, err)
+ var opts binapigen.Options
+
+ if *theInputFile != "" {
+ if flag.NArg() > 0 {
+ fmt.Fprintln(os.Stderr, "input-file cannot be combined with files to generate in arguments")
os.Exit(1)
}
+ opts.FilesToGenerate = append(opts.FilesToGenerate, *theInputFile)
} else {
- // process all files in specified directory
- files, err := getInputFiles(*inputDir)
- if err != nil {
- fmt.Fprintf(os.Stderr, "ERROR: code generation failed: %v\n", err)
- os.Exit(1)
- }
- for _, file := range files {
- if err := generateFromFile(file, *outputDir); err != nil {
- fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", file, err)
- if *continueOnError {
- continue
- }
- os.Exit(1)
- }
- }
- }
-}
-
-// getInputFiles returns all input files located in specified directory
-func getInputFiles(inputDir string) (res []string, err error) {
- files, err := ioutil.ReadDir(inputDir)
- if err != nil {
- return nil, fmt.Errorf("reading directory %s failed: %v", inputDir, err)
- }
- for _, f := range files {
- if strings.HasSuffix(f.Name(), inputFileExt) {
- res = append(res, filepath.Join(inputDir, f.Name()))
- }
- }
- return res, nil
-}
-
-// generateFromFile generates Go package from one input JSON file
-func generateFromFile(inputFile, outputDir string) error {
- logf("generating from file: %s", inputFile)
- logf("------------------------------------------------------------")
- defer logf("------------------------------------------------------------")
-
- ctx, err := getContext(inputFile, outputDir)
- if err != nil {
- return err
+ opts.FilesToGenerate = append(opts.FilesToGenerate, flag.Args()...)
}
// prepare options
- ctx.includeAPIVersion = *includeAPIVer
- ctx.includeComments = *includeComments
- ctx.includeBinapiNames = *includeBinapiNames
- ctx.includeServices = *includeServices
-
- // read API definition from input file
- ctx.inputData, err = ioutil.ReadFile(ctx.inputFile)
- if err != nil {
- return fmt.Errorf("reading input file %s failed: %v", ctx.inputFile, err)
- }
-
- // parse JSON data into objects
- jsonRoot := new(jsongo.JSONNode)
- if err := json.Unmarshal(ctx.inputData, jsonRoot); err != nil {
- return fmt.Errorf("unmarshalling JSON failed: %v", err)
- }
- ctx.packageData, err = parsePackage(ctx, jsonRoot)
- if err != nil {
- return fmt.Errorf("parsing package %s failed: %v", ctx.packageName, err)
- }
-
- // generate Go package code
- var buf bytes.Buffer
- if err := generatePackage(ctx, &buf); err != nil {
- return fmt.Errorf("generating code for package %s failed: %v", ctx.packageName, err)
- }
-
- // create output directory
- packageDir := filepath.Dir(ctx.outputFile)
- if err := os.MkdirAll(packageDir, 06); err != nil {
- 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 {
- 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))
- }
-
- // count number of lines in generated output file
- cmd = exec.Command("wc", "-l", ctx.outputFile)
- if output, err := cmd.CombinedOutput(); err != nil {
- logf("wc command failed: %v\n%s", err, string(output))
+ if ver := os.Getenv("VPP_API_VERSION"); ver != "" {
+ // use version from env var if set
+ opts.VPPVersion = ver
} else {
- logf("number of generated lines: %s", output)
+ opts.VPPVersion = ResolveVppVersion(*theApiDir)
+ }
+ opts.IncludeAPIVersion = *includeAPIVer
+ opts.IncludeComments = *includeComments
+ opts.IncludeBinapiNames = *includeBinapiNames
+ opts.IncludeServices = *includeServices
+ opts.IncludeVppVersion = *includeVppVersion
+ opts.ImportPrefix = *importPrefix
+ opts.ImportTypes = *importTypes
+
+ if *debugMode {
+ logrus.SetLevel(logrus.DebugLevel)
+ logrus.Debug("debug mode enabled")
}
- return nil
-}
+ apiDir := *theApiDir
+ outputDir := *theOutputDir
-func logf(f string, v ...interface{}) {
- if *debug {
- logrus.Debugf(f, v...)
- }
+ binapigen.Run(apiDir, opts, func(g *binapigen.Generator) error {
+ for _, file := range g.Files {
+ if !file.Generate {
+ continue
+ }
+ binapigen.GenerateBinapiFile(g, file, outputDir)
+ if g.IncludeServices && file.Service != nil {
+ binapigen.GenerateRPC(g, file, outputDir)
+ }
+ }
+ return nil
+ })
}