Skip to content

Commit

Permalink
chore: fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
omissis committed Sep 10, 2022
1 parent 88d2cdf commit 4c67f39
Show file tree
Hide file tree
Showing 51 changed files with 559 additions and 241 deletions.
21 changes: 20 additions & 1 deletion .rules/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ linters:
# Disable specific linter
# https://golangci-lint.run/usage/linters/#disabled-by-default-linters--e--enable
disable:
# todo
- exhaustruct
- forbidigo
- godox
- ireturn
- revive
- varnamelen
# deprecated
- deadcode
- exhaustivestruct
Expand All @@ -45,6 +52,13 @@ linters-settings:
exhaustive:
check-generated: true
default-signifies-exhaustive: true
gci:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/omissis) # Custom section: groups all imports with the specified Prefix.
skip-generated: true
custom-order: true
godot:
scope: all
exclude:
Expand Down Expand Up @@ -253,7 +267,7 @@ linters-settings:
- name: line-length-limit
severity: warning
disabled: false
arguments: [80]
arguments: [120]
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#max-public-structs
- name: max-public-structs
severity: warning
Expand Down Expand Up @@ -432,11 +446,16 @@ issues:
- dupl
- errcheck
- forcetypeassert
- funlen
- goconst
- gocyclo
- goerr113
- gosec
- lll
- maintidx
- path: main\.go
linters:
- gochecknoglobals

# Independently of option `exclude` we use default exclude patterns,
# it can be disabled by this option.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ gci:
@find . -name "*.go" -type f -not -path '*/vendor/*' \
| sed 's/^\.\///g' \
| xargs -I {} sh -c 'echo "formatting imports for {}.." && \
gci write --skip-generated -s standard -s default -s "prefix(github.com/omissis)" {}'
gci write --skip-generated -s standard,default,"prefix(github.com/omissis)" {}'

.PHONY: lint lint-go

Expand Down
13 changes: 7 additions & 6 deletions cmd/common.go → cmd/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cmdutil

import (
"errors"
Expand All @@ -12,11 +12,12 @@ import (
)

var (
ErrNoOutputFormat = errors.New("output cannot be nil")
ErrNoConfigFileFound = errors.New("no config files found")
ErrNoOutputFormat = errors.New("output cannot be nil")
ErrNoConfigFileFound = errors.New("no config files found")
ErrUnknownOutputFormat = errors.New("unknown output format, supported ones are: json, text")
)

func getWd() string {
func GetWd() string {
cwd, err := os.Getwd()
if err != nil {
logx.Fatal(err)
Expand All @@ -25,7 +26,7 @@ func getWd() string {
return cwd
}

func listConfigFiles(cfs []string) []string {
func ListConfigFiles(cfs []string) []string {
configFiles := make([]string, 0)

for _, cf := range cfs {
Expand Down Expand Up @@ -64,7 +65,7 @@ func visitConfigFolder(files *[]string) filepath.WalkFunc {
}
}

func loadConfig[T any](file string) T {
func LoadConfig[T any](file string) T {
var conf T

configData, err := os.ReadFile(file)
Expand Down
16 changes: 10 additions & 6 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cmd

import (
"fmt"
"path/filepath"

"github.com/spf13/cobra"

"github.com/omissis/goarkitect/cmd/cmdutil"
"github.com/omissis/goarkitect/cmd/validate"
"github.com/omissis/goarkitect/internal/schema/santhosh"
)
Expand All @@ -15,27 +17,27 @@ func NewValidateCommand(output *string) *cobra.Command {
Short: "Validate the configuration file(s)",
RunE: func(_ *cobra.Command, args []string) error {
if output == nil {
return ErrNoOutputFormat
return cmdutil.ErrNoOutputFormat
}

basePath := getWd()
basePath := cmdutil.GetWd()
schema, err := santhosh.LoadSchema(basePath)
if err != nil {
return err
return fmt.Errorf("failed to load schema: %w", err)
}

if len(args) == 0 {
args = append(args, filepath.Join(basePath, ".goarkitect.yaml"))
}

cfs := listConfigFiles(args)
cfs := cmdutil.ListConfigFiles(args)
if len(cfs) == 0 {
return ErrNoConfigFileFound
return cmdutil.ErrNoConfigFileFound
}

hasErrors := error(nil)
for _, cf := range cfs {
conf := loadConfig[any](cf)
conf := cmdutil.LoadConfig[any](cf)

if err := schema.ValidateInterface(conf); err != nil {
validate.PrintResults(*output, err, conf, cf)
Expand All @@ -44,6 +46,8 @@ func NewValidateCommand(output *string) *cobra.Command {
}
}

validate.PrintSummary(*output, hasErrors != nil)

return hasErrors
},
}
Expand Down
105 changes: 69 additions & 36 deletions cmd/validate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,90 @@ import (
"errors"
"fmt"

"github.com/omissis/goarkitect/cmd/cmdutil"
"github.com/omissis/goarkitect/internal/jsonx"
"github.com/omissis/goarkitect/internal/logx"
"github.com/omissis/goarkitect/internal/schema/santhosh"
)

var ErrHasValidationErrors = errors.New("schema has validation errors")

func PrintResults(output string, err error, conf any, configFile string) {
ptrPaths := santhosh.GetPtrPaths(err)
func PrintSummary(output string, hasErrors bool) {

switch output {
case "text":
// TODO: improve formatting
fmt.Printf("CONFIG FILE %s\n", configFile)

for _, path := range ptrPaths {
value, serr := santhosh.GetValueAtPath(conf, path)
if serr != nil {
logx.Fatal(serr)
}

// TODO: improve santhosh.JoinPtrPath output
fmt.Printf(
"path '%s' contains an invalid configuration value: %+v\n",
santhosh.JoinPtrPath(path),
value,
)
if hasErrors {
fmt.Println("Validation failed")
} else {
fmt.Println("Validation succeeded")
}

fmt.Println(err)
case "json":
for _, path := range ptrPaths {
value, serr := santhosh.GetValueAtPath(conf, path)
if serr != nil {
logx.Fatal(serr)
}

fmt.Println(
jsonx.Marshal(
map[string]any{
"file": configFile,
"message": "path contains an invalid configuration value",
"path": santhosh.JoinPtrPath(path),
"value": value,
},
),
)
if hasErrors {
fmt.Println("{\"result\":\"Validation failed\"}")
} else {
fmt.Println("{\"result\":\"Validation succeeded\"}")
}

fmt.Println(jsonx.Marshal(err))
default:
logx.Fatal(fmt.Errorf("unknown output format: '%s', supported ones are: json, text", output))
logx.Fatal(fmt.Errorf("'%s': %w", output, cmdutil.ErrUnknownOutputFormat))
}
}

func PrintResults(output string, err error, conf any, configFile string) {
ptrPaths := santhosh.GetPtrPaths(err)

switch output {
case "text":
printTextResults(ptrPaths, err, conf, configFile)

case "json":
printJSONResults(ptrPaths, err, conf, configFile)

default:
logx.Fatal(fmt.Errorf("'%s': %w", output, cmdutil.ErrUnknownOutputFormat))
}
}

func printTextResults(ptrPaths [][]any, err error, conf any, configFile string) {
// TODO: improve formatting.
fmt.Printf("CONFIG FILE %s\n", configFile)

for _, path := range ptrPaths {
value, serr := santhosh.GetValueAtPath(conf, path)
if serr != nil {
logx.Fatal(serr)
}

// TODO: improve santhosh.JoinPtrPath output.
fmt.Printf(
"path '%s' contains an invalid configuration value: %+v\n",
santhosh.JoinPtrPath(path),
value,
)
}

fmt.Println(err)
}

func printJSONResults(ptrPaths [][]any, err error, conf any, configFile string) {
for _, path := range ptrPaths {
value, serr := santhosh.GetValueAtPath(conf, path)
if serr != nil {
logx.Fatal(serr)
}

fmt.Println(
jsonx.Marshal(
map[string]any{
"file": configFile,
"message": "path contains an invalid configuration value",
"path": santhosh.JoinPtrPath(path),
"value": value,
},
),
)
}

fmt.Println(jsonx.Marshal(err))
}
11 changes: 6 additions & 5 deletions cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/omissis/goarkitect/cmd/cmdutil"
"github.com/omissis/goarkitect/cmd/verify"
"github.com/omissis/goarkitect/internal/config"
)
Expand All @@ -15,21 +16,21 @@ func NewVerifyCommand(output *string) *cobra.Command {
Short: "Verify the ruleset against a project",
RunE: func(_ *cobra.Command, args []string) error {
if output == nil {
return ErrNoOutputFormat
return cmdutil.ErrNoOutputFormat
}

if len(args) == 0 {
args = append(args, filepath.Join(getWd(), ".goarkitect.yaml"))
args = append(args, filepath.Join(cmdutil.GetWd(), ".goarkitect.yaml"))
}

cfs := listConfigFiles(args)
cfs := cmdutil.ListConfigFiles(args)
if len(cfs) == 0 {
return ErrNoConfigFileFound
return cmdutil.ErrNoConfigFileFound
}

hasErrors := error(nil)
for _, cf := range cfs {
conf := loadConfig[config.Root](cf)
conf := cmdutil.LoadConfig[config.Root](cf)

results := config.Execute(conf)

Expand Down
11 changes: 9 additions & 2 deletions cmd/verify/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"

"github.com/omissis/goarkitect/cmd/cmdutil"
"github.com/omissis/goarkitect/internal/arch/rule"
"github.com/omissis/goarkitect/internal/config"
"github.com/omissis/goarkitect/internal/jsonx"
Expand All @@ -15,28 +16,33 @@ var ErrProjectDoesNotRespectRules = errors.New("project does not respect defined
func PrintResults(output, configFile string, results []config.RuleExecutionResult) {
switch output {
case "text":
// TODO: improve formatting
// TODO: improve formatting.
fmt.Printf("CONFIG FILE %s\n", configFile)

for _, r := range results {
fmt.Printf("\nRULE '%s'\n", r.RuleName)

fmt.Printf("Violations:\n")

for _, v := range r.Violations {
fmt.Printf("- %s\n", v)
}

if len(r.Violations) == 0 {
fmt.Printf("- None\n")
}

fmt.Printf("Errors:\n")

for _, v := range r.Errors {
fmt.Printf("- %s\n", v)
}

if len(r.Errors) == 0 {
fmt.Printf("- None\n")
}
}

case "json":
fmt.Println(
jsonx.Marshal(
Expand All @@ -46,8 +52,9 @@ func PrintResults(output, configFile string, results []config.RuleExecutionResul
},
),
)

default:
logx.Fatal(fmt.Errorf("unknown output format: '%s', supported ones are: json, text", output))
logx.Fatal(fmt.Errorf("'%s': %w", output, cmdutil.ErrUnknownOutputFormat))
}
}

Expand Down
Loading

0 comments on commit 4c67f39

Please sign in to comment.