Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gcmurphy committed Dec 14, 2017
1 parent af25ac1 commit 25d74c6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (gas *Analyzer) Process(packagePath string) error {
}

packageConfig := loader.Config{Build: &build.Default, ParserMode: parser.ParseComments}
packageFiles := make([]string, 0)
var packageFiles []string
for _, filename := range basePackage.GoFiles {
packageFiles = append(packageFiles, path.Join(packagePath, filename))
}
Expand Down
21 changes: 21 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ var _ = Describe("Analyzer", func() {

})

It("should be able to analyze mulitple Go files", func() {
analyzer.LoadRules(rules.Generate().Builders()...)
pkg := testutils.NewTestPackage()
defer pkg.Close()
pkg.AddFile("foo.go", `
package main
func main(){
bar()
}`)
pkg.AddFile("bar.go", `
package main
func bar(){
println("package has two files!")
}`)
pkg.Build()
err := analyzer.Process(pkg.Path)
Expect(err).ShouldNot(HaveOccurred())
_, metrics := analyzer.Report()
Expect(metrics.NumFiles).To(Equal(2))
})

It("should find errors when nosec is not in use", func() {

// Rule for MD5 weak crypto usage
Expand Down
11 changes: 10 additions & 1 deletion cmd/gas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func usage() {
flag.PrintDefaults()
fmt.Fprint(os.Stderr, "\n\nRULES:\n\n")

// sorted rule list for eas of reading
// sorted rule list for ease of reading
rl := rules.Generate()
keys := make([]string, 0, len(rl))
for key := range rl {
Expand Down Expand Up @@ -126,13 +126,19 @@ func loadConfig(configFile string) (gas.Config, error) {
func loadRules(include, exclude string) rules.RuleList {
var filters []rules.RuleFilter
if include != "" {
log.Printf("including rules: %s", include)
including := strings.Split(include, ",")
filters = append(filters, rules.NewRuleFilter(false, including...))
} else {
log.Println("including rules: default")
}

if exclude != "" {
log.Printf("excluding rules: %s", exclude)
excluding := strings.Split(exclude, ",")
filters = append(filters, rules.NewRuleFilter(true, excluding...))
} else {
log.Println("excluding rules: default")
}
return rules.Generate(filters...)
}
Expand Down Expand Up @@ -186,6 +192,9 @@ func main() {

// Load enabled rule definitions
ruleDefinitions := loadRules(*flagRulesInclude, *flagRulesExclude)
if len(ruleDefinitions) <= 0 {
log.Fatal("cannot continue: no rules are configured.")
}

// Create the analyzer
analyzer := gas.NewAnalyzer(config, logger)
Expand Down
12 changes: 9 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"io/ioutil"
)

const (
// Globals are applicable to all rules and used for general
// configuration settings for gas.
Globals = "global"
)

// Config is used to provide configuration and customization to each of the rules.
type Config map[string]interface{}

Expand All @@ -16,7 +22,7 @@ type Config map[string]interface{}
// or from a *os.File.
func NewConfig() Config {
cfg := make(Config)
cfg["global"] = make(map[string]string)
cfg[Globals] = make(map[string]string)
return cfg
}

Expand Down Expand Up @@ -60,7 +66,7 @@ func (c Config) Set(section string, value interface{}) {

// GetGlobal returns value associated with global configuration option
func (c Config) GetGlobal(option string) (string, error) {
if globals, ok := c["global"]; ok {
if globals, ok := c[Globals]; ok {
if settings, ok := globals.(map[string]string); ok {
if value, ok := settings[option]; ok {
return value, nil
Expand All @@ -74,7 +80,7 @@ func (c Config) GetGlobal(option string) (string, error) {

// SetGlobal associates a value with a global configuration ooption
func (c Config) SetGlobal(option, value string) {
if globals, ok := c["global"]; ok {
if globals, ok := c[Globals]; ok {
if settings, ok := globals.(map[string]string); ok {
settings[option] = value
}
Expand Down

0 comments on commit 25d74c6

Please sign in to comment.