-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-license-formating.go
160 lines (142 loc) · 3.87 KB
/
check-license-formating.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Copyright (c) 2019-2021 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"bufio"
)
var (
licenseTemplatePath string
)
func init() {
flag.StringVar(&licenseTemplatePath, "f", "", "License header template file")
}
type CommentDelimiters struct {
Start string
Delimiter string
End string
}
func main() {
flag.Parse()
if len(licenseTemplatePath) == 0 {
fmt.Println("Set up license template using flag '-f'")
os.Exit(1)
}
if flag.NArg() == 0 {
flag.Usage()
fmt.Println("There is no arguments with file paths!")
os.Exit(1)
}
templLines := readTemplate(licenseTemplatePath)
templSize := len(templLines)
rxp, err := regexp.Compile("(\\s*Copyright \\(c\\)\\s+)([0-9]{4}(-{1}[0-9]{4})?)(\\s+.*)")
if err != nil {
log.Fatal(err.Error())
}
for _, file := range flag.Args() {
commentDelms := getCommentDelimiters(file)
fileContent := readNLinesFromFile(file, templSize)
for i, line := range templLines {
var licenseLine string
switch i {
case 0:
licenseLine = commentDelms.Start + line
case len(templLines) - 2:
licenseLine = commentDelms.End + line
case len(templLines) - 1:
continue
default:
if len(line) > 0 {
licenseLine = commentDelms.Delimiter + " " + line
} else {
licenseLine = commentDelms.Delimiter
}
}
// Compare 'Copyright (c).*' line, but respect years provided by developer
if rxp.MatchString(licenseLine) {
licenseLineWithoutYears := rxp.ReplaceAllString(licenseLine, "$1$4")
fileContentLineWithoutYears := rxp.ReplaceAllString(fileContent[i], "$1$4")
if licenseLineWithoutYears != fileContentLineWithoutYears {
fmt.Printf("Bad formatted license years in the header for file %s \n", file)
os.Exit(1)
break
}
continue
}
if licenseLine != fileContent[i] {
fmt.Printf("Bad formatted license header for file %s \n", file)
fmt.Println(licenseLine)
fmt.Println(fileContent[i])
os.Exit(1)
break
}
}
}
}
func readTemplate(filePath string) []string {
content, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalf("Unable to read template file %s", filePath)
}
return strings.Split(string(content), "\n")
}
// readNLinesFromFile read N lines from file, but skips first empty lines or some special lines.
func readNLinesFromFile(filePath string, n int) []string {
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Unable to open file %s", err.Error())
}
defer func() {
if err := file.Close(); err != nil {
fmt.Printf("Unable to close file %s. Cause: %s ", filePath, err.Error())
}
}()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for i := 0; scanner.Scan() && i < n; {
if strings.HasPrefix(scanner.Text(), "#!") && filepath.Ext(filePath) == ".sh" {
// skip line with shell binary definition f.e.: '#!/bin/bash'
continue
}
if strings.HasSuffix(scanner.Text(), "!ignore_autogenerated") && filepath.Ext(filePath) == ".go" {
// skip line about autogeneration nature of the go file
continue
}
if len(scanner.Text()) > 0 || len(lines) > 0 {
lines = append(lines, scanner.Text())
i++
}
}
return lines
}
func getCommentDelimiters(file string) *CommentDelimiters {
fileExtension := filepath.Ext(file)
switch fileExtension {
case ".sh", ".yaml", ".yml":
return &CommentDelimiters{"#", "#", "#"}
case ".go":
return &CommentDelimiters{"//", "//", "//"}
case ".ts":
return &CommentDelimiters{"/**", " *", " */"}
default:
log.Fatalf("Unsupported file extension: %s", fileExtension)
}
return nil
}