Skip to content
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

Merged
merged 1 commit into from
Jan 9, 2020

Conversation

tweksteen
Copy link
Member

Adds the -v option which prints the name of the files that have been modified.

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)
Copy link
Contributor

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.

Copy link
Member Author

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).

Copy link
Contributor

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?

Copy link
Member Author

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)
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Adds the -v option which prints the name of the files that have been
modified.
Copy link
Contributor

@x1ddos x1ddos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thanks!

@x1ddos x1ddos merged commit 9fa18aa into google:master Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants