socket adapter: don't bother sending sockclnt_delete messages
[govpp.git] / binapigen / vppapi / parser.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
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:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package vppapi
16
17 import (
18         "fmt"
19         "io/ioutil"
20         "path/filepath"
21         "strings"
22
23         "github.com/sirupsen/logrus"
24 )
25
26 const (
27         DefaultAPIDir = "/usr/share/vpp/api"
28 )
29
30 const apifileSuffixJson = ".api.json"
31
32 // FindFiles returns all input files located in specified directory
33 func FindFiles(dir string, deep int) (paths []string, err error) {
34         entries, err := ioutil.ReadDir(dir)
35         if err != nil {
36                 return nil, fmt.Errorf("reading directory %s failed: %v", dir, err)
37         }
38         for _, e := range entries {
39                 if e.IsDir() && deep > 0 {
40                         nestedDir := filepath.Join(dir, e.Name())
41                         if nested, err := FindFiles(nestedDir, deep-1); err != nil {
42                                 return nil, err
43                         } else {
44                                 paths = append(paths, nested...)
45                         }
46                 } else if strings.HasSuffix(e.Name(), apifileSuffixJson) {
47                         paths = append(paths, filepath.Join(dir, e.Name()))
48                 }
49         }
50         return paths, nil
51 }
52
53 func Parse() ([]*File, error) {
54         return ParseDir(DefaultAPIDir)
55 }
56
57 func ParseDir(apidir string) ([]*File, error) {
58         files, err := FindFiles(apidir, 1)
59         if err != nil {
60                 return nil, err
61         }
62
63         logrus.Infof("found %d files in API dir %q", len(files), apidir)
64
65         var modules []*File
66
67         for _, file := range files {
68                 module, err := ParseFile(file)
69                 if err != nil {
70                         return nil, err
71                 }
72                 modules = append(modules, module)
73         }
74
75         return modules, nil
76 }
77
78 // ParseFile parses API file contents and returns File.
79 func ParseFile(apifile string) (*File, error) {
80         if !strings.HasSuffix(apifile, apifileSuffixJson) {
81                 return nil, fmt.Errorf("unsupported file format: %q", apifile)
82         }
83
84         data, err := ioutil.ReadFile(apifile)
85         if err != nil {
86                 return nil, fmt.Errorf("reading file %s failed: %v", apifile, err)
87         }
88
89         base := filepath.Base(apifile)
90         name := base[:strings.Index(base, ".")]
91
92         logf("parsing file %q", base)
93
94         module, err := ParseRaw(data)
95         if err != nil {
96                 return nil, fmt.Errorf("parsing file %s failed: %v", base, err)
97         }
98         module.Name = name
99         module.Path = apifile
100
101         return module, nil
102 }
103
104 func ParseRaw(data []byte) (file *File, err error) {
105         file, err = parseJSON(data)
106         if err != nil {
107                 return nil, err
108         }
109
110         return file, nil
111 }