diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2021-02-22 20:37:25 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2021-02-22 21:02:12 +0100 |
| commit | fcc6d71be2c3ce7d9305c04fc2e87af554571bac (patch) | |
| tree | b01dbb3d1e2988e28ea158d2d543d603ec0b9569 /vendor/github.com/denis-tingajkin | |
| parent | 8f23c528ad5a943b9ffec5dcaf332fd0f614006e (diff) | |
go.mod: update golangci-lint to v1.37
Diffstat (limited to 'vendor/github.com/denis-tingajkin')
10 files changed, 283 insertions, 92 deletions
diff --git a/vendor/github.com/denis-tingajkin/go-header/.go-header.yml b/vendor/github.com/denis-tingajkin/go-header/.go-header.yml new file mode 100644 index 000000000..446d7317e --- /dev/null +++ b/vendor/github.com/denis-tingajkin/go-header/.go-header.yml @@ -0,0 +1,19 @@ +values: + regexp: + copyright-holder: Copyright \(c\) {{year-range}} Denis Tingajkin +template: | + {{copyright-holder}} + + 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.
\ No newline at end of file diff --git a/vendor/github.com/denis-tingajkin/go-header/README.md b/vendor/github.com/denis-tingajkin/go-header/README.md index 43348b8be..1a2a3d9a6 100644 --- a/vendor/github.com/denis-tingajkin/go-header/README.md +++ b/vendor/github.com/denis-tingajkin/go-header/README.md @@ -3,67 +3,79 @@ Go source code linter providing checks for license headers. -# Installation +## Installation For installation you can simply use `go get`. -``` +```bash go get github.com/denis-tingajkin/go-header/cmd/go-header ``` -# Configuration - -To configuring `go-header.yml` linter you simply need to fill the next structures in YAML format. -```go -// Configuration represents go-header linter setup parameters -type Configuration struct { - // Values is map of values. Supports two types 'const` and `regexp`. Values can be used recursively. - Values map[string]map[string]string `yaml:"values"'` - // Template is template for checking. Uses values. - Template string `yaml:"template"` - // TemplatePath path to the template file. Useful if need to load the template from a specific file. - TemplatePath string `yaml:"template-path"` -} +## Configuration + +To configuring `.go-header.yml` linter you simply need to fill the next fields: + +```yaml +--- +temaplte: # expects header template string. +tempalte-path: # expects path to file with license header string. +values: # expects `const` or `regexp` node with values where values is a map string to string. + const: + key1: value1 # const value just checks equality. Note `key1` should be used in template string as {{ key1 }} or {{ KEY1 }}. + regexp: + key2: value2 # regexp value just checks regex match. The value should be a valid regexp pattern. Note `key2` should be used in template string as {{ key2 }} or {{ KEY2 }}. ``` -Where supported two kinds of values: `const` and `regexp`. NOTE: values can be used recursively. -Values with type `const` checks on equality. -Values with type `regexp` checks on the match. -# Execution +Where `values` also can be used recursively. Example: + +```yaml +values: + const: + key1: "value" + regexp: + key2: "{{key1}} value1" # Reads as regex pattern "value value1" +``` + +## Bult-in values + +- **YEAR** - Expects current year. Example header value: `2020`. Example of template using: `{{YEAR}}` or `{{year}}`. +- **YEAR-RANGE** - Expects any valid year interval or current year. Example header value: `2020` or `2000-2020`. Example of template using: `{{year-range}}` or `{{YEAR-RANGE}}`. + +## Execution + +`go-header` linter expects file paths on input. If you want to run `go-header` only on diff files, then you can use this command: -`go-header` linter expects file path on input. If you want to run `go-header` only on diff files, then you can use this command ```bash -go-header $(git diff --name-only) +go-header $(git diff --name-only | grep -E '.*\.go') ``` -# Setup example +## Setup example + +### Step 1 + +Create configuration file `.go-header.yml` in the root of project. -## Step 1 -Create configuration file `.go-header.yaml` in the root of project. ```yaml --- values: const: MY COMPANY: mycompany.com -template-path: ./mypath/mytemplate.txt -``` -## Step 2 -Write the template file. For example for config above `mytemplate.txt` could be -```text -{{ MY COMPANY }} -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. +template: | + {{ MY COMPANY }} + 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. ``` -## Step 3 -You are ready! Execute `go-header {FILES}` from the root of the project. + +### Step 2 +You are ready! Execute `go-header ${PATH_TO_FILES}` from the root of the project. diff --git a/vendor/github.com/denis-tingajkin/go-header/analyzer.go b/vendor/github.com/denis-tingajkin/go-header/analyzer.go index f4efe75a7..5707890b0 100644 --- a/vendor/github.com/denis-tingajkin/go-header/analyzer.go +++ b/vendor/github.com/denis-tingajkin/go-header/analyzer.go @@ -1,30 +1,77 @@ +// Copyright (c) 2020 Denis Tingajkin +// +// 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 ( "fmt" "go/ast" + "os" + "os/exec" "strings" + "time" ) -type Analyzer interface { - Analyze(file *ast.File) Issue +type Target struct { + Path string + File *ast.File +} + +const iso = "2006-01-02 15:04:05 -0700" + +func (t *Target) ModTime() (time.Time, error) { + diff, err := exec.Command("git", "diff", t.Path).CombinedOutput() + if err == nil && len(diff) == 0 { + line, err := exec.Command("git", "log", "-1", "--pretty=format:%cd", "--date=iso", "--", t.Path).CombinedOutput() + if err == nil { + return time.Parse(iso, string(line)) + } + } + info, err := os.Stat(t.Path) + if err != nil { + return time.Time{}, err + } + return info.ModTime(), nil } -type analyzer struct { +type Analyzer struct { values map[string]Value template string } -func (a *analyzer) Analyze(file *ast.File) Issue { +func (a *Analyzer) Analyze(target *Target) Issue { if a.template == "" { return NewIssue("Missed template for check") } + if t, err := target.ModTime(); err == nil { + if t.Year() != time.Now().Year() { + return nil + } + } + file := target.File var header string + var offset = Location{ + Position: 1, + } if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package { if strings.HasPrefix(file.Comments[0].List[0].Text, "/*") { header = (&ast.CommentGroup{List: []*ast.Comment{file.Comments[0].List[0]}}).Text() } else { header = file.Comments[0].Text() + offset.Position += 3 } } header = strings.TrimSpace(header) @@ -32,6 +79,7 @@ func (a *analyzer) Analyze(file *ast.File) Issue { return NewIssue("Missed header for check") } s := NewReader(header) + s.SetOffset(offset) t := NewReader(a.template) for !s.Done() && !t.Done() { templateCh := t.Peek() @@ -69,7 +117,7 @@ func (a *analyzer) Analyze(file *ast.File) Issue { return nil } -func (a *analyzer) readField(reader Reader) string { +func (a *Analyzer) readField(reader *Reader) string { _ = reader.Next() _ = reader.Next() @@ -83,8 +131,8 @@ func (a *analyzer) readField(reader Reader) string { return strings.ToLower(strings.TrimSpace(r)) } -func New(options ...AnalyzerOption) Analyzer { - a := &analyzer{} +func New(options ...Option) *Analyzer { + a := &Analyzer{} for _, o := range options { o.apply(a) } diff --git a/vendor/github.com/denis-tingajkin/go-header/config.go b/vendor/github.com/denis-tingajkin/go-header/config.go index 67e273b9b..fa8b23c2d 100644 --- a/vendor/github.com/denis-tingajkin/go-header/config.go +++ b/vendor/github.com/denis-tingajkin/go-header/config.go @@ -1,11 +1,29 @@ +// Copyright (c) 2020 Denis Tingajkin +// +// 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 ( "errors" "fmt" - "gopkg.in/yaml.v2" "io/ioutil" "strings" + "time" + + "gopkg.in/yaml.v2" ) // Configuration represents go-header linter setup parameters @@ -18,8 +36,20 @@ type Configuration struct { TemplatePath string `yaml:"template-path"` } -func (c *Configuration) GetValues() (map[string]Value, error) { +func (c *Configuration) builtInValues() map[string]Value { var result = make(map[string]Value) + year := fmt.Sprint(time.Now().Year()) + result["year-range"] = &RegexpValue{ + RawValue: strings.ReplaceAll(`(20\d\d\-YEAR)|(YEAR)`, "YEAR", year), + } + result["year"] = &ConstValue{ + RawValue: year, + } + return result +} + +func (c *Configuration) GetValues() (map[string]Value, error) { + var result = c.builtInValues() createConst := func(raw string) Value { return &ConstValue{RawValue: raw} } @@ -55,7 +85,7 @@ func (c *Configuration) GetTemplate() (string, error) { if b, err := ioutil.ReadFile(c.TemplatePath); err != nil { return "", err } else { - c.Template = string(b) + c.Template = strings.TrimSpace(string(b)) return c.Template, nil } } diff --git a/vendor/github.com/denis-tingajkin/go-header/go.mod b/vendor/github.com/denis-tingajkin/go-header/go.mod index c557afeb4..68984cb02 100644 --- a/vendor/github.com/denis-tingajkin/go-header/go.mod +++ b/vendor/github.com/denis-tingajkin/go-header/go.mod @@ -1,6 +1,6 @@ module github.com/denis-tingajkin/go-header -go 1.13 +go 1.15 require ( github.com/fatih/color v1.9.0 diff --git a/vendor/github.com/denis-tingajkin/go-header/issue.go b/vendor/github.com/denis-tingajkin/go-header/issue.go index d4921966a..2ff7bfd3c 100644 --- a/vendor/github.com/denis-tingajkin/go-header/issue.go +++ b/vendor/github.com/denis-tingajkin/go-header/issue.go @@ -1,3 +1,19 @@ +// Copyright (c) 2020 Denis Tingajkin +// +// 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 type Issue interface { diff --git a/vendor/github.com/denis-tingajkin/go-header/location.go b/vendor/github.com/denis-tingajkin/go-header/location.go index fc33e48d8..ba4d1907b 100644 --- a/vendor/github.com/denis-tingajkin/go-header/location.go +++ b/vendor/github.com/denis-tingajkin/go-header/location.go @@ -1,3 +1,19 @@ +// Copyright (c) 2020 Denis Tingajkin +// +// 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 "fmt" @@ -10,3 +26,10 @@ type Location struct { func (l Location) String() string { return fmt.Sprintf("%v:%v", l.Line+1, l.Position) } + +func (l Location) Add(other Location) Location { + return Location{ + Line: l.Line + other.Line, + Position: l.Position + other.Position, + } +} diff --git a/vendor/github.com/denis-tingajkin/go-header/option.go b/vendor/github.com/denis-tingajkin/go-header/option.go index 2adaa9a91..afbcb62e1 100644 --- a/vendor/github.com/denis-tingajkin/go-header/option.go +++ b/vendor/github.com/denis-tingajkin/go-header/option.go @@ -1,19 +1,35 @@ +// Copyright (c) 2020 Denis Tingajkin +// +// 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 "strings" -type AnalyzerOption interface { - apply(*analyzer) +type Option interface { + apply(*Analyzer) } -type applyAnalyzerOptionFunc func(*analyzer) +type applyAnalyzerOptionFunc func(*Analyzer) -func (f applyAnalyzerOptionFunc) apply(a *analyzer) { +func (f applyAnalyzerOptionFunc) apply(a *Analyzer) { f(a) } -func WithValues(values map[string]Value) AnalyzerOption { - return applyAnalyzerOptionFunc(func(a *analyzer) { +func WithValues(values map[string]Value) Option { + return applyAnalyzerOptionFunc(func(a *Analyzer) { a.values = make(map[string]Value) for k, v := range values { a.values[strings.ToLower(k)] = v @@ -21,8 +37,8 @@ func WithValues(values map[string]Value) AnalyzerOption { }) } -func WithTemplate(template string) AnalyzerOption { - return applyAnalyzerOptionFunc(func(a *analyzer) { +func WithTemplate(template string) Option { + return applyAnalyzerOptionFunc(func(a *Analyzer) { a.template = template }) } diff --git a/vendor/github.com/denis-tingajkin/go-header/reader.go b/vendor/github.com/denis-tingajkin/go-header/reader.go index 4386f30da..2393c9488 100644 --- a/vendor/github.com/denis-tingajkin/go-header/reader.go +++ b/vendor/github.com/denis-tingajkin/go-header/reader.go @@ -1,46 +1,57 @@ -package goheader +/* +Copyright (c) 2020 Denis Tingajkin -type Reader interface { - Peek() rune - Next() rune - Done() bool - Finish() string - Position() int - Location() Location - SetPosition(int) - ReadWhile(func(rune) bool) string -} +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 -func NewReader(text string) Reader { - return &reader{source: text} +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 + +func NewReader(text string) *Reader { + return &Reader{source: text} } -type reader struct { +type Reader struct { source string position int location Location + offset Location +} + +func (r *Reader) SetOffset(offset Location) { + r.offset = offset } -func (r *reader) Position() int { +func (r *Reader) Position() int { return r.position } -func (r *reader) Location() Location { - return r.location +func (r *Reader) Location() Location { + return r.location.Add(r.offset) } -func (r *reader) Peek() rune { +func (r *Reader) Peek() rune { if r.Done() { return rune(0) } return rune(r.source[r.position]) } -func (r *reader) Done() bool { +func (r *Reader) Done() bool { return r.position >= len(r.source) } -func (r *reader) Next() rune { +func (r *Reader) Next() rune { if r.Done() { return rune(0) } @@ -55,7 +66,7 @@ func (r *reader) Next() rune { return reuslt } -func (r *reader) Finish() string { +func (r *Reader) Finish() string { if r.position >= len(r.source) { return "" } @@ -63,7 +74,7 @@ func (r *reader) Finish() string { return r.source[r.position:] } -func (r *reader) SetPosition(pos int) { +func (r *Reader) SetPosition(pos int) { if pos < 0 { r.position = 0 } @@ -71,7 +82,7 @@ func (r *reader) SetPosition(pos int) { r.location = r.calculateLocation() } -func (r *reader) ReadWhile(match func(rune) bool) string { +func (r *Reader) ReadWhile(match func(rune) bool) string { if match == nil { return "" } @@ -82,12 +93,12 @@ func (r *reader) ReadWhile(match func(rune) bool) string { return r.source[start:r.position] } -func (r *reader) till() { +func (r *Reader) till() { r.position = len(r.source) r.location = r.calculateLocation() } -func (r *reader) calculateLocation() Location { +func (r *Reader) calculateLocation() Location { min := len(r.source) if min > r.position { min = r.position diff --git a/vendor/github.com/denis-tingajkin/go-header/value.go b/vendor/github.com/denis-tingajkin/go-header/value.go index bebc03b6e..2a3adcdce 100644 --- a/vendor/github.com/denis-tingajkin/go-header/value.go +++ b/vendor/github.com/denis-tingajkin/go-header/value.go @@ -1,3 +1,19 @@ +// Copyright (c) 2020 Denis Tingajkin +// +// 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 ( @@ -14,7 +30,7 @@ type Calculable interface { type Value interface { Calculable - Read(Reader) Issue + Read(*Reader) Issue } func calculateValue(calculable Calculable, values map[string]Value) (string, error) { @@ -61,7 +77,7 @@ func (c *ConstValue) Get() string { return c.RawValue } -func (c *ConstValue) Read(s Reader) Issue { +func (c *ConstValue) Read(s *Reader) Issue { l := s.Location() p := s.Position() for _, ch := range c.Get() { @@ -94,7 +110,7 @@ func (r *RegexpValue) Get() string { return r.RawValue } -func (r *RegexpValue) Read(s Reader) Issue { +func (r *RegexpValue) Read(s *Reader) Issue { l := s.Location() p := regexp.MustCompile(r.Get()) pos := s.Position() |
