forked from meshery/meshkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
80 lines (72 loc) · 2.04 KB
/
path.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
package coder
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/layer5io/meshkit/cmd/errorutil/internal/component"
mesherr "github.com/layer5io/meshkit/cmd/errorutil/internal/error"
"github.com/sirupsen/logrus"
)
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func walk(globalFlags globalFlags, update bool, updateAll bool, errorsInfo *mesherr.InfoAll) error {
subDirsToSkip := append([]string{".git", ".github"}, globalFlags.skipDirs...)
logrus.Info(fmt.Sprintf("root directory: %s", globalFlags.rootDir))
logrus.Info(fmt.Sprintf("output directory: %s", globalFlags.outDir))
logrus.Info(fmt.Sprintf("info directory: %s", globalFlags.infoDir))
logrus.Info(fmt.Sprintf("subdirs to skip: %v", subDirsToSkip))
comp, err := component.New(globalFlags.infoDir)
if err != nil {
return err
}
err = filepath.Walk(globalFlags.rootDir, func(path string, info os.FileInfo, err error) error {
logger := logrus.WithFields(logrus.Fields{"path": path})
if err != nil {
logger.WithFields(logrus.Fields{"error": fmt.Sprintf("%v", err)}).Warn("failure accessing path")
return err
}
if info.IsDir() && contains(subDirsToSkip, info.Name()) {
logger.Infof("skipping directory %s", info.Name())
return filepath.SkipDir
}
if info.IsDir() {
logger.Debug("handling dir")
} else {
if includeFile(path) {
isErrorsGoFile := isErrorGoFile(path)
logger.WithFields(logrus.Fields{"iserrorsfile": fmt.Sprintf("%v", isErrorsGoFile)}).Debug("handling Go file")
err := handleFile(path, update && isErrorsGoFile, updateAll, errorsInfo, comp)
if err != nil {
return err
}
} else {
logger.Debug("skipping file")
}
}
return nil
})
if update {
err = comp.Write()
}
return err
}
func isErrorGoFile(path string) bool {
_, file := filepath.Split(path)
return file == "error.go"
}
func includeFile(path string) bool {
if strings.HasSuffix(path, "_test.go") {
return false
}
if filepath.Ext(path) == ".go" {
return true
}
return false
}