Skip to content

Commit

Permalink
Add output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
caitlinelfring committed Aug 27, 2020
1 parent 2db4342 commit 7506201
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 9 deletions.
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
ruleConfig string
debug bool
stdin bool
output string

// Version is populated by goreleaser during build
// Version...
Expand Down Expand Up @@ -92,8 +93,9 @@ Provide a list file globs for files you'd like to check.`,
}

if len(results) > 0 {
for _, fr := range results {
cmd.Println(fr.String())
print := config.CreatePrinter(output)
for _, res := range results {
print.Print(res)
}

if exitOneOnFailure {
Expand Down Expand Up @@ -127,6 +129,7 @@ func init() {
rootCmd.PersistentFlags().BoolVar(&exitOneOnFailure, "exit-1-on-failure", false, "Exit with exit code 1 on failures")
rootCmd.PersistentFlags().BoolVar(&stdin, "stdin", false, "Read from stdin")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug logging")
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", config.OutFormatText, fmt.Sprintf("Output type [%s]", config.OutFormatsString))
}

func setLogLevel() {
Expand Down
32 changes: 32 additions & 0 deletions pkg/config/outformat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package config

import (
"strings"

"github.com/caitlinelfring/woke/pkg/printer"
"github.com/rs/zerolog/log"
)

const (
OutFormatText = "text"
OutFormatSimple = "simple"
)

var OutFormats = []string{
OutFormatText,
OutFormatSimple,
}

var OutFormatsString = strings.Join(OutFormats, ",")

func CreatePrinter(f string) printer.Printer {
var p printer.Printer
switch f {
case OutFormatText:
p = printer.NewText()
case OutFormatSimple:
p = printer.NewSimple()
}
log.Debug().Str("printer", f).Msg("created new printer")
return p
}
14 changes: 10 additions & 4 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ func (p *Parser) ParseFiles(files []string, ignorer *ignore.Ignore) (results []*
parsable := WalkDirsWithIgnores(files, ignorer)

for _, f := range parsable {
if fileResult, err := p.ParseFile(f); fileResult != nil && err == nil {
results = append(results, fileResult)
fileResult, err := p.ParseFile(f)
if err != nil {
log.Error().Err(err).Str("file", f).Msg("parse failed")
continue
}
if fileResult == nil {
continue
}
results = append(results, fileResult)

}

return
Expand Down Expand Up @@ -79,9 +86,8 @@ func (p *Parser) parseFile(file *os.File) (*result.FileResults, error) {
for scanner.Scan() {
text := scanner.Text()
for _, r := range p.Rules {
lineResults := result.FindResults(r, text, line)
lineResults := result.FindResults(r, results.Filename, text, line)
results.Results = append(results.Results, lineResults...)
// results.Push(lineResults...)
}
line++
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package printer

import "github.com/caitlinelfring/woke/pkg/result"

type Printer interface {
Print(*result.FileResults) error
}
35 changes: 35 additions & 0 deletions pkg/printer/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package printer

import (
"fmt"

"github.com/caitlinelfring/woke/pkg/result"
)

// Simple
type Simple struct{}

func NewSimple() *Simple {
return &Simple{}
}

// Print prints in the format 'filename:line:column: message'
func (p *Simple) Print(fs *result.FileResults) error {
var err error
if _, err = fmt.Println(fs.Filename); err != nil {
return err
}

for _, r := range fs.Results {
out := fmt.Sprintf("%s:%d: [%s] %s",
r.Filename,
r.StartPosition.Line,
r.Rule.Severity,
r.Reason())

if _, err = fmt.Print(out); err != nil {
return err
}
}
return nil
}
31 changes: 31 additions & 0 deletions pkg/printer/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package printer

import (
"fmt"
"os"

"github.com/caitlinelfring/woke/pkg/result"
)

type Text struct{}

func NewText() *Text {
return &Text{}
}

func (t *Text) Print(fs *result.FileResults) error {
var err error
if _, err = fmt.Fprintln(os.Stdout, fs.Filename); err != nil {
return err
}
for _, r := range fs.Results {
pos := fmt.Sprintf("%s-%s",
r.StartPosition.String(),
r.EndPosition.String())

if _, err = fmt.Fprintf(os.Stdout, " %-14s %-10s %s\n", pos, r.Rule.Severity, r.Reason()); err != nil {
return err
}
}
return nil
}
8 changes: 5 additions & 3 deletions pkg/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ import (
type Result struct {
Rule *rule.Rule
Match string
Filename string
StartPosition *token.Position
EndPosition *token.Position
}

// FindResults returns the results that match the rule for the given text.
// filename and line are only used for the Position
func FindResults(r *rule.Rule, text string, line int) (rs []Result) {
func FindResults(r *rule.Rule, filename, text string, line int) (rs []Result) {
idxs := r.Regexp.FindAllStringIndex(text, -1)

for _, idx := range idxs {
start := idx[0]
end := idx[1]
newResult := Result{
Rule: r,
Match: text[start:end],
Rule: r,
Match: text[start:end],
Filename: filename,
StartPosition: &token.Position{
Line: line,
Column: start,
Expand Down

0 comments on commit 7506201

Please sign in to comment.