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.
29 "github.com/bennyscetbun/jsongo"
33 inputFile = flag.String("input-file", "", "Input JSON file.")
34 inputDir = flag.String("input-dir", ".", "Input directory with JSON files.")
35 outputDir = flag.String("output-dir", ".", "Output directory where package folders will be generated.")
36 includeAPIVer = flag.Bool("include-apiver", false, "Whether to include VlAPIVersion in generated file.")
37 debug = flag.Bool("debug", false, "Turn on debug mode.")
38 continueOnError = flag.Bool("continue-onerror", false, "Wheter to continue with next file on error.")
41 func logf(f string, v ...interface{}) {
50 if *inputFile == "" && *inputDir == "" {
51 fmt.Fprintln(os.Stderr, "ERROR: input-file or input-dir must be specified")
56 // process one input file
57 if err := generateFromFile(*inputFile, *outputDir); err != nil {
58 fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", *inputFile, err)
62 // process all files in specified directory
63 files, err := getInputFiles(*inputDir)
65 fmt.Fprintf(os.Stderr, "ERROR: code generation failed: %v\n", err)
68 for _, file := range files {
69 if err := generateFromFile(file, *outputDir); err != nil {
70 fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", file, err)
80 // getInputFiles returns all input files located in specified directory
81 func getInputFiles(inputDir string) (res []string, err error) {
82 files, err := ioutil.ReadDir(inputDir)
84 return nil, fmt.Errorf("reading directory %s failed: %v", inputDir, err)
86 for _, f := range files {
87 if strings.HasSuffix(f.Name(), inputFileExt) {
88 res = append(res, filepath.Join(inputDir, f.Name()))
94 // generateFromFile generates Go package from one input JSON file
95 func generateFromFile(inputFile, outputDir string) error {
96 logf("generating from file: %q", inputFile)
97 defer logf("--------------------------------------")
99 ctx, err := getContext(inputFile, outputDir)
104 // read input file contents
105 ctx.inputData, err = readFile(inputFile)
109 // parse JSON data into objects
110 jsonRoot, err := parseJSON(ctx.inputData)
114 ctx.packageData, err = parsePackage(ctx, jsonRoot)
119 // create output directory
120 packageDir := filepath.Dir(ctx.outputFile)
121 if err := os.MkdirAll(packageDir, 0777); err != nil {
122 return fmt.Errorf("creating output directory %q failed: %v", packageDir, err)
125 f, err := os.Create(ctx.outputFile)
127 return fmt.Errorf("creating output file %q failed: %v", ctx.outputFile, err)
131 // generate Go package code
132 w := bufio.NewWriter(f)
133 if err := generatePackage(ctx, w); err != nil {
137 // go format the output file (fail probably means the output is not compilable)
138 cmd := exec.Command("gofmt", "-w", ctx.outputFile)
139 if output, err := cmd.CombinedOutput(); err != nil {
140 return fmt.Errorf("gofmt failed: %v\n%s", err, string(output))
143 // count number of lines in generated output file
144 cmd = exec.Command("wc", "-l", ctx.outputFile)
145 if output, err := cmd.CombinedOutput(); err != nil {
146 log.Printf("wc command failed: %v\n%s", err, string(output))
148 logf("generated lines: %s", output)
154 // readFile reads content of a file into memory
155 func readFile(inputFile string) ([]byte, error) {
156 inputData, err := ioutil.ReadFile(inputFile)
158 return nil, fmt.Errorf("reading data from file failed: %v", err)
161 return inputData, nil
164 // parseJSON parses a JSON data into an in-memory tree
165 func parseJSON(inputData []byte) (*jsongo.JSONNode, error) {
166 root := jsongo.JSONNode{}
168 if err := json.Unmarshal(inputData, &root); err != nil {
169 return nil, fmt.Errorf("unmarshalling JSON failed: %v", err)