-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (155 loc) · 4.05 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// Copyright 2022 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/arduino/go-paths-helper"
"github.com/spf13/cobra"
)
var onlyDetectLicense bool
func main() {
command := &cobra.Command{
Long: "licenser is a tool for keeping the license text in the source code up-to-date.",
Use: "licenser SOURCE_ROOT_DIR [LICENSE_FILE]",
SilenceUsage: false,
Example: "licenser .",
Run: licenser,
}
command.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
fatal(1, "%s\n", err)
return err
})
flags := command.Flags()
flags.BoolVarP(&onlyDetectLicense, "detect-only", "d", false, "Only detect and print license")
_ = command.Execute()
}
func fatal(code int, format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "Error: "+format, args...)
os.Exit(code)
}
func licenser(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fatal(2, "Please specify the root directory\n")
}
rootDir := paths.New(args[0])
if !rootDir.IsDir() {
fatal(3, "%s is not a directory\n", rootDir)
}
var license []string
if len(args) < 2 {
license = detectLicense(rootDir)
} else if l, err := paths.New(args[1]).ReadFileAsLines(); err != nil {
fatal(4, "Error reading license file: %s\n", err)
} else {
license = l
}
if onlyDetectLicense {
fmt.Println("Detected license:")
fmt.Println()
for _, l := range license {
fmt.Println(">", l)
}
os.Exit(0)
}
sources, err := rootDir.ReadDirRecursiveFiltered(
paths.AndFilter(
func(file *paths.Path) bool { return file.Base() != ".git" },
),
paths.FilterOutDirectories(),
)
if err != nil {
fatal(8, "Error reading source directory: %s\n", err)
}
for _, s := range sources {
switch s.Ext() {
case ".go", ".c", ".cpp", ".h":
applyLicenseCStyle(s, license)
default:
fmt.Println("IGNORED:", s)
}
}
}
func detectLicense(rootDir *paths.Path) []string {
if rootDir.Join("go.mod").Exist() {
fmt.Println("Golang project detected")
// First check for docs.go
if source, err := rootDir.Join("doc.go").ReadFileAsLines(); err == nil {
fmt.Println("Extracting license from doc.go")
return extractLicense(source)
}
}
fatal(5, "Could not find any license file in %s\n", rootDir)
return nil
}
func extractLicense(source []string) []string {
if len(source) == 0 {
fatal(6, "License file is empty.\n")
}
for i := range source {
source[i] = strings.TrimSpace(source[i])
}
if source[0] == "" {
fatal(7, "The first line of the license file must not be empty.\n")
}
license := []string{}
for len(source) > 0 && (source[0] == "//" || strings.HasPrefix(source[0], "// ")) {
l := strings.TrimPrefix(source[0], "// ")
l = strings.TrimPrefix(l, "//")
license = append(license, l)
source = source[1:]
}
return license
}
func applyLicenseCStyle(sourceFile *paths.Path, license []string) {
source, err := sourceFile.ReadFileAsLines()
if err != nil {
fatal(8, "Error opening %s: %s\n", sourceFile, err)
}
output := new(bytes.Buffer)
for _, line := range license {
if line == "" {
output.WriteString(fmt.Sprintln("//"))
} else {
output.WriteString(fmt.Sprintln("//", line))
}
}
output.Write([]byte(fmt.Sprintln()))
original := new(bytes.Buffer)
header := true
for i, line := range source {
last := i == len(source)-1
original.WriteString(line)
if !last {
original.WriteString(fmt.Sprintln())
}
if header && strings.HasPrefix(line, "//") {
continue
}
if header {
header = false
if line != "" {
// This is not a license header, we should not replace it in the output
output.Write(original.Bytes())
}
continue
}
output.WriteString(line)
if !last {
output.WriteString(fmt.Sprintln())
}
}
if bytes.Equal(original.Bytes(), output.Bytes()) {
fmt.Println("OK", sourceFile)
return
}
if err := sourceFile.WriteFile(output.Bytes()); err != nil {
fatal(8, "Error writing %s: %s\n", sourceFile, err)
}
fmt.Println("UPDATED", sourceFile)
}