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

remember original replace directive #24

Merged
merged 1 commit into from
Oct 4, 2018
Merged

Conversation

rogpeppe
Copy link
Owner

@rogpeppe rogpeppe commented Oct 1, 2018

When we run gohack get on a go.mod file that contains a replace
directive, we now remember the old information inside a comment,
and restore it when running gohack undo.

To do this, we need to be able to parse go.mod comments, so we
now use the modfile package rather than go mod edit.

We also use testscript to add some tests. More tests still needed :)

@rogpeppe
Copy link
Owner Author

rogpeppe commented Oct 1, 2018

This should fix (at least partially) issue #8.

if err := f.AddReplace(mod, "", dir, ""); err != nil {
return errors.Wrap(err)
}
return nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

If errors.Wrap knew to keep nil errors as nil, you'd be able to just do a single-line return here I think.

Though it would be a bit of an ugly line.

Choose a reason for hiding this comment

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

drive-by comment, sorry. The more popular pkg/errors handles this case :)
https://github.com/pkg/errors/blob/c059e472/errors.go#L180-L183

Copy link
Owner Author

Choose a reason for hiding this comment

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

errors.Wrap does know to keep nil errors as nil, but it made for a bit of an ugly line so I avoided it here :)

cmdundo.go Outdated
// It's not a "was" comment. Just remove it (after this loop so we don't
// interfere with the current range statement).
drop[r.Old.Path] = true
delete(modMap, r.Old.Path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can move these two deletes out of the if/else

}
for _, m := range modules {
fmt.Printf("dropped %s\n", m)
}
return nil
}

func splitWasComment(s string) *modfile.Replace {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not implement this with regexp and capture groups? This kind of string handling code always looks complicated to me.

Copy link
Owner Author

Choose a reason for hiding this comment

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

yeah, not a bad idea. done.

return tokens
}

func splitPathVersion(s string) (module.Version, bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit - why not return just module.Version, then check if it's non-nil? Some types, like go/token.Position, have an IsValid() bool method to make the code easier to write.

Copy link
Owner Author

Choose a reason for hiding this comment

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

not sure. I think the bool return is quite clear, and the code not much longer with it. i'll leave it for now.

continue
}
// These checks shouldn't fail when checkCanReplace has been
// called previously, but check anyway just to be sure.
Copy link
Collaborator

Choose a reason for hiding this comment

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

then maybe make them panics? otherwise you may have a bug in checkCanReplace, and you might never notice.

Or perhaps make these error messages more scary, like "this shouldn't happen, file a bug!". Though I tend to just make these be panics.

Copy link
Owner Author

Choose a reason for hiding this comment

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

done

When we run `gohack get` on a go.mod file that contains a replace
directive, we now remember the old information inside a comment,
and restore it when running `gohack undo`.

To do this, we need to be able to parse go.mod comments, so we
now use the modfile package rather than `go mod edit`.

We also use testscript to add some tests. More tests still needed :)
@rogpeppe rogpeppe merged commit deec612 into master Oct 4, 2018
This was referenced Oct 5, 2018
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.

3 participants