forked from google/gnostic-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.go
100 lines (91 loc) · 2.94 KB
/
analysis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This is an aggregation tool which scans for and aggregates incompatibilities
// in OpenAPI documents within a given directory. The only argument given to this
// tool should be the intended directory.
package main
import (
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/google/gnostic-grpc/incompatibility"
"github.com/google/gnostic-grpc/utils"
)
// main function for aggreation tool
func main() {
if len(os.Args) != 2 {
exitIfError(errors.New("argument should be a path to a directory"))
}
dir := os.Args[1]
analysis := generateAnalysis(dir)
exitIfError(writeAnalysis(dir, analysis))
os.Exit(0)
}
func writeAnalysis(analysisName string, analysis *incompatibility.ApiSetIncompatibility) error {
pbMessage, msgErr := utils.ProtoTextBytes(analysis)
if msgErr != nil {
return msgErr
}
dirName := filepath.Base(filepath.Dir(analysisName))
f, fileErr := os.Create(dirName + "_analysis.pb")
if fileErr != nil {
return fileErr
}
f.Write(pbMessage)
return nil
}
// runs analysis on given directory
func generateAnalysis(dirPath string) *incompatibility.ApiSetIncompatibility {
var reports []*incompatibility.IncompatibilityReport
readingDirectoryErr := filepath.WalkDir(dirPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Printf("walk error for file at %s", path)
return nil
}
if d.IsDir() {
return nil
}
incompatibilityReport, analysisErr := fileHandler(path)
if analysisErr != nil {
log.Printf("unable to produce analysis for file %s with error <%s>", path, analysisErr.Error())
} else {
reports = append(reports, incompatibilityReport)
}
return nil
})
if readingDirectoryErr != nil {
log.Println("unable to walk through directory")
}
analysisAggregation := incompatibility.AggregateReports(reports...)
return analysisAggregation
}
// fileHander attempts to parse the file at path to then create an incompatibility report
func fileHandler(path string) (*incompatibility.IncompatibilityReport, error) {
openAPIDoc, err := utils.ParseOpenAPIDoc(path)
if err != nil {
return nil, err
}
incompatibilityReport := incompatibility.ScanIncompatibilities(openAPIDoc, path)
log.Printf("created incompatibility report for file at %s\n", path)
return incompatibilityReport, nil
}
func exitIfError(e error) {
if e == nil {
return
}
log.Printf("Exiting with error: %s\n", e)
os.Exit(1)
}