Skip to content

Commit

Permalink
feat: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
loeffel-io committed Sep 13, 2023
1 parent ccef621 commit 5206390
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 34 deletions.
1 change: 1 addition & 0 deletions cmd/ls_lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
deps = [
"//internal/config",
"//internal/debug",
"//internal/flag",
"//internal/linter",
"//internal/rule",
"@in_gopkg_yaml_v3//:yaml_v3",
Expand Down
38 changes: 28 additions & 10 deletions cmd/ls_lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"fmt"
"github.com/loeffel-io/ls-lint/v2/internal/config"
"github.com/loeffel-io/ls-lint/v2/internal/debug"
_flag "github.com/loeffel-io/ls-lint/v2/internal/flag"
"github.com/loeffel-io/ls-lint/v2/internal/linter"
"github.com/loeffel-io/ls-lint/v2/internal/rule"
"gopkg.in/yaml.v3"
"log"
"maps"
"os"
"runtime"
"slices"
"strings"
)

Expand All @@ -21,12 +24,14 @@ func main() {
var exitCode = 0
var writer = os.Stdout
var flags = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
var flagConfig = flags.String("config", ".ls-lint.yml", "ls-lint config file path")
var flagWorkdir = flags.String("workdir", ".", "change working directory before executing the given subcommand")
var flagWarn = flags.Bool("warn", false, "write lint errors to stdout instead of stderr (exit 0)")
var flagDebug = flags.Bool("debug", false, "write debug informations to stdout")
var flagVersion = flags.Bool("version", false, "prints version information for ls-lint")

var flagConfig _flag.Config
flags.Var(&flagConfig, "config", "ls-lint config file paths")

if err = flags.Parse(os.Args[1:]); err != nil {
log.Fatal(err)
}
Expand All @@ -37,17 +42,30 @@ func main() {
}

var filesystem = os.DirFS(*flagWorkdir)
var lslintConfig = config.NewConfig(nil, nil)
var configBytes []byte

// read file
if configBytes, err = os.ReadFile(*flagConfig); err != nil {
log.Fatal(err)
if len(flagConfig) == 0 {
flagConfig = _flag.Config{".ls-lint.yaml"}
}

// to yaml
if err = yaml.Unmarshal(configBytes, lslintConfig); err != nil {
log.Fatal(err)
var lslintConfig = config.NewConfig(make(config.Ls), make([]string, 0))
for _, c := range flagConfig {
var tmpLslintConfig = config.NewConfig(nil, nil)
var tmpConfigBytes []byte

// read file
if tmpConfigBytes, err = os.ReadFile(c); err != nil {
log.Fatal(err)
}

// to yaml
if err = yaml.Unmarshal(tmpConfigBytes, tmpLslintConfig); err != nil {
log.Fatal(err)
}

maps.Copy(lslintConfig.GetLs(), tmpLslintConfig.GetLs())
lslintConfig.Ignore = append(lslintConfig.Ignore, tmpLslintConfig.GetIgnore()...)
slices.Sort(lslintConfig.Ignore)
lslintConfig.Ignore = slices.Compact(lslintConfig.Ignore)
}

// linter
Expand All @@ -59,7 +77,7 @@ func main() {
)

// runner
if err = lslintLinter.Run(filesystem, *flagDebug, false); err != nil {
if err = lslintLinter.Run(filesystem, *flagDebug); err != nil {
log.Fatal(err)
}

Expand Down
1 change: 1 addition & 0 deletions internal/debug/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Statistic struct {

func NewStatistic() *Statistic {
return &Statistic{
Start: time.Now(),
RWMutex: new(sync.RWMutex),
}
}
Expand Down
8 changes: 8 additions & 0 deletions internal/flag/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "flag",
srcs = ["config.go"],
importpath = "github.com/loeffel-io/ls-lint/v2/internal/flag",
visibility = ["//:__subpackages__"],
)
14 changes: 14 additions & 0 deletions internal/flag/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package flag

import "strings"

type Config []string

func (config *Config) String() string {
return strings.Join(*config, ",")
}

func (config *Config) Set(value string) error {
*config = append(*config, value)
return nil
}
5 changes: 4 additions & 1 deletion internal/glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func Index[IndexValue bool | map[string][]rule.Rule](filesystem fs.FS, index map
continue
}

index[match] = value
if _, ok := index[match]; !ok {
index[match] = value
}

delete(index, key)
}
}
Expand Down
78 changes: 56 additions & 22 deletions internal/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/loeffel-io/ls-lint/v2/internal/rule"
"golang.org/x/sync/errgroup"
"io/fs"
"log"
"path/filepath"
"strings"
"sync"
"time"
)

const (
Expand Down Expand Up @@ -164,7 +164,7 @@ func (linter *Linter) validateFile(index config.RuleIndex, path string) error {
return nil
}

func (linter *Linter) Run(filesystem fs.FS, debug bool, statistics bool) (err error) {
func (linter *Linter) Run(filesystem fs.FS, debug bool) (err error) {
var index config.RuleIndex
var ignoreIndex = linter.config.GetIgnoreIndex()

Expand All @@ -183,26 +183,66 @@ func (linter *Linter) Run(filesystem fs.FS, debug bool, statistics bool) (err er
return err
}

if debug {
fmt.Printf("=============================\nls index\n-----------------------------\n")
for path, pathIndex := range index {
switch path == "" {
case true:
fmt.Printf(".:")
case false:
fmt.Printf("%s:", path)
}

for extension, rules := range pathIndex {
var tmpRules = make([]string, 0)
for _, tmpRule := range rules {
if len(tmpRule.GetParameters()) > 0 {
tmpRules = append(tmpRules, fmt.Sprintf("%s:%s", tmpRule.GetName(), strings.Join(tmpRule.GetParameters(), ",")))
continue
}

tmpRules = append(tmpRules, tmpRule.GetName())
}

fmt.Printf(" %s: %s", extension, strings.Join(tmpRules, ", "))
}
fmt.Printf("\n")
}

fmt.Printf("-----------------------------\nignore index\n-----------------------------\n")
for path := range ignoreIndex {
fmt.Printf("%s\n", path)
}

fmt.Printf("-----------------------------\nlint\n-----------------------------\n")
}

if debug {
defer func() {
fmt.Printf("-----------------------------\nstatistics\n-----------------------------\n")
fmt.Printf("time: %d ms\n", time.Since(linter.GetStatistics().Start).Milliseconds())
fmt.Printf("files: %d\n", linter.GetStatistics().Files)
fmt.Printf("file skips: %d\n", linter.GetStatistics().FileSkips)
fmt.Printf("dirs: %d\n", linter.GetStatistics().Dirs)
fmt.Printf("dir skips: %d\n", linter.GetStatistics().DirSkips)
fmt.Printf("=============================\n")
}()
}

return fs.WalkDir(filesystem, linter.root, func(path string, info fs.DirEntry, err error) error {
if linter.config.ShouldIgnore(ignoreIndex, path) {
if info.IsDir() {
if debug {
log.Printf("skip dir: %s", path)
}

if statistics {
defer linter.GetStatistics().AddDirSkip()
fmt.Printf("skip dir: %s\n", path)
linter.GetStatistics().AddDirSkip()
}

return fs.SkipDir
}

if debug {
log.Printf("skip file: %s", path)
}

if statistics {
defer linter.GetStatistics().AddFileSkip()
fmt.Printf("skip file: %s\n", path)
linter.GetStatistics().AddFileSkip()
}

return nil
Expand All @@ -214,22 +254,16 @@ func (linter *Linter) Run(filesystem fs.FS, debug bool, statistics bool) (err er

if info.IsDir() {
if debug {
log.Printf("lint dir: %s", path)
}

if statistics {
defer linter.GetStatistics().AddDir()
fmt.Printf("lint dir: %s\n", path)
linter.GetStatistics().AddDir()
}

return linter.validateDir(index, path)
}

if debug {
log.Printf("lint file: %s", path)
}

if statistics {
defer linter.GetStatistics().AddFile()
fmt.Printf("lint file: %s\n", path)
linter.GetStatistics().AddFile()
}

return linter.validateFile(index, path)
Expand Down
2 changes: 1 addition & 1 deletion internal/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func TestLinterRun(t *testing.T) {
for _, test := range tests {
fmt.Printf("Run test %d (%s)\n", i, test.description)

var err = test.linter.Run(test.filesystem, true, true)
var err = test.linter.Run(test.filesystem, true)

if !errors.Is(err, test.expectedErr) {
t.Errorf("Test %d (%s) failed with unmatched error value - %v", i, test.description, err)
Expand Down
4 changes: 4 additions & 0 deletions internal/rule/camelcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (rule *CamelCase) SetParameters(params []string) error {
return nil
}

func (rule *CamelCase) GetParameters() []string {
return nil
}

// Validate checks if string is camel case
// false if rune is no letter and no digit
func (rule *CamelCase) Validate(value string) (bool, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/rule/kebabcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (rule *KebabCase) SetParameters(params []string) error {
return nil
}

func (rule *KebabCase) GetParameters() []string {
return nil
}

// Validate checks if string is kebab case
// false if rune is no lowercase letter, digit or -
func (rule *KebabCase) Validate(value string) (bool, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/rule/lowercase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (rule *Lowercase) SetParameters(params []string) error {
return nil
}

func (rule *Lowercase) GetParameters() []string {
return nil
}

// Validate checks if every letter is lower
func (rule *Lowercase) Validate(value string) (bool, error) {
for _, c := range value {
Expand Down
4 changes: 4 additions & 0 deletions internal/rule/pascalcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (rule *PascalCase) SetParameters(params []string) error {
return nil
}

func (rule *PascalCase) GetParameters() []string {
return nil
}

// Validate checks if string is pascal case
// false if rune is no letter and no digit
// false if first rune is not upper
Expand Down
4 changes: 4 additions & 0 deletions internal/rule/pointcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (rule *PointCase) SetParameters(params []string) error {
return nil
}

func (rule *PointCase) GetParameters() []string {
return nil
}

// Validate checks if string is "point case"
// false if rune is no lowercase letter, digit or .
func (rule *PointCase) Validate(value string) (bool, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/rule/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (rule *Regex) SetParameters(params []string) error {
return nil
}

func (rule *Regex) GetParameters() []string {
return []string{rule.RegexPattern}
}

// Validate checks if full string matches regex
func (rule *Regex) Validate(value string) (bool, error) {
return regexp.MatchString(fmt.Sprintf("^%s$", rule.getRegexPattern()), value)
Expand Down
1 change: 1 addition & 0 deletions internal/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Rule interface {
Init() Rule
GetName() string
SetParameters(params []string) error
GetParameters() []string
Validate(value string) (bool, error)
GetErrorMessage() string
}
4 changes: 4 additions & 0 deletions internal/rule/screamingsnakecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (rule *ScreamingSnakeCase) SetParameters(params []string) error {
return nil
}

func (rule *ScreamingSnakeCase) GetParameters() []string {
return nil
}

// Validate checks if string is screaming sneak case
// false if rune is no uppercase letter, digit or _
func (rule *ScreamingSnakeCase) Validate(value string) (bool, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/rule/snakecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (rule *SnakeCase) SetParameters(params []string) error {
return nil
}

func (rule *SnakeCase) GetParameters() []string {
return nil
}

// Validate checks if string is sneak case
// false if rune is no lowercase letter, digit or _
func (rule *SnakeCase) Validate(value string) (bool, error) {
Expand Down
2 changes: 2 additions & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def go_repositories():
sum = "h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc=",
version = "v4.6.0",
)

go_repository(
name = "in_gopkg_check_v1",
importpath = "gopkg.in/check.v1",
Expand All @@ -19,6 +20,7 @@ def go_repositories():
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
version = "v3.0.1",
)

go_repository(
name = "org_golang_x_sync",
importpath = "golang.org/x/sync",
Expand Down

0 comments on commit 5206390

Please sign in to comment.