generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 78
/
file.go
76 lines (71 loc) · 2.1 KB
/
file.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
package coder
import (
"bytes"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"regexp"
"strconv"
"github.com/layer5io/meshkit/cmd/errorutil/internal/component"
errutilerr "github.com/layer5io/meshkit/cmd/errorutil/internal/error"
"github.com/sirupsen/logrus"
)
func handleFile(path string, update bool, updateAll bool, infoAll *errutilerr.InfoAll, comp *component.Info) error {
logger := logrus.WithFields(logrus.Fields{"path": path})
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return err
}
logger.WithFields(logrus.Fields{"update": update}).Info("inspecting file")
anyValueChanged := false
ast.Inspect(file, func(n ast.Node) bool {
if pgkid, ok := isNewDefaultCallExpr(n); ok {
logger.Warnf("Usage of deprecated function %s.NewDefault detected.", pgkid)
if !contains(infoAll.DeprecatedNewDefault, path) {
infoAll.DeprecatedNewDefault = append(infoAll.DeprecatedNewDefault, path)
}
// If a NewDefault call expression is detected, child-nodes are not inspected.
// This would lead to duplicates detections in case of dot-import.
return false
}
if newErr, ok := isNewCallExpr(n); ok {
name := newErr.Name
logger.Infof("New.Error(...) call detected, error code name: '%s'", name)
_, ok := infoAll.Errors[name]
if !ok {
infoAll.Errors[name] = []errutilerr.Error{}
}
infoAll.Errors[name] = append(infoAll.Errors[name], *newErr)
// If a New call expression is detected, child-nodes are not inspected:
return false
}
if handleValueSpec(n, update, updateAll, comp, logger, path, infoAll) {
anyValueChanged = true
}
return true
})
if update && anyValueChanged {
logger.Info("writing updated file")
buf := new(bytes.Buffer)
err = format.Node(buf, fset, file)
if err != nil {
return err
}
err = os.WriteFile(path, buf.Bytes(), 0600)
if err != nil {
return err
}
}
return nil
}
func isErrorCodeVarName(name string) bool {
matched, _ := regexp.MatchString("^Err[A-Z].+Code$", name)
return matched
}
func isInt(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}