-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add verbose option to print filename when modified #25
Conversation
main.go
Outdated
@@ -95,7 +96,7 @@ func main() { | |||
for f := range ch { | |||
wg.Add(1) | |||
go func(f *file) { | |||
err := addLicense(f.path, f.mode, t, data) | |||
err := addLicense(f.path, f.mode, t, data, *verbose) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's better to do the verbose logging here for two reasons:
- cluttering arguments: the addLicense func never logs an error so why should it log anything else
- there's already if err != nil { ... } right below this line to print an error. It feels like log.Printf("%v modified", path) belongs here. So, maybe something like:
defer wg.Done() // move it at the top
err := addLicense(f.path, f.mode, t, data)
if err != nil {
log.Printf("%s: %v", f.path, err)
return
}
if verbose {
log.Printf("%s modified", path)
}
This also avoid making invalid claim "path/to/file modified" it there were an I/O error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because if this function does not return an error, it doesn't mean the file has been modified. Specifically if err != nil || hasLicense(b)
will return nil if we consider that the file already has a license.
We could change the definition of addLicense to return (modified bool, err error).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah sorry you're right!
yeah, I like the idea of addLicense(...) (modified bool, err error)
.
let's do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. PTAL. Thanks.
main.go
Outdated
@@ -177,6 +178,9 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c | |||
lic = append(line, lic...) | |||
} | |||
b = append(lic, b...) | |||
if verbose { | |||
log.Printf("%v modified", path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go usually invites to use %s
for strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
da32d53
to
191868e
Compare
Adds the -v option which prints the name of the files that have been modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks!
Adds the -v option which prints the name of the files that have been modified.