Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

887 integrate lint in flow #891

Merged
merged 15 commits into from
Sep 26, 2024
Merged
2 changes: 1 addition & 1 deletion cmd/analyzer/subcmds/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func lintVPCConfigs(cmd *cobra.Command, args *inArgs) error {
return err1
}
// potential errors already handled
_, _, err2 := linter.LinterExecute(multiConfigs.Configs(), args.printAllLinters,
_, err2 := linter.LinterExecute(multiConfigs.Configs(), args.printAllLinters,
args.enableLinters, args.disableLinters)
return err2
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commonvpc/testfunc/lint_test_functionality.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (tt *VpcLintTest) TestSingleLint(t *testing.T, rc commonvpc.ResourcesContai
func (tt *VpcLintTest) runLintTest(t *testing.T, cConfigs map[string]*vpcmodel.VPCConfig, outDir string) error {
// output use case is not significant here, but being used so that lint test can rely on existing mechanism
tt.initLintTestFileNames(outDir)
_, actualOutput, _ := linter.LinterExecute(cConfigs, tt.PrintAllLints, tt.Enable, tt.Disable)
actualOutput, _ := linter.LinterExecute(cConfigs, tt.PrintAllLints, tt.Enable, tt.Disable)
if err := compareOrRegenerateOutputPerTest(t, tt.Mode, actualOutput, lintOut, tt.Name, tt.ExpectedOutput,
vpcmodel.AllEndpoints); err != nil {
return err
Expand Down
40 changes: 27 additions & 13 deletions pkg/linter/linterExecute.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func IsValidLintersNames(name string) bool {
_, ok := linterGenerators[name]
return ok
}
func generateLinters(configs map[string]*vpcmodel.VPCConfig, nodeConn map[string]*vpcmodel.VPCConnectivity) []linter {
func generateLinters(configs map[string]*vpcmodel.VPCConfig, nodeConn map[string]*vpcmodel.VPCConnectivity) Linters {
res := make([]linter, len(linterGenerators))
i := 0
for name, generator := range linterGenerators {
Expand All @@ -65,17 +65,27 @@ func computeConnectivity(configs map[string]*vpcmodel.VPCConfig) (map[string]*vp
return nodesConn, nil
}

// //////////////////////////////////////////////////////////////////////////////////////////////
// LinterExecute executes linters one by one
// LinterExecute performs the lint analysis and then prints the string result; should be redundant once lint is
// integrated in the general flow
func LinterExecute(configs map[string]*vpcmodel.VPCConfig, printAllFindings bool,
enableList, disableList []string) (issueFound bool, resString string, err error) {
enableList, disableList []string) (resString string, err error) {
linters, err := linterAnalysis(configs, enableList, disableList)
if err != nil {
return "", err
}
resString = linters.String(printAllFindings)
fmt.Println(resString)
zivnevo marked this conversation as resolved.
Show resolved Hide resolved
return resString, nil
}

// linterAnalysis executes linters one by one and collects their results
func linterAnalysis(configs map[string]*vpcmodel.VPCConfig, enableList, disableList []string) (linters Linters, err error) {
nodesConn, err := computeConnectivity(configs)
if err != nil {
return false, "", err
return nil, err
}

linters := generateLinters(configs, nodesConn)
strPerLint := []string{}
linters = generateLinters(configs, nodesConn)
for _, thisLinter := range linters {
name := thisLinter.lintName()
enable := thisLinter.enableByDefault()
Expand All @@ -84,21 +94,25 @@ func LinterExecute(configs map[string]*vpcmodel.VPCConfig, printAllFindings bool
if !enable {
continue
}
thisLintStr := ""
err := thisLinter.check()
if err != nil {
return false, "", err
return nil, err
}
}
return linters, nil
}

func (linters Linters) String(printAllFindings bool) (resString string) {
strPerLint := []string{}
for _, thisLinter := range linters {
lintFindings := thisLinter.getFindings()
if len(lintFindings) > 0 {
issueFound = true
thisLintStr = thisLinter.string(thisLinter.lintDescription(), printAllFindings)
thisLintStr := thisLinter.string(thisLinter.lintDescription(), printAllFindings)
strPerLint = append(strPerLint, thisLintStr)
}
}
sort.Strings(strPerLint)
delimBetweenLints := strings.Repeat("_", delimBetweenLintsChars)
resString = strings.Join(strPerLint, "\n"+delimBetweenLints+"\n\n")
fmt.Println(resString)
return issueFound, resString, nil
return resString
}
2 changes: 2 additions & 0 deletions pkg/linter/linterTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

const numFindingToPrint = 3

type Linters []linter

type linter interface {
check() error
getFindings() []finding // returns all findings detected by the linter
Expand Down