-
Couldn't load subscription status.
- Fork 10
Release/v1.0.0 #49
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
Merged
Merged
Release/v1.0.0 #49
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9e3e45e
fix issues:
denis-tingaikin 724119f
fix ci
denis-tingaikin 123ab1f
rebase and add 'vars'
denis-tingaikin ebb64bf
fix tests and ci
denis-tingaikin adc22d3
fix self check
denis-tingaikin 0c76dad
add star-block like headers support
denis-tingaikin 8186b8e
add analysis.Analyzer
denis-tingaikin 7a4c9ab
add parallel execution support and plans
denis-tingaikin 167bd46
simplify readme
denis-tingaikin c7b2750
add fixes to be compatible with go tools
denis-tingaikin 764886d
simplify goheader
denis-tingaikin 2ccdb86
add fixes after manual testing
denis-tingaikin fccb61d
fix issues reported on self-check
denis-tingaikin 9f351aa
downgrade to go v1.23.0
denis-tingaikin b24f0bb
apply code review comments
denis-tingaikin cb94816
apply code review comments: refactor analyzer
denis-tingaikin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Copyright (c) 2020-2025 Denis Tingaikin | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at: | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package goheader | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "runtime" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "golang.org/x/tools/go/analysis" | ||
| ) | ||
|
|
||
| // NewAnalyzer creates new analyzer based on template and goheader values | ||
| func NewAnalyzer(c *Config) *analysis.Analyzer { | ||
| var initOncer sync.Once | ||
| var initErr error | ||
| var goheader *Analyzer | ||
|
|
||
| return &analysis.Analyzer{ | ||
| Doc: "the_only_doc", | ||
| URL: "https://github.com/denis-tingaikin/go-header", | ||
| Name: "goheader", | ||
| RunDespiteErrors: true, | ||
| Run: func(p *analysis.Pass) (any, error) { | ||
| initOncer.Do(func() { | ||
| var templ string | ||
| var vals map[string]Value | ||
|
|
||
| templ, initErr = c.GetTemplate() | ||
| if initErr != nil { | ||
| return | ||
| } | ||
|
|
||
| vals, initErr = c.GetValues() | ||
| if initErr != nil { | ||
| return | ||
| } | ||
|
|
||
| goheader = New(WithTemplate(templ), WithValues(vals)) | ||
|
|
||
| }) | ||
| if initErr != nil { | ||
| return nil, initErr | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
|
|
||
| var jobCh = make(chan *ast.File, len(p.Files)) | ||
|
|
||
| for _, f := range p.Files { | ||
| file := f | ||
| jobCh <- file | ||
| } | ||
| close(jobCh) | ||
|
|
||
| for range runtime.NumCPU() { | ||
denis-tingaikin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
|
|
||
| for file := range jobCh { | ||
| filename := p.Fset.Position(file.Pos()).Filename | ||
| if !strings.HasSuffix(filename, ".go") { | ||
| continue | ||
| } | ||
|
|
||
| res := goheader.Analyze(&Target{ | ||
| File: file, | ||
| Path: filename, | ||
| }) | ||
|
|
||
| if res.Err == nil { | ||
| continue | ||
| } | ||
| var line = 1 | ||
| if hasCGOImport(file) { | ||
| line = 4 | ||
| } | ||
|
|
||
| var start = p.Fset.File(file.FileStart).LineStart(line) | ||
| var end = res.End - res.Start + start | ||
| var endLine = p.Fset.File(file.FileStart).Line(end) + 1 | ||
| end = p.Fset.File(file.FileStart).LineStart(endLine) | ||
|
|
||
| diag := analysis.Diagnostic{ | ||
| Pos: start, | ||
| End: end, | ||
| Message: res.Err.Error(), | ||
| } | ||
|
|
||
| if res.Fix != "" { | ||
| diag.SuggestedFixes = []analysis.SuggestedFix{{ | ||
| TextEdits: []analysis.TextEdit{{ | ||
| Pos: start, | ||
| End: end, | ||
| NewText: []byte(res.Fix + "\n"), | ||
| }}, | ||
| }} | ||
| } | ||
| p.Report(diag) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
| return nil, nil | ||
| }, | ||
| } | ||
| } | ||
| func hasCGOImport(file *ast.File) bool { | ||
| var unsafeCount int | ||
| for _, imp := range file.Imports { | ||
| if imp.Path.Value == `"C"` { | ||
| return true | ||
| } | ||
| if imp.Path.Value == `"unsafe"` { | ||
| unsafeCount++ | ||
| } | ||
| } | ||
| return unsafeCount > 1 | ||
| } | ||
|
|
||
| // NewAnalyzerFromConfigPath creates a new analysis.Analyzer from goheader config file | ||
| func NewAnalyzerFromConfigPath(config *string) *analysis.Analyzer { | ||
| var goheaderOncer sync.Once | ||
| var goheader *analysis.Analyzer | ||
|
|
||
| return &analysis.Analyzer{ | ||
| Doc: "the_only_doc", | ||
| URL: "https://github.com/denis-tingaikin/go-header", | ||
| Name: "goheader", | ||
| RunDespiteErrors: true, | ||
| Run: func(p *analysis.Pass) (any, error) { | ||
| var err error | ||
| goheaderOncer.Do(func() { | ||
| var cfg Config | ||
| if err = cfg.Parse(*config); err != nil { | ||
| return | ||
| } | ||
| goheader = NewAnalyzer(&cfg) | ||
| }) | ||
denis-tingaikin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return goheader.Run(p) | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.