Skip to content

Commit

Permalink
Emit real golangci-lint verison
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed May 2, 2020
1 parent f15aec7 commit 71ada44
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/gdamore/tcell v1.3.0
github.com/golangci/golangci-lint v1.26.0
github.com/k0kubun/pp v3.0.1+incompatible
github.com/rivo/tview v0.0.0-20200414130344-8e06c826b3a5
github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0
github.com/sirupsen/logrus v1.5.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5/go.mod h1:1FEQNm3xl
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0kubun/pp v1.3.0 h1:r9td75hcmetrcVbmsZRjnxcIbI9mhm+/N6iWyG4TWe0=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
7 changes: 4 additions & 3 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ type App struct {
}

func New(conf *config.Config) (*App, error) {
runner := golangcilint.NewRunner(conf.Executable, []string{})
logger := newLogger(conf)
runner := golangcilint.NewRunner(conf.Executable, []string{}, logger)
return &App{
closers: []io.Closer{},
Config: conf,
Log: newLogger(conf),
Gui: gui.New(runner),
Log: logger,
Gui: gui.New(logger, runner),
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/app/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func newDevelopmentLogger() *logrus.Logger {
log.SetOutput(file)
return log
}

func newProductionLogger() *logrus.Logger {
log := logrus.New()
log.Out = ioutil.Discard
Expand Down
28 changes: 20 additions & 8 deletions pkg/golangcilint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package golangcilint

import (
"encoding/json"
"fmt"
"os/exec"

"github.com/sirupsen/logrus"

"github.com/golangci/golangci-lint/pkg/printers"

"github.com/nakabonne/golintui/pkg/config"
Expand All @@ -21,16 +24,18 @@ type Runner struct {
Config *config.Config

// dir specifies the working directory.
dir string
dir string
logger *logrus.Entry
}

func NewRunner(executable string, args []string) *Runner {
func NewRunner(executable string, args []string, logger *logrus.Entry) *Runner {
// TODO: Automatically read config from golangci settings file.
return &Runner{
Executable: executable,
Args: args,
Config: &config.Config{},
dir: ".",
logger: logger,
}
}

Expand All @@ -39,9 +44,11 @@ func (r *Runner) AddArgs(arg string) {
}

// Run executes `golangci-lint run` with its own args and configuration.
func (r *Runner) Run(arg string) ([]Issue, error) {
outJSON, err := r.execute("run", true, r.Args...)
func (r *Runner) Run() ([]Issue, error) {
outJSON, err := r.execute(append([]string{"run", "--out-format=json", "--issues-exit-code=0"}, r.Args...)...)
if err != nil {
fmt.Println("outJSON:", string(outJSON), "err:", err)
r.logger.Error("failed to run golangci-lint run", outJSON, err)
return nil, err
}

Expand All @@ -57,11 +64,16 @@ func (r *Runner) ListLinters() []Linter {
// And then fetch linters from Report.Linters.
return []Linter{}
}

func (r *Runner) execute(subCommand string, outJSON bool, args ...string) ([]byte, error) {
if outJSON {
args = append(args, "--out-format=json")
func (r *Runner) GetVersion() string {
version, err := r.execute("version")
if err != nil {
r.logger.Error(err)
return ""
}
return string(version)
}

func (r *Runner) execute(args ...string) ([]byte, error) {
cmd := exec.Command(r.Executable, args...)
cmd.Dir = r.dir
return cmd.CombinedOutput()
Expand Down
8 changes: 6 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package gui
import (
"fmt"

"github.com/sirupsen/logrus"

"github.com/rivo/tview"

"github.com/nakabonne/golintui/pkg/golangcilint"
Expand All @@ -20,16 +22,18 @@ type Gui struct {
infoItem *item.Info

runner *golangcilint.Runner
logger *logrus.Entry
}

func New(runner *golangcilint.Runner) *Gui {
func New(logger *logrus.Entry, runner *golangcilint.Runner) *Gui {
return &Gui{
application: tview.NewApplication(),
lintersItem: item.NewLinters(),
sourceFilesItem: item.NewSourceFiles("."),
resultsItem: item.NewResults(),
infoItem: item.NewInfo(),
infoItem: item.NewInfo(runner.GetVersion()),
runner: runner,
logger: logger,
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/gui/item/info.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package item

import "github.com/rivo/tview"
import (
"github.com/rivo/tview"
)

type Info struct {
*tview.TextView
}

func NewInfo() *Info {
func NewInfo(version string) *Info {
b := &Info{
TextView: tview.NewTextView(),
}
// TODO: Populate real version of golangci-lint
b.SetText("golangci-lint version: 1.25.0").
b.SetText(version).
SetBorder(false).
SetTitle("Info").SetTitleAlign(tview.AlignLeft)
return b
Expand Down
11 changes: 10 additions & 1 deletion pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gui

import "github.com/gdamore/tcell"
import (
"github.com/gdamore/tcell"
"github.com/k0kubun/pp"
)

func (g *Gui) setKeybind() {
g.sourceFilesItem.SetKeybinds(g.grobalKeybind, g.registerPath)
Expand All @@ -12,5 +15,11 @@ func (g *Gui) grobalKeybind(event *tcell.EventKey) {
g.application.Stop()
case 'r':
// TODO: Run golangci-lint against the directories marked as selected.
issues, err := g.runner.Run()
if err != nil {
g.logger.Error(err.Error())
return
}
pp.Println(issues)
}
}

0 comments on commit 71ada44

Please sign in to comment.