diff --git a/analyzer.go b/analyzer.go index 23deb65..12aff3c 100644 --- a/analyzer.go +++ b/analyzer.go @@ -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)) } diff --git a/analyzer_test.go b/analyzer_test.go index 2376d43..e69a822 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -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 diff --git a/cmd/gas/main.go b/cmd/gas/main.go index 37d876b..ef20462 100644 --- a/cmd/gas/main.go +++ b/cmd/gas/main.go @@ -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 { @@ -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...) } @@ -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) diff --git a/config.go b/config.go index ba9b9cd..09b97d3 100644 --- a/config.go +++ b/config.go @@ -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{} @@ -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 } @@ -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 @@ -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 }