github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/golinters/goheader.go (about)

     1  package golinters
     2  
     3  import (
     4  	"go/token"
     5  	"sync"
     6  
     7  	goheader "github.com/denis-tingaikin/go-header"
     8  	"golang.org/x/tools/go/analysis"
     9  
    10  	"github.com/golangci/golangci-lint/pkg/config"
    11  	"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
    12  	"github.com/golangci/golangci-lint/pkg/lint/linter"
    13  	"github.com/golangci/golangci-lint/pkg/result"
    14  )
    15  
    16  const goHeaderName = "goheader"
    17  
    18  func NewGoHeader(settings *config.GoHeaderSettings) *goanalysis.Linter {
    19  	var mu sync.Mutex
    20  	var resIssues []goanalysis.Issue
    21  
    22  	conf := &goheader.Configuration{}
    23  	if settings != nil {
    24  		conf = &goheader.Configuration{
    25  			Values:       settings.Values,
    26  			Template:     settings.Template,
    27  			TemplatePath: settings.TemplatePath,
    28  		}
    29  	}
    30  
    31  	analyzer := &analysis.Analyzer{
    32  		Name: goHeaderName,
    33  		Doc:  goanalysis.TheOnlyanalyzerDoc,
    34  		Run: func(pass *analysis.Pass) (interface{}, error) {
    35  			issues, err := runGoHeader(pass, conf)
    36  			if err != nil {
    37  				return nil, err
    38  			}
    39  
    40  			if len(issues) == 0 {
    41  				return nil, nil
    42  			}
    43  
    44  			mu.Lock()
    45  			resIssues = append(resIssues, issues...)
    46  			mu.Unlock()
    47  
    48  			return nil, nil
    49  		},
    50  	}
    51  
    52  	return goanalysis.NewLinter(
    53  		goHeaderName,
    54  		"Checks is file header matches to pattern",
    55  		[]*analysis.Analyzer{analyzer},
    56  		nil,
    57  	).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
    58  		return resIssues
    59  	}).WithLoadMode(goanalysis.LoadModeSyntax)
    60  }
    61  
    62  func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) ([]goanalysis.Issue, error) {
    63  	if conf.TemplatePath == "" && conf.Template == "" {
    64  		// User did not pass template, so then do not run go-header linter
    65  		return nil, nil
    66  	}
    67  
    68  	template, err := conf.GetTemplate()
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	values, err := conf.GetValues()
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))
    79  
    80  	var issues []goanalysis.Issue
    81  	for _, file := range pass.Files {
    82  		path := pass.Fset.Position(file.Pos()).Filename
    83  
    84  		i := a.Analyze(&goheader.Target{File: file, Path: path})
    85  
    86  		if i == nil {
    87  			continue
    88  		}
    89  
    90  		issue := result.Issue{
    91  			Pos: token.Position{
    92  				Line:     i.Location().Line + 1,
    93  				Column:   i.Location().Position,
    94  				Filename: path,
    95  			},
    96  			Text:       i.Message(),
    97  			FromLinter: goHeaderName,
    98  		}
    99  
   100  		issues = append(issues, goanalysis.NewIssue(&issue, pass))
   101  	}
   102  
   103  	return issues, nil
   104  }