github.com/blaisereilly/goreporter@v0.0.0-20240129165232-a6e9a46234bd/engine/strategy_golint.go (about)

     1  package engine
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/360EntSecGroup-Skylar/goreporter/linters/golint"
     8  	"github.com/360EntSecGroup-Skylar/goreporter/utils"
     9  )
    10  
    11  type StrategyLint struct {
    12  	Sync *Synchronizer `inject:""`
    13  }
    14  
    15  func (s *StrategyLint) GetName() string {
    16  	return "GoLint"
    17  }
    18  
    19  func (s *StrategyLint) GetDescription() string {
    20  	return "All golang code hints that can be optimized and give suggestions for changes."
    21  }
    22  
    23  func (s *StrategyLint) GetWeight() float64 {
    24  	return 0.05
    25  }
    26  
    27  func (s *StrategyLint) Compute(parameters StrategyParameter) (summaries *Summaries) {
    28  	summaries = NewSummaries()
    29  	slicePackagePaths := make([]string, 0)
    30  	for _, packagePath := range parameters.AllDirs {
    31  		slicePackagePaths = append(slicePackagePaths, packagePath)
    32  	}
    33  	lints := golint.GoLinter(slicePackagePaths)
    34  	sumProcessNumber := int64(10)
    35  	processUnit := utils.GetProcessUnit(sumProcessNumber, len(lints))
    36  	for _, lintTip := range lints {
    37  		lintTips := strings.Split(lintTip, ":")
    38  		if len(lintTips) == 4 {
    39  			packageName := utils.PackageNameFromGoPath(lintTips[0])
    40  			line, _ := strconv.Atoi(lintTips[1])
    41  			erroru := Error{
    42  				LineNumber:  line,
    43  				ErrorString: utils.AbsPath(lintTips[0]) + ":" + strings.Join(lintTips[1:], ":"),
    44  			}
    45  			summaries.Lock()
    46  			if summarie, ok := summaries.Summaries[packageName]; ok {
    47  				summarie.Errors = append(summarie.Errors, erroru)
    48  				summaries.Summaries[packageName] = summarie
    49  			} else {
    50  				summarie := Summary{
    51  					Name:   packageName,
    52  					Errors: make([]Error, 0),
    53  				}
    54  				summarie.Errors = append(summarie.Errors, erroru)
    55  				summaries.Summaries[packageName] = summarie
    56  			}
    57  			summaries.Unlock()
    58  		}
    59  		if sumProcessNumber > 0 {
    60  			s.Sync.LintersProcessChans <- processUnit
    61  			sumProcessNumber = sumProcessNumber - processUnit
    62  		}
    63  	}
    64  
    65  	return summaries
    66  }
    67  
    68  func (s *StrategyLint) Percentage(summaries *Summaries) float64 {
    69  	summaries.RLock()
    70  	defer summaries.RUnlock()
    71  	return utils.CountPercentage(len(summaries.Summaries))
    72  }