Skip to content

Commit fd1a812

Browse files
committed
Add verbose option to print filename when modified
Adds the -v option which prints the name of the files that have been modified.
1 parent c6b7f1e commit fd1a812

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

main.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
license = flag.String("l", "apache", "license type: apache, bsd, mit")
5050
licensef = flag.String("f", "", "license file")
5151
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")
5253
)
5354

5455
func main() {
@@ -95,11 +96,15 @@ func main() {
9596
for f := range ch {
9697
wg.Add(1)
9798
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)
99101
if err != nil {
100102
log.Printf("%s: %v", f.path, err)
103+
return
104+
}
105+
if *verbose && modified {
106+
log.Printf("%s modified", f.path)
101107
}
102-
wg.Done()
103108
}(f)
104109
}
105110
wg.Wait()
@@ -132,12 +137,12 @@ func walk(ch chan<- *file, start string) {
132137
})
133138
}
134139

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) {
136141
var lic []byte
137142
var err error
138143
switch fileExtension(path) {
139144
default:
140-
return nil
145+
return false, nil
141146
case ".c", ".h":
142147
lic, err = prefix(tmpl, data, "/*", " * ", " */")
143148
case ".js", ".jsx", ".tsx", ".css", ".tf", ".ts":
@@ -160,12 +165,12 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
160165
lic, err = prefix(tmpl, data, "(**", " ", "*)")
161166
}
162167
if err != nil || lic == nil {
163-
return err
168+
return false, err
164169
}
165170

166171
b, err := ioutil.ReadFile(path)
167172
if err != nil || hasLicense(b) {
168-
return err
173+
return false, err
169174
}
170175

171176
line := hashBang(b)
@@ -177,7 +182,7 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
177182
lic = append(line, lic...)
178183
}
179184
b = append(lic, b...)
180-
return ioutil.WriteFile(path, b, fmode)
185+
return true, ioutil.WriteFile(path, b, fmode)
181186
}
182187

183188
func fileExtension(name string) string {

0 commit comments

Comments
 (0)