Skip to content

Commit b068284

Browse files
authored
Merge pull request #152 from ashanbrown/one-build
Do a single build for all packages
2 parents 085e0f6 + 22dc893 commit b068284

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

analyzer.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"reflect"
2828
"strings"
2929

30+
"path/filepath"
31+
3032
"golang.org/x/tools/go/loader"
3133
)
3234

@@ -93,20 +95,25 @@ func (gas *Analyzer) LoadRules(ruleDefinitions ...RuleBuilder) {
9395
}
9496

9597
// Process kicks off the analysis process for a given package
96-
func (gas *Analyzer) Process(packagePath string) error {
98+
func (gas *Analyzer) Process(packagePaths ...string) error {
99+
packageConfig := loader.Config{Build: &build.Default, ParserMode: parser.ParseComments}
100+
for _, packagePath := range packagePaths {
101+
abspath, _ := filepath.Abs(packagePath)
102+
gas.logger.Println("Searching directory:", abspath)
97103

98-
basePackage, err := build.Default.ImportDir(packagePath, build.ImportComment)
99-
if err != nil {
100-
return err
101-
}
104+
basePackage, err := build.Default.ImportDir(packagePath, build.ImportComment)
105+
if err != nil {
106+
return err
107+
}
102108

103-
packageConfig := loader.Config{Build: &build.Default, ParserMode: parser.ParseComments}
104-
var packageFiles []string
105-
for _, filename := range basePackage.GoFiles {
106-
packageFiles = append(packageFiles, path.Join(packagePath, filename))
109+
var packageFiles []string
110+
for _, filename := range basePackage.GoFiles {
111+
packageFiles = append(packageFiles, path.Join(packagePath, filename))
112+
}
113+
114+
packageConfig.CreateFromFilenames(basePackage.Name, packageFiles...)
107115
}
108116

109-
packageConfig.CreateFromFilenames(basePackage.Name, packageFiles...)
110117
builtPackage, err := packageConfig.Load()
111118
if err != nil {
112119
return err

analyzer_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ var _ = Describe("Analyzer", func() {
7373
Expect(metrics.NumFiles).To(Equal(2))
7474
})
7575

76+
It("should be able to analyze mulitple Go packages", func() {
77+
analyzer.LoadRules(rules.Generate().Builders()...)
78+
pkg1 := testutils.NewTestPackage()
79+
pkg2 := testutils.NewTestPackage()
80+
defer pkg1.Close()
81+
defer pkg2.Close()
82+
pkg1.AddFile("foo.go", `
83+
package main
84+
func main(){
85+
}`)
86+
pkg2.AddFile("bar.go", `
87+
package main
88+
func bar(){
89+
}`)
90+
pkg1.Build()
91+
pkg2.Build()
92+
err := analyzer.Process(pkg1.Path, pkg2.Path)
93+
Expect(err).ShouldNot(HaveOccurred())
94+
_, metrics := analyzer.Report()
95+
Expect(metrics.NumFiles).To(Equal(2))
96+
})
97+
7698
It("should find errors when nosec is not in use", func() {
7799

78100
// Rule for MD5 weak crypto usage

cmd/gas/main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"fmt"
2020
"log"
2121
"os"
22-
"path/filepath"
2322
"regexp"
2423
"sort"
2524
"strings"
@@ -202,19 +201,19 @@ func main() {
202201

203202
vendor := regexp.MustCompile(`[\\/]vendor([\\/]|$)`)
204203

204+
var packages []string
205205
// Iterate over packages on the import paths
206206
for _, pkg := range gotool.ImportPaths(flag.Args()) {
207207

208208
// Skip vendor directory
209209
if vendor.MatchString(pkg) {
210210
continue
211211
}
212+
packages = append(packages, pkg)
213+
}
212214

213-
abspath, _ := filepath.Abs(pkg)
214-
logger.Println("Searching directory:", abspath)
215-
if err := analyzer.Process(pkg); err != nil {
216-
logger.Fatal(err)
217-
}
215+
if err := analyzer.Process(packages...); err != nil {
216+
logger.Fatal(err)
218217
}
219218

220219
// Collect the results

0 commit comments

Comments
 (0)