49
49
license = flag .String ("l" , "apache" , "license type: apache, bsd, mit" )
50
50
licensef = flag .String ("f" , "" , "license file" )
51
51
year = flag .String ("y" , fmt .Sprint (time .Now ().Year ()), "copyright year(s)" )
52
+ verbose = flag .Bool ("v" , false , "verbose mode: print the name of the files that are modified" )
52
53
)
53
54
54
55
func main () {
@@ -95,11 +96,15 @@ func main() {
95
96
for f := range ch {
96
97
wg .Add (1 )
97
98
go func (f * file ) {
98
- err := addLicense (f .path , f .mode , t , data )
99
+ defer wg .Done ()
100
+ modified , err := addLicense (f .path , f .mode , t , data )
99
101
if err != nil {
100
102
log .Printf ("%s: %v" , f .path , err )
103
+ return
104
+ }
105
+ if * verbose && modified {
106
+ log .Printf ("%s modified" , f .path )
101
107
}
102
- wg .Done ()
103
108
}(f )
104
109
}
105
110
wg .Wait ()
@@ -132,12 +137,12 @@ func walk(ch chan<- *file, start string) {
132
137
})
133
138
}
134
139
135
- func addLicense (path string , fmode os.FileMode , tmpl * template.Template , data * copyrightData ) error {
140
+ func addLicense (path string , fmode os.FileMode , tmpl * template.Template , data * copyrightData ) ( bool , error ) {
136
141
var lic []byte
137
142
var err error
138
143
switch fileExtension (path ) {
139
144
default :
140
- return nil
145
+ return false , nil
141
146
case ".c" , ".h" :
142
147
lic , err = prefix (tmpl , data , "/*" , " * " , " */" )
143
148
case ".js" , ".jsx" , ".tsx" , ".css" , ".tf" , ".ts" :
@@ -160,12 +165,12 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
160
165
lic , err = prefix (tmpl , data , "(**" , " " , "*)" )
161
166
}
162
167
if err != nil || lic == nil {
163
- return err
168
+ return false , err
164
169
}
165
170
166
171
b , err := ioutil .ReadFile (path )
167
172
if err != nil || hasLicense (b ) {
168
- return err
173
+ return false , err
169
174
}
170
175
171
176
line := hashBang (b )
@@ -177,7 +182,7 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
177
182
lic = append (line , lic ... )
178
183
}
179
184
b = append (lic , b ... )
180
- return ioutil .WriteFile (path , b , fmode )
185
+ return true , ioutil .WriteFile (path , b , fmode )
181
186
}
182
187
183
188
func fileExtension (name string ) string {
0 commit comments