Skip to content

Commit

Permalink
fix: [#6] Add logrus logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-bvanb committed Nov 2, 2024
1 parent 1e350ba commit 55eeae8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ linters-settings:
deny:
- pkg: "github.com/tj/assert"
desc: not allowed, did you mean testify assert?

- pkg: "log"
desc: Use 'log "github.com/sirupsen/logrus"' instead
errcheck:
check-type-assertions: false
gci:
Expand Down
2 changes: 2 additions & 0 deletions .prolayout.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
module: "github.com/wimspaargaren/prolayout"
root:
- name: ""
- name: ".test"
- name: "bar"
- name: "internal"
- name: "tests"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ root:
- name: ".*_test.go"
```
and run `prolayout ./...`
and run `prolayout ./...`. It is possible to change the loggerLevel by
specifying `--loggerLevel info`.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/wimspaargaren/prolayout
go 1.23.0

require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
golang.org/x/tools v0.20.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -13,4 +14,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38 changes: 36 additions & 2 deletions internal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"

log "github.com/sirupsen/logrus"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"

Expand All @@ -14,6 +15,8 @@ import (

// New creates a new analyzer for a given root directory.
func New(root model.Root) *analysis.Analyzer {
// log.WithFields(log.Fields{"root": root}).Info("New")

runner := newRunner(root)
return &analysis.Analyzer{
Name: "prolayout",
Expand All @@ -28,10 +31,14 @@ type runner struct {
}

func newRunner(root model.Root) *runner {
// log.WithFields(log.Fields{"root": root}).Info("newRunner")

return &runner{Root: root}
}

func (r *runner) run(pass *analysis.Pass) (any, error) {
// log.WithFields(log.Fields{"pass": pass}).Info("run")

err := r.assess(pass)
if err != nil {
return nil, err
Expand All @@ -41,6 +48,8 @@ func (r *runner) run(pass *analysis.Pass) (any, error) {
}

func (r *runner) assess(pass *analysis.Pass) error {
// log.WithFields(log.Fields{"pass": pass}).Info("assess")

dir, err := r.assessDir(pass)
if err != nil {
return err
Expand All @@ -50,6 +59,8 @@ func (r *runner) assess(pass *analysis.Pass) error {
}

func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) {
// log.WithFields(log.Fields{"pass": pass}).Info("assessDir")

module := r.Root.Module
packagePathWithoutModule := strings.ReplaceAll(pass.Pkg.Path(), module, "")
packagePathWithoutModule = strings.TrimPrefix(packagePathWithoutModule, "/")
Expand All @@ -58,7 +69,13 @@ func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) {
dir := &model.Dir{}

for _, folder := range packageSplittedPerFolder {
if len(dirs) == 0 || strings.HasSuffix(folder, ".test") {
log.WithFields(log.Fields{"folder": folder}).Info("assessDir")
// if folder == ".test" {
// continue
// }

// if len(dirs) == 0 || strings.HasSuffix(folder, ".test") {
if len(dirs) == 0 {
return nil, nil
}

Expand All @@ -67,7 +84,10 @@ func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) {
return nil, err
}
if !ok {
if len(pass.Files) == 0 || packagePathWithoutModule == "" {
// if len(pass.Files) == 0 || packagePathWithoutModule == "" {

// if packagePathWithoutModule == "" then this indicates that there is a main.go in the root
if len(pass.Files) == 0 {
continue
}
pass.ReportRangef(pass.Files[0], "package not allowed: %s, %s not found in allowed names: [%s]", packagePathWithoutModule, folder, strings.Join(dirsNames(dirs), ","))
Expand All @@ -81,6 +101,8 @@ func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) {
}

func dirsNames(dirs []*model.Dir) []string {
// log.WithFields(log.Fields{"dirs": dirs}).Info("dirsNames")

names := make([]string, len(dirs))
for i, d := range dirs {
names[i] = d.Name
Expand All @@ -89,7 +111,11 @@ func dirsNames(dirs []*model.Dir) []string {
}

func (r *runner) assessFiles(pass *analysis.Pass, dir *model.Dir) error {
// log.WithFields(log.Fields{"pass": pass, "dir": dir}).Info("assessFiles")

if dir == nil || len(dir.Files) == 0 {
// log.WithFields(log.Fields{"dir": dir}).Trace("both dir and dir.Files are empty")

return nil
}
for _, file := range pass.Files {
Expand All @@ -105,6 +131,8 @@ func (r *runner) assessFiles(pass *analysis.Pass, dir *model.Dir) error {
}

func (r *runner) matchFiles(files []*model.File, name string) (bool, error) {
// log.WithFields(log.Fields{"files": files, "name": name}).Info("matchFiles")

for _, f := range files {
match, err := regexp.MatchString(f.Name, name+".go")
if err != nil {
Expand All @@ -118,7 +146,11 @@ func (r *runner) matchFiles(files []*model.File, name string) (bool, error) {
}

func matchDir(dir []*model.Dir, name string) (*model.Dir, bool, error) {
// log.WithFields(log.Fields{"dir": dir, "name": name}).Info("matchDir")

for _, d := range dir {
// log.WithFields(log.Fields{"d": d, "dName": d.Name, "name": name}).Info("matchDir")

match, err := regexp.MatchString(d.Name, name)
if err != nil {
return nil, false, errors.ErrInvalidDirNameRegex{DirName: d.Name}
Expand All @@ -131,5 +163,7 @@ func matchDir(dir []*model.Dir, name string) (*model.Dir, bool, error) {
}

func splitPath(path string) []string {
// log.WithFields(log.Fields{"path": path}).Info("splitPath")

return strings.Split(path, "/")
}
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
"golang.org/x/tools/go/analysis/singlechecker"
"gopkg.in/yaml.v3"

Expand All @@ -30,10 +31,27 @@ func readAndUnmarshalProLayoutYML(proLayoutFile string) (*model.Root, error) {
return &t, nil
}

func configureLogging(loggerLevel string) error {
logrusLoggerLevel, err := log.ParseLevel(loggerLevel)
if err != nil {
return fmt.Errorf("unable to parse the logrusLoggerLevel: '%w'", err)
}
log.SetLevel(logrusLoggerLevel)
log.SetReportCaller(true)
return nil
}

func main() {
loggerLevel := flag.String("loggerLevel", "Fatal", "set the loggerLevel to either: Trace, Debug, Info, Warning, Error, Fatal or Panic")
flag.Parse()

if err := configureLogging(*loggerLevel); err != nil {
log.WithError(err).Fatal("could not configure logging")
}

unmarshalledProLayoutYML, err := readAndUnmarshalProLayoutYML(proLayoutFile)
if err != nil {
log.Fatalf("failed to unmarshal '%s': '%v'", proLayoutFile, err)
log.WithError(err).Fatal("failed to unmarshal")
}

singlechecker.Main(analyzer.New(*unmarshalledProLayoutYML))
Expand Down

0 comments on commit 55eeae8

Please sign in to comment.