-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
208 lines (180 loc) · 4.63 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
// Return values
const (
FileReadError = 0
NormalFiles = 1
CompilerFlags = 2
AutoGenerated = 3
IsExecutable = 0111
)
// Applier interface required to implement for new file type
type Applier interface {
CheckHeader(target *os.File, t *TagContext) (bool, error)
ApplyHeader(path string, t *TagContext) error
}
// TagContext keeps context info for Applier
type TagContext struct {
excludeList []string
templatePath string
templateFiles TemplateFiles
dryRun bool
outfileList []string
}
// TemplateFiles stores template of header
type TemplateFiles struct {
goTemplateFile *os.File
shTemplateFile *os.File
dTemplateFile *os.File
mTemplateFile *os.File
}
func main() {
ppath := flag.String("path", ".", "project path")
excludes := flag.String("excludes", "vendor", "exclude folders")
tpath := flag.String("t", "./template", "template files path")
dryRun := flag.Bool("check", false, "check files missing header")
verbose := flag.Bool("v", false, "verbose output")
flag.Parse()
dTFile, err := loadTemplate(*tpath, "dockerfile.txt")
if err != nil {
fmt.Println("No template file for Dockerfile, shall skip all Dockerfile")
}
if dTFile != nil {
defer dTFile.Close()
}
goTFile, err := loadTemplate(*tpath, "go.txt")
if err != nil {
fmt.Println("No template file for golang files, shall skip all golang files")
}
if goTFile != nil {
defer goTFile.Close()
}
bashTFile, err := loadTemplate(*tpath, "bash.txt")
if err != nil {
fmt.Println("No template file for bash scripts, shall skip all bash scripts")
}
if bashTFile != nil {
defer bashTFile.Close()
}
makeTFile, err := loadTemplate(*tpath, "makefile.txt")
if err != nil {
fmt.Println("No template file for Makefile, shall skip all makefiles")
}
if makeTFile != nil {
defer makeTFile.Close()
}
excludeList := strings.Split(*excludes, " ")
templateFiles := TemplateFiles{
mTemplateFile: makeTFile,
shTemplateFile: bashTFile,
goTemplateFile: goTFile,
dTemplateFile: dTFile}
t := TagContext{
excludeList: excludeList,
templateFiles: templateFiles,
templatePath: *tpath,
dryRun: *dryRun}
if err = filepath.Walk(*ppath, t.tagFiles); err != nil {
panic(err)
}
if !*dryRun {
fmt.Println("Files modified : ", len(t.outfileList))
}
if *verbose {
for _, path := range t.outfileList {
fmt.Println(path)
}
}
if *dryRun && len(t.outfileList) > 0 {
os.Exit(1)
}
}
func (t *TagContext) tagFiles(path string, f os.FileInfo, err error) error {
var applier Applier
processed := false
if (f.Mode() & os.ModeSymlink) != 0 { // skip symlinks
return nil
}
if (f.Name() == ".git" || f.Name() == ".svn" || f.Name() == "..") && f.IsDir() {
return filepath.SkipDir
}
if f.IsDir() {
for _, exclude := range t.excludeList {
if f.Name() == exclude {
return filepath.SkipDir
}
}
}
if !f.IsDir() && f.Size() > 0 {
if f.Name() == "LICENSE" || f.Name() == "MAINTAINERS" {
return nil
}
file, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
return err
}
defer file.Close()
fname := strings.Split(f.Name(), ".")
if len(fname) == 1 { // Without extension.
if f.Mode()&IsExecutable != 0 && t.templateFiles.shTemplateFile != nil {
applier = &bashApplier{}
processed = true
}
if fname[0] == "Makefile" && t.templateFiles.mTemplateFile != nil {
applier = &makefileApplier{}
processed = true
}
if strings.ToLower(fname[0]) == "dockerfile" && t.templateFiles.dTemplateFile != nil {
applier = &dockerfileApplier{}
processed = true
}
} else {
if fname[1] == "go" && t.templateFiles.goTemplateFile != nil {
applier = &golangApplier{}
processed = true
}
if strings.ToLower(fname[1]) == "dockerfile" && t.templateFiles.dTemplateFile != nil {
applier = &dockerfileApplier{}
processed = true
}
if strings.ToLower(fname[0]) == "makefile" && t.templateFiles.mTemplateFile != nil {
applier = &makefileApplier{}
processed = true
}
if fname[1] == "sh" && t.templateFiles.shTemplateFile != nil {
applier = &bashApplier{}
processed = true
}
}
if !processed {
return nil
}
processed = false
headerExist, err := applier.CheckHeader(file, t)
if err != nil {
return err
}
if headerExist {
return nil
}
if !t.dryRun {
err = applier.ApplyHeader(path, t)
if err != nil {
return err
}
}
t.outfileList = append(t.outfileList, path)
}
return nil
}
func loadTemplate(path string, name string) (*os.File, error) {
templateFile := filepath.Join(path, name)
tFile, err := os.OpenFile(templateFile, os.O_RDONLY, 0666)
return tFile, err
}