-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'make-it-a-lib' into 'master'
Make it a lib See merge request greut/eclint!9
- Loading branch information
Showing
13 changed files
with
169 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ before: | |
builds: | ||
- id: eclint | ||
binary: eclint | ||
main: ./cmd/eclint/main.go | ||
goos: | ||
- linux | ||
- darwin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.