1 // Copyright (c) 2018 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
28 "github.com/bennyscetbun/jsongo"
29 "github.com/sirupsen/logrus"
31 "git.fd.io/govpp.git/version"
35 theInputFile = flag.String("input-file", "", "Input file with VPP API in JSON format.")
36 theInputTypes = flag.String("input-types", "", "Types input file with VPP API in JSON format. (split by comma)")
37 theInputDir = flag.String("input-dir", "/usr/share/vpp/api", "Input directory with VPP API files in JSON format.")
38 theOutputDir = flag.String("output-dir", ".", "Output directory where package folders will be generated.")
40 includeAPIVer = flag.Bool("include-apiver", true, "Include APIVersion constant for each module.")
41 includeServices = flag.Bool("include-services", true, "Include RPC service api and client implementation.")
42 includeComments = flag.Bool("include-comments", false, "Include JSON API source in comments for each object.")
43 includeBinapiNames = flag.Bool("include-binapi-names", false, "Include binary API names in struct tag.")
45 continueOnError = flag.Bool("continue-onerror", false, "Continue with next file on error.")
46 debugMode = flag.Bool("debug", os.Getenv("GOVPP_DEBUG") != "", "Enable debug mode.")
48 printVersion = flag.Bool("version", false, "Prints current version and exits.")
60 switch cmd := flag.Arg(0); cmd {
62 fmt.Fprintln(os.Stdout, version.Verbose())
66 fmt.Fprintf(os.Stderr, "unknown command: %s\n", cmd)
73 fmt.Fprintln(os.Stdout, version.Info())
78 logrus.SetLevel(logrus.DebugLevel)
79 logrus.Info("debug mode enabled")
82 if err := run(*theInputFile, *theInputDir, *theOutputDir, *continueOnError); err != nil {
83 logrus.Errorln("binapi-generator:", err)
88 func run(inputFile, inputDir string, outputDir string, continueErr bool) (err error) {
89 if inputFile == "" && inputDir == "" {
90 return fmt.Errorf("input-file or input-dir must be specified")
93 var typesPkgs []*context
94 if *theInputTypes != "" {
95 types := strings.Split(*theInputTypes, ",")
96 typesPkgs, err = loadTypesPackages(types...)
98 return fmt.Errorf("loading types input failed: %v", err)
103 // process one input file
104 if err := generateFromFile(inputFile, outputDir, typesPkgs); err != nil {
105 return fmt.Errorf("code generation from %s failed: %v\n", inputFile, err)
108 // process all files in specified directory
109 dir, err := filepath.Abs(inputDir)
111 return fmt.Errorf("invalid input directory: %v\n", err)
113 files, err := getInputFiles(inputDir, 1)
115 return fmt.Errorf("problem getting files from input directory: %v\n", err)
116 } else if len(files) == 0 {
117 return fmt.Errorf("no input files found in input directory: %v\n", dir)
119 for _, file := range files {
120 if err := generateFromFile(file, outputDir, typesPkgs); err != nil {
122 logrus.Warnf("code generation from %s failed: %v (error ignored)\n", file, err)
125 return fmt.Errorf("code generation from %s failed: %v\n", file, err)
134 // getInputFiles returns all input files located in specified directory
135 func getInputFiles(inputDir string, deep int) (files []string, err error) {
136 entries, err := ioutil.ReadDir(inputDir)
138 return nil, fmt.Errorf("reading directory %s failed: %v", inputDir, err)
140 for _, e := range entries {
141 if e.IsDir() && deep > 0 {
142 nestedDir := filepath.Join(inputDir, e.Name())
143 if nested, err := getInputFiles(nestedDir, deep-1); err != nil {
146 files = append(files, nested...)
148 } else if strings.HasSuffix(e.Name(), inputFileExt) {
149 files = append(files, filepath.Join(inputDir, e.Name()))
155 func parseInputJSON(inputData []byte) (*jsongo.Node, error) {
156 jsonRoot := new(jsongo.Node)
157 if err := json.Unmarshal(inputData, jsonRoot); err != nil {
158 return nil, fmt.Errorf("unmarshalling JSON failed: %v", err)
163 // generateFromFile generates Go package from one input JSON file
164 func generateFromFile(inputFile, outputDir string, typesPkgs []*context) error {
165 // create generator context
166 ctx, err := newContext(inputFile, outputDir)
171 logf("------------------------------------------------------------")
172 logf("module: %s", ctx.moduleName)
173 logf(" - input: %s", ctx.inputFile)
174 logf(" - output: %s", ctx.outputFile)
175 logf("------------------------------------------------------------")
178 ctx.includeAPIVersion = *includeAPIVer
179 ctx.includeComments = *includeComments
180 ctx.includeBinapiNames = *includeBinapiNames
181 ctx.includeServices = *includeServices
183 // read API definition from input file
184 ctx.inputData, err = ioutil.ReadFile(ctx.inputFile)
186 return fmt.Errorf("reading input file %s failed: %v", ctx.inputFile, err)
188 // parse JSON data into objects
189 jsonRoot, err := parseInputJSON(ctx.inputData)
191 return fmt.Errorf("parsing JSON input failed: %v", err)
193 ctx.packageData, err = parsePackage(ctx, jsonRoot)
195 return fmt.Errorf("parsing package %s failed: %v", ctx.packageName, err)
198 if len(typesPkgs) > 0 {
199 err = loadTypeAliases(ctx, typesPkgs)
201 return fmt.Errorf("loading type aliases failed: %v", err)
205 // generate Go package code
207 if err := generatePackage(ctx, &buf); err != nil {
208 return fmt.Errorf("generating code for package %s failed: %v", ctx.packageName, err)
211 // create output directory
212 packageDir := filepath.Dir(ctx.outputFile)
213 if err := os.MkdirAll(packageDir, 0775); err != nil {
214 return fmt.Errorf("creating output dir %s failed: %v", packageDir, err)
216 // write generated code to output file
217 if err := ioutil.WriteFile(ctx.outputFile, buf.Bytes(), 0666); err != nil {
218 return fmt.Errorf("writing to output file %s failed: %v", ctx.outputFile, err)
221 // go format the output file (fail probably means the output is not compilable)
222 cmd := exec.Command("gofmt", "-w", ctx.outputFile)
223 if output, err := cmd.CombinedOutput(); err != nil {
224 return fmt.Errorf("gofmt failed: %v\n%s", err, string(output))
230 func loadTypesPackages(types ...string) ([]*context, error) {
232 for _, inputFile := range types {
233 // create generator context
234 ctx, err := newContext(inputFile, "")
238 // read API definition from input file
239 ctx.inputData, err = ioutil.ReadFile(ctx.inputFile)
241 return nil, fmt.Errorf("reading input file %s failed: %v", ctx.inputFile, err)
243 // parse JSON data into objects
244 jsonRoot, err := parseInputJSON(ctx.inputData)
246 return nil, fmt.Errorf("parsing JSON input failed: %v", err)
248 ctx.packageData, err = parsePackage(ctx, jsonRoot)
250 return nil, fmt.Errorf("parsing package %s failed: %v", ctx.packageName, err)
252 ctxs = append(ctxs, ctx)
257 func loadTypeAliases(ctx *context, typesCtxs []*context) error {
258 for _, t := range ctx.packageData.Types {
259 for _, c := range typesCtxs {
260 if _, ok := ctx.packageData.Imports[t.Name]; ok {
263 for _, at := range c.packageData.Types {
264 if at.Name != t.Name {
267 if len(at.Fields) != len(t.Fields) {
270 ctx.packageData.Imports[t.Name] = Import{
271 Package: c.packageName,
276 for _, t := range ctx.packageData.Aliases {
277 for _, c := range typesCtxs {
278 if _, ok := ctx.packageData.Imports[t.Name]; ok {
281 for _, at := range c.packageData.Aliases {
282 if at.Name != t.Name {
285 if at.Length != t.Length {
288 if at.Type != t.Type {
291 ctx.packageData.Imports[t.Name] = Import{
292 Package: c.packageName,
297 for _, t := range ctx.packageData.Enums {
298 for _, c := range typesCtxs {
299 if _, ok := ctx.packageData.Imports[t.Name]; ok {
302 for _, at := range c.packageData.Enums {
303 if at.Name != t.Name {
306 if at.Type != t.Type {
309 ctx.packageData.Imports[t.Name] = Import{
310 Package: c.packageName,
315 for _, t := range ctx.packageData.Unions {
316 for _, c := range typesCtxs {
317 if _, ok := ctx.packageData.Imports[t.Name]; ok {
320 for _, at := range c.packageData.Unions {
321 if at.Name != t.Name {
324 ctx.packageData.Imports[t.Name] = Import{
325 Package: c.packageName,
333 func logf(f string, v ...interface{}) {
335 logrus.Debugf(f, v...)