Skip to content

Commit

Permalink
lint: max_line_length supports utf-8
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <[email protected]>
  • Loading branch information
greut committed Nov 22, 2019
1 parent 0a58a4e commit 7b0b0cb
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ go test:
eclint:
stage: lint
script:
- go build .
- go build -o eclint cmd/eclint/main.go
- ./eclint -exclude "testdata/**/*"

golangci-lint:
Expand Down
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ before:
builds:
- id: eclint
binary: eclint
main: ./cmd/eclint/main.go
goos:
- linux
- darwin
Expand Down
99 changes: 99 additions & 0 deletions cmd/eclint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"flag"
"fmt"
"os"
"runtime"
"syscall"

"golang.org/x/crypto/ssh/terminal"

"github.com/editorconfig/editorconfig-core-go/v2"
"github.com/mattn/go-colorable"
"gitlab.com/greut/eclint"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
)

var (
version = "dev"
)

func main() { //nolint:funlen
flagVersion := false
log := klogr.New()
opt := eclint.Option{
Stdout: os.Stdout,
ShowErrorQuantity: 10,
IsTerminal: terminal.IsTerminal(syscall.Stdout),
Log: log,
}

if runtime.GOOS == "windows" {
opt.Stdout = colorable.NewColorableStdout()
}

// Flags
klog.InitFlags(nil)
flag.BoolVar(&flagVersion, "version", false, "print the version number")
flag.BoolVar(&opt.NoColors, "no_colors", false, "enable or disable colors")
flag.BoolVar(&opt.Summary, "summary", false, "enable the summary view")
flag.BoolVar(
&opt.ShowAllErrors,
"show_all_errors",
false,
fmt.Sprintf("display all errors for each file (otherwise %d are kept)", opt.ShowErrorQuantity),
)
flag.StringVar(&opt.Exclude, "exclude", "", "paths to exclude")
flag.Parse()

if flagVersion {
fmt.Fprintf(opt.Stdout, "eclint %s\n", version)
return
}

args := flag.Args()
files, err := eclint.ListFiles(log, args...)
if err != nil {
log.Error(err, "error while handling the arguments")
flag.Usage()
os.Exit(1)
return
}

log.V(1).Info("files", "count", len(files), "exclude", opt.Exclude)

if opt.Summary {
opt.ShowAllErrors = true
opt.ShowErrorQuantity = int(^uint(0) >> 1)
}

c := 0
for _, filename := range files {
// Skip excluded files
if opt.Exclude != "" {
ok, err := editorconfig.FnmatchCase(opt.Exclude, filename)
if err != nil {
log.Error(err, "exclude pattern failure", "exclude", opt.Exclude)
c++
break
}
if ok {
continue
}
}

errs := eclint.Lint(filename, opt.Log)
c += len(errs)
err := eclint.PrintErrors(opt, filename, errs)
if err != nil {
log.Error(err, "print errors failure", "filename", filename)
}
}

if c > 0 {
opt.Log.V(1).Info("Some errors were found.", "count", c)
os.Exit(1)
}
}
6 changes: 3 additions & 3 deletions files.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package eclint

import (
"bytes"
Expand All @@ -9,15 +9,15 @@ import (
"github.com/go-logr/logr"
)

// listFiles returns the list of files based on the input.
// ListFiles returns the list of files based on the input.
//
// When its empty, it relies on `git ls-files` first, which
// whould fail if `git` is not present or the current working
// directory is not managed by it. In that case, it work the
// current working directory.
//
// When args are given, it recursively walks into them.
func listFiles(log logr.Logger, args ...string) ([]string, error) {
func ListFiles(log logr.Logger, args ...string) ([]string, error) {
if len(args) == 0 {
fs, err := gitLsFiles(log, ".")
if err == nil {
Expand Down
8 changes: 4 additions & 4 deletions files_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package eclint

import (
"fmt"
Expand All @@ -17,7 +17,7 @@ const (
func TestListFiles(t *testing.T) {
l := tlogr.TestLogger{}
d := testdataSimple
fs, err := listFiles(l, d)
fs, err := ListFiles(l, d)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestListFilesNoArgs(t *testing.T) {
t.Fatal(err)
}

fs, err := listFiles(l)
fs, err := ListFiles(l)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestListFilesNoGit(t *testing.T) {
t.Fatal(err)
}

fs, err := listFiles(l)
fs, err := ListFiles(l)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions lint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package eclint

import (
"bytes"
Expand Down Expand Up @@ -144,7 +144,7 @@ func validate(r io.Reader, log logr.Logger, def *editorconfig.Definition) []erro
}
}
}
err = maxLineLength(maxLength, tabWidth, d)
err = MaxLineLength(maxLength, tabWidth, d)
}

// Enrich the error with the line number
Expand Down Expand Up @@ -233,7 +233,8 @@ func overrideUsingPrefix(def *editorconfig.Definition, prefix string) error {
return nil
}

func lint(filename string, log logr.Logger) []error {
// Lint does the hard work of validating the given file.
func Lint(filename string, log logr.Logger) []error {
// XXX editorconfig should be able to treat a flux of
// filenames with caching capabilities.
def, err := editorconfig.GetDefinitionForFilename(filename)
Expand Down
8 changes: 4 additions & 4 deletions lint_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package eclint

import (
"bytes"
Expand Down Expand Up @@ -88,7 +88,7 @@ without a final newline.`),
func TestLintSimple(t *testing.T) {
l := tlogr.TestLogger{}

for _, err := range lint("testdata/simple/simple.txt", l) {
for _, err := range Lint("testdata/simple/simple.txt", l) {
if err != nil {
t.Errorf("no errors where expected, got %s", err)
}
Expand All @@ -98,7 +98,7 @@ func TestLintSimple(t *testing.T) {
func TestLintMissing(t *testing.T) {
l := tlogr.TestLogger{}

errs := lint("testdata/missing/file", l)
errs := Lint("testdata/missing/file", l)
if len(errs) == 0 {
t.Error("an error was expected, got none")
}
Expand All @@ -113,7 +113,7 @@ func TestLintMissing(t *testing.T) {
func TestLintInvalid(t *testing.T) {
l := tlogr.TestLogger{}

errs := lint("testdata/invalid/.editorconfig", l)
errs := Lint("testdata/invalid/.editorconfig", l)
if len(errs) == 0 {
t.Error("an error was expected, got none")
}
Expand Down
100 changes: 0 additions & 100 deletions main.go

This file was deleted.

19 changes: 19 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package eclint

import (
"io"

"github.com/go-logr/logr"
)

// Option contains the environment of the program.
type Option struct {
IsTerminal bool
NoColors bool
ShowAllErrors bool
Summary bool
ShowErrorQuantity int
Exclude string
Log logr.Logger
Stdout io.Writer
}
Loading

0 comments on commit 7b0b0cb

Please sign in to comment.