Skip to content

Commit

Permalink
Make it possible to parse issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed May 2, 2020
1 parent 3d60d66 commit f15aec7
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/gdamore/tcell v1.3.0
github.com/golangci/golangci-lint v1.26.0
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
302 changes: 302 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
// TODO: Populate
appConfig, err := config.New("", "", "", "", "", false)
appConfig, err := config.New("", "", "", "", "", "", false)
if err != nil {
log.Fatal(err.Error())
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/nakabonne/golintui/pkg/config"
"github.com/nakabonne/golintui/pkg/golangcilint"
"github.com/nakabonne/golintui/pkg/gui"
)

Expand All @@ -21,11 +22,12 @@ type App struct {
}

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

Expand Down
11 changes: 10 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

const defaultExecutable = "golangci-lint"

// Config includes the base configuration fields required for golintui.
type Config struct {
Name string
Expand All @@ -8,19 +10,26 @@ type Config struct {
Commit string
BuildDate string
BuildSource string

// Path to a golangci-lint executable.
Executable string
// UserConfig *viper.Viper
// UserConfigDir string
// IsNewRepo bool
}

func New(name, version, commit, date, buildSource string, debuggingFlag bool) (*Config, error) {
func New(name, version, commit, date, buildSource, executable string, debuggingFlag bool) (*Config, error) {
if executable == "" {
executable = defaultExecutable
}
return &Config{
Name: "",
Debug: false,
Version: "",
Commit: "",
BuildDate: "",
BuildSource: "",
Executable: executable,
}, nil
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/golangcilint/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package golangcilint

import (
"github.com/golangci/golangci-lint/pkg/result"
)

// Issue is a wrapper of the issue model represented golangci-lint internally.
type Issue struct {
issue *result.Issue
}

func NewIssues(issues []result.Issue) []Issue {
res := make([]Issue, 0, len(issues))
for _, i := range issues {
res = append(res, Issue{issue: &i})
}
return res
}
6 changes: 6 additions & 0 deletions pkg/golangcilint/linter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package golangcilint

type Linter struct {
Name string
Enabled bool
}
5 changes: 0 additions & 5 deletions pkg/golangcilint/linters/parser.go

This file was deleted.

5 changes: 0 additions & 5 deletions pkg/golangcilint/result/parser.go

This file was deleted.

52 changes: 49 additions & 3 deletions pkg/golangcilint/runner.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
package golangcilint

import "github.com/nakabonne/golintui/pkg/config"
import (
"encoding/json"
"os/exec"

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

"github.com/nakabonne/golintui/pkg/config"
)

const globOperator = "/..."

type Runner struct {
// Path to a golangci-lint executable.
Executable string
// Args given to `golangci-lint run`.
// An arg can be a file name, a dir, and in addition,
// `...` to analyze them recursively.
Args []string
Config *config.Config

// dir specifies the working directory.
dir string
}

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

func (r *Runner) AddArgs(arg string) {
r.Args = append(r.Args, arg+globOperator)
}

// 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...)
if err != nil {
return nil, err
}

var res printers.JSONResult
if err := json.Unmarshal(outJSON, &res); err != nil {
return nil, err
}
return NewIssues(res.Issues), nil
}

func (r *Runner) ListLinters() []Linter {
// TODO: First up, run `golangci-lint run --out-format=json` against safety dir.
// 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")
}
cmd := exec.Command(r.Executable, args...)
cmd.Dir = r.dir
return cmd.CombinedOutput()
}
4 changes: 2 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type Gui struct {
runner *golangcilint.Runner
}

func New() *Gui {
func New(runner *golangcilint.Runner) *Gui {
return &Gui{
application: tview.NewApplication(),
lintersItem: item.NewLinters(),
sourceFilesItem: item.NewSourceFiles("."),
resultsItem: item.NewResults(),
infoItem: item.NewInfo(),
runner: golangcilint.NewRunner([]string{}),
runner: runner,
}
}

Expand Down

0 comments on commit f15aec7

Please sign in to comment.