Skip to content

Commit

Permalink
Analyzer: manage nested dirs & custom log-level
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Bezzubov <bzz@apache.org>
  • Loading branch information
bzz committed Aug 29, 2018
1 parent ab80968 commit b8ab6fe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

A [lookout](https://github.com/src-d/lookout/) analyzer implementation that uses [gometalinter](https://github.com/alecthomas/gometalinter).

It only applies 6 checks from gometalinter that are file-level, and skips dir and package level ones.

_Disclamer: this is not official product, but only serves the purpose of testing the lookout._


Expand Down Expand Up @@ -43,7 +45,7 @@ $ lookout review -v ipv4://localhost:2001 \
| `GOMETALINT_HOST` | `0.0.0.0` | IP address to bind the gRCP serve |
| `GOMETALINT_PORT` | `2001` | Port to bind the gRPC server |
| `GOMETALINT_SERVER_URL` | `ipv4://localhost:10302` | gRPC URL of the [Data service](https://github.com/src-d/lookout/tree/master/docs#components)

| `GOMETALINT_LOG_LEVEL` | `info` | Logging level (info, debug, warning or error) |

# Development
## SDK update
Expand Down
32 changes: 25 additions & 7 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"io/ioutil"
"os"
"path"
"strings"

"github.com/src-d/lookout"
log "gopkg.in/src-d/go-log.v1"
)

const artificialSep = "___.___"

// Analyzer for the lookout
type Analyzer struct {
Version string
DataClient *lookout.DataClient
Expand Down Expand Up @@ -37,19 +41,15 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *lookout.ReviewEvent
return nil, err
}
defer os.RemoveAll(tmp)
log.Debugf("Saving files to '%s'", tmp)

for changes.Next() {
change := changes.Change()
if change.Head == nil {
continue
}

file := path.Join(tmp, path.Base(change.Head.Path))
err = ioutil.WriteFile(file, change.Head.Content, 0644)
if err != nil {
log.Errorf(err, "failed to write a file %s", file)
}
log.Infof("Saved file:'%s'", file)
tryToSaveTo(change.Head, tmp)
}
if changes.Err() != nil {
log.Errorf(changes.Err(), "failed to get a file from DataServer")
Expand All @@ -59,20 +59,38 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *lookout.ReviewEvent
comments := RunGometalinter(withArgs)
var allComments []*lookout.Comment
for _, comment := range comments {
//TrimLeft(, tmp) but \w rel path
file := comment.file[strings.LastIndex(comment.file, tmp)+len(tmp):]
newComment := lookout.Comment{
File: comment.file,
File: strings.TrimLeft(
path.Join(strings.Split(file, artificialSep)...),
string(os.PathSeparator)),
Line: comment.lino,
Text: comment.text,
}
allComments = append(allComments, &newComment)
log.Debugf("Get comment %v", newComment)
}

log.Infof("%d comments created", len(allComments))
return &lookout.EventResponse{
AnalyzerVersion: a.Version,
Comments: allComments,
}, nil
}

// tryToSaveTo saves a file to given dir, preserving it's original path.
// It only loggs any errors and does not fail. All files saved this way will
// be in the root of the same dir.
func tryToSaveTo(file *lookout.File, tmp string) {
nFile := strings.Join(strings.Split(file.Path, string(os.PathSeparator)), artificialSep)
nPath := path.Join(tmp, nFile)
log.Debugf("Saving file '%s', as '%s'", file.Path, nPath)
err := ioutil.WriteFile(nPath, file.Content, 0644)
if err != nil {
log.Errorf(err, "failed to write a file %s", nPath)
}
}
func (a *Analyzer) NotifyPushEvent(ctx context.Context, e *lookout.PushEvent) (*lookout.EventResponse, error) {
return &lookout.EventResponse{}, nil
}
2 changes: 1 addition & 1 deletion cmd/gometalint-analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var usageMessage = fmt.Sprintf(`usage: %s [-version] [OPTIONS]
%s is a lookout analyzer implementation, based on https://github.com/alecthomas/gometalinter.
OPTIONS - any of the supported by gometalinter.
`, name, name)

var (
Expand All @@ -35,6 +34,7 @@ type config struct {
Host string `envconfig:"HOST" default:"0.0.0.0"`
Port int `envconfig:"PORT" default:"2001"`
DataServiceURL string `envconfig:"DATA_SERVICE_URL" default:"ipv4://localhost:10301"`
LogLevel string `envconfig:"LOG_LEVEL" default:"info" description:"Logging level (info, debug, warning or error)"`
}

func main() {
Expand Down
Binary file removed gometalint-analyzer
Binary file not shown.
16 changes: 9 additions & 7 deletions gometalint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ import (

var (
bin = "gometalinter.v2"
DefaultArgs = []string{
defaultArgs = []string{
"--disable-all", "--enable=dupl", "--enable=gas",
"--enable=gofmt", "--enable=goimports", "--enable=lll", "--enable=misspell",
}
)

type comment struct {
// Comment as returned by gometalint
type Comment struct {
level string
file string
lino int32
text string
}

func RunGometalinter(args []string) []comment {
dArgs := append([]string(nil), DefaultArgs...)
// RunGometalinter execs gometalint binary \w pre-configured set of linters
func RunGometalinter(args []string) []Comment {
dArgs := append([]string(nil), defaultArgs...)
args = append(dArgs, args...)
log.Infof("Running '%s %v'\n", bin, args)
out, _ := exec.Command(bin, args...).Output()
out, _ := exec.Command(bin, args...).Output() // nolint: gas
// ignoring err, as it's always not nil if anything found

var comments []comment
var comments []Comment
s := bufio.NewScanner(bytes.NewReader(out))
for s.Scan() { //scan stdout for results
sp := strings.SplitN(s.Text(), ":", 5)
Expand All @@ -42,7 +44,7 @@ func RunGometalinter(args []string) []comment {
}

file, line, _, severity, msg := sp[0], sp[1], sp[2], sp[3], sp[4]
c := comment{
c := Comment{
level: severity,
file: file,
text: msg,
Expand Down

0 comments on commit b8ab6fe

Please sign in to comment.