gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/engine/strategy_gofmt.go (about)

     1  package engine
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/360EntSecGroup-Skylar/goreporter/linters/gofmt"
     7  	"github.com/360EntSecGroup-Skylar/goreporter/utils"
     8  )
     9  
    10  type StrategyGoFmt struct {
    11  	Sync *Synchronizer `inject:""`
    12  }
    13  
    14  func (s *StrategyGoFmt) GetName() string {
    15  	return "GoFmt"
    16  }
    17  
    18  func (s *StrategyGoFmt) GetDescription() string {
    19  	return `go fmt formats Go programs. We run gofmt -s on your code, where -s is for the "simplify" command.`
    20  }
    21  
    22  func (s *StrategyGoFmt) GetWeight() float64 {
    23  	return 0.05
    24  }
    25  
    26  func (s *StrategyGoFmt) Compute(parameters StrategyParameter) (summaries *Summaries) {
    27  	summaries = NewSummaries()
    28  	slicePackagePaths := make([]string, 0)
    29  	for _, packagePath := range parameters.AllDirs {
    30  		slicePackagePaths = append(slicePackagePaths, packagePath)
    31  	}
    32  	lints, err := gofmt.GoFmt(slicePackagePaths)
    33  	if err != nil {
    34  		fmt.Println(err)
    35  	}
    36  	sumProcessNumber := int64(10)
    37  	processUnit := utils.GetProcessUnit(sumProcessNumber, len(lints))
    38  	for _, lintTip := range lints {
    39  		packageName := utils.PackageNameFromGoPath(lintTip)
    40  		erroru := Error{
    41  			LineNumber:  1,
    42  			ErrorString: utils.AbsPath(lintTip) + ":warning: file is not gofmted with -s (gofmt)",
    43  		}
    44  		summaries.Lock()
    45  		if summarie, ok := summaries.Summaries[packageName]; ok {
    46  			summarie.Errors = append(summarie.Errors, erroru)
    47  			summaries.Summaries[packageName] = summarie
    48  		} else {
    49  			summarie := Summary{
    50  				Name:   packageName,
    51  				Errors: make([]Error, 0),
    52  			}
    53  			summarie.Errors = append(summarie.Errors, erroru)
    54  			summaries.Summaries[packageName] = summarie
    55  		}
    56  		summaries.Unlock()
    57  
    58  		if sumProcessNumber > 0 {
    59  			s.Sync.LintersProcessChans <- processUnit
    60  			sumProcessNumber = sumProcessNumber - processUnit
    61  		}
    62  	}
    63  
    64  	return summaries
    65  }
    66  
    67  func (s *StrategyGoFmt) Percentage(summaries *Summaries) float64 {
    68  	summaries.RLock()
    69  	defer summaries.RUnlock()
    70  	return utils.CountPercentage(len(summaries.Summaries))
    71  }