Generator improvements and cleanup
[govpp.git] / cmd / binapi-generator / main.go
index 8045212..b3a131c 100644 (file)
@@ -20,32 +20,36 @@ import (
        "flag"
        "fmt"
        "io/ioutil"
-       "log"
        "os"
        "os/exec"
        "path/filepath"
        "strings"
 
        "github.com/bennyscetbun/jsongo"
+       "github.com/sirupsen/logrus"
 )
 
 var (
-       inputFile       = flag.String("input-file", "", "Input JSON file.")
-       inputDir        = flag.String("input-dir", ".", "Input directory with JSON files.")
+       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", false, "Whether to include VlAPIVersion in generated file.")
-       debug           = flag.Bool("debug", false, "Turn on debug mode.")
-       continueOnError = flag.Bool("continue-onerror", false, "Wheter to continue with next file on error.")
+       includeAPIVer   = flag.Bool("include-apiver", false, "Include APIVersion constant for each module.")
+       includeComments = flag.Bool("include-comments", false, "Include JSON API source in comments for each object.")
+       continueOnError = flag.Bool("continue-onerror", false, "Continue with next file on error.")
+       debug           = flag.Bool("debug", false, "Enable debug mode.")
 )
 
-func logf(f string, v ...interface{}) {
-       if *debug {
-               log.Printf(f, v...)
-       }
+var log = logrus.Logger{
+       Level:     logrus.InfoLevel,
+       Formatter: &logrus.TextFormatter{},
+       Out:       os.Stdout,
 }
 
 func main() {
        flag.Parse()
+       if *debug {
+               logrus.SetLevel(logrus.DebugLevel)
+       }
 
        if *inputFile == "" && *inputDir == "" {
                fmt.Fprintln(os.Stderr, "ERROR: input-file or input-dir must be specified")
@@ -101,6 +105,9 @@ func generateFromFile(inputFile, outputDir string) error {
                return err
        }
 
+       ctx.includeAPIVersionCrc = *includeAPIVer
+       ctx.includeComments = *includeComments
+
        // read input file contents
        ctx.inputData, err = readFile(inputFile)
        if err != nil {
@@ -143,7 +150,7 @@ func generateFromFile(inputFile, outputDir string) error {
        // count number of lines in generated output file
        cmd = exec.Command("wc", "-l", ctx.outputFile)
        if output, err := cmd.CombinedOutput(); err != nil {
-               log.Printf("wc command failed: %v\n%s", err, string(output))
+               log.Warnf("wc command failed: %v\n%s", err, string(output))
        } else {
                logf("generated lines: %s", output)
        }
@@ -171,3 +178,9 @@ func parseJSON(inputData []byte) (*jsongo.JSONNode, error) {
 
        return &root, nil
 }
+
+func logf(f string, v ...interface{}) {
+       if *debug {
+               logrus.Debugf(f, v...)
+       }
+}