Skip to content

Commit 97ef360

Browse files
1. Fix issues #5 and #6
2. Code cleanup Signed-off-by: Denis Tingajkin <[email protected]>
1 parent f9e53eb commit 97ef360

File tree

13 files changed

+288
-70
lines changed

13 files changed

+288
-70
lines changed

.github/workflows/ci.yml

+14-8
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99

10-
- name: Set up Go 1.13
10+
- name: Set up Go 1.15
1111
uses: actions/setup-go@v1
1212
with:
13-
go-version: 1.13
14-
id: go
13+
go-version: 1.15
1514

1615
- name: Check out code into the Go module directory
17-
uses: actions/checkout@v1
16+
uses: actions/checkout@v2
17+
18+
- name: Set go envs
19+
run: |
20+
echo "GOPATH=$(dirname $GITHUB_WORKSPACE)" >> GITHUB_ENV
21+
echo "$(dirname $GITHUB_WORKSPACE)/BIN" >> GITHUB_PATH
1822
1923
- name: Build
20-
env:
21-
GO111MODULE: on
2224
run: go build
2325

2426
- name: Test
25-
env:
26-
GO111MODULE: on
2727
run: go test ./...
28+
29+
- name: Install
30+
run: go install ./...
31+
32+
- name: Self-check
33+
run: go-header $(git ls-files | grep -E '.*\.go')

.go-header.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
values:
2+
regexp:
3+
copyright-holder: Copyright \(c\) {{year-range}} Denis Tingajkin
4+
template: |
5+
{{copyright-holder}}
6+
7+
SPDX-License-Identifier: Apache-2.0
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at:
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.

analyzer.go

+48-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
1+
// Copyright (c) 2020 Denis Tingajkin
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package goheader
218

319
import (
420
"fmt"
521
"go/ast"
22+
"os"
23+
"os/exec"
624
"strings"
25+
"time"
726
)
827

9-
type Analyzer interface {
10-
Analyze(file *ast.File) Issue
28+
type Target struct {
29+
Path string
30+
File *ast.File
31+
}
32+
33+
func (t *Target) ModTime() (time.Time, error) {
34+
diff, err := exec.Command("git", "diff", t.Path).CombinedOutput()
35+
if err == nil && len(diff) == 0 {
36+
line, err := exec.Command("git", "log", "--pretty=format:%cd", "-n", "1", "--date=rfc", "--", t.Path).CombinedOutput()
37+
if err == nil {
38+
return time.Parse(time.RFC1123Z, string(line))
39+
}
40+
}
41+
info, err := os.Stat(t.Path)
42+
if err != nil {
43+
return time.Time{}, err
44+
}
45+
return info.ModTime(), nil
1146
}
1247

13-
type analyzer struct {
48+
type Analyzer struct {
1449
values map[string]Value
1550
template string
1651
}
1752

18-
func (a *analyzer) Analyze(file *ast.File) Issue {
53+
func (a *Analyzer) Analyze(target *Target) Issue {
1954
if a.template == "" {
2055
return NewIssue("Missed template for check")
2156
}
57+
if t, err := target.ModTime(); err == nil {
58+
if t.Year() != time.Now().Year() {
59+
return nil
60+
}
61+
}
62+
file := target.File
2263
var header string
2364
if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package {
2465
if strings.HasPrefix(file.Comments[0].List[0].Text, "/*") {
@@ -69,7 +110,7 @@ func (a *analyzer) Analyze(file *ast.File) Issue {
69110
return nil
70111
}
71112

72-
func (a *analyzer) readField(reader Reader) string {
113+
func (a *Analyzer) readField(reader *Reader) string {
73114
_ = reader.Next()
74115
_ = reader.Next()
75116

@@ -83,8 +124,8 @@ func (a *analyzer) readField(reader Reader) string {
83124
return strings.ToLower(strings.TrimSpace(r))
84125
}
85126

86-
func New(options ...AnalyzerOption) Analyzer {
87-
a := &analyzer{}
127+
func New(options ...Option) *Analyzer {
128+
a := &Analyzer{}
88129
for _, o := range options {
89130
o.apply(a)
90131
}

analyzer_test.go

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
1+
// Copyright (c) 2020 Denis Tingajkin
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package goheader_test
218

319
import (
4-
"github.com/denis-tingajkin/go-header"
5-
"github.com/stretchr/testify/require"
620
"go/ast"
721
"go/parser"
822
"go/token"
923
"io/ioutil"
1024
"os"
1125
"path"
1226
"testing"
27+
28+
goheader "github.com/denis-tingajkin/go-header"
29+
"github.com/stretchr/testify/require"
1330
)
1431

15-
func header(header string) *ast.File {
16-
return &ast.File{
17-
Comments: []*ast.CommentGroup{
18-
{
19-
List: []*ast.Comment{
20-
{
21-
Text: header,
32+
func header(header string) *goheader.Target {
33+
return &goheader.Target{
34+
File: &ast.File{
35+
Comments: []*ast.CommentGroup{
36+
{
37+
List: []*ast.Comment{
38+
{
39+
Text: header,
40+
},
2241
},
2342
},
2443
},
44+
Package: token.Pos(len(header)),
2545
},
26-
Package: token.Pos(len(header)),
46+
Path: os.TempDir(),
2747
}
2848
}
2949

@@ -115,7 +135,7 @@ func TestAnalyzer_Analyze5(t *testing.T) {
115135
s := token.NewFileSet()
116136
f, err := parser.ParseFile(s, p, nil, parser.ParseComments)
117137
require.Nil(t, err)
118-
require.Nil(t, a.Analyze(f))
138+
require.Nil(t, a.Analyze(&goheader.Target{File: f, Path: p}))
119139
}
120140

121141
func TestREADME(t *testing.T) {

cmd/go-header/main.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1+
// Copyright (c) 2020 Denis Tingajkin
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package main
218

319
import (
420
"fmt"
21+
"go/parser"
22+
"go/token"
23+
"os"
24+
525
goheader "github.com/denis-tingajkin/go-header"
626
"github.com/denis-tingajkin/go-header/version"
727
"github.com/fatih/color"
828
"github.com/sirupsen/logrus"
9-
"go/parser"
10-
"go/token"
11-
"os"
1229
)
1330

1431
const configPath = ".go-header.yml"
@@ -49,7 +66,10 @@ func main() {
4966
if err != nil {
5067
logrus.Fatalf("File %v can not be parsed due compilation errors %v", p, err.Error())
5168
}
52-
i := a.Analyze(f)
69+
i := a.Analyze(&goheader.Target{
70+
Path: p,
71+
File: f,
72+
})
5373
if i != nil {
5474
issues = append(issues, &issue{
5575
Issue: i,

config.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1+
// Copyright (c) 2020 Denis Tingajkin
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package goheader
218

319
import (
420
"errors"
521
"fmt"
6-
"gopkg.in/yaml.v2"
722
"io/ioutil"
823
"strings"
24+
"time"
25+
26+
"gopkg.in/yaml.v2"
927
)
1028

1129
// Configuration represents go-header linter setup parameters
@@ -18,8 +36,16 @@ type Configuration struct {
1836
TemplatePath string `yaml:"template-path"`
1937
}
2038

21-
func (c *Configuration) GetValues() (map[string]Value, error) {
39+
func (c *Configuration) builtInValues() map[string]Value {
2240
var result = make(map[string]Value)
41+
result["year-range"] = &RegexpValue{
42+
RawValue: strings.ReplaceAll(`(200\d\-YEAR)|(YEAR)`, "YEAR", fmt.Sprint(time.Now().Year())),
43+
}
44+
return result
45+
}
46+
47+
func (c *Configuration) GetValues() (map[string]Value, error) {
48+
var result = c.builtInValues()
2349
createConst := func(raw string) Value {
2450
return &ConstValue{RawValue: raw}
2551
}
@@ -55,7 +81,7 @@ func (c *Configuration) GetTemplate() (string, error) {
5581
if b, err := ioutil.ReadFile(c.TemplatePath); err != nil {
5682
return "", err
5783
} else {
58-
c.Template = string(b)
84+
c.Template = strings.TrimSpace(string(b))
5985
return c.Template, nil
6086
}
6187
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/denis-tingajkin/go-header
22

3-
go 1.13
3+
go 1.15
44

55
require (
66
github.com/fatih/color v1.9.0

issue.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright (c) 2020 Denis Tingajkin
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package goheader
218

319
type Issue interface {

location.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright (c) 2020 Denis Tingajkin
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
117
package goheader
218

319
import "fmt"

0 commit comments

Comments
 (0)