-
Notifications
You must be signed in to change notification settings - Fork 516
Add go-wrapper support for Go 1.4 Custom Import Paths, relegating .godir support legacy and "second fiddle" #32
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,17 +10,19 @@ usage: $base command [args] | |
This script assumes that is is run from the root of your Go package (for | ||
example, "/go/src/app" if your GOPATH is set to "/go"). | ||
|
||
The main feature of this wrapper over the native "go" tool is that it supports | ||
the addition of a ".godir" file in your package directory which is a plain text | ||
file that is the import path your application expects (ie, it would be something | ||
like "github.com/jsmith/my-cool-app"). If this file is found, the current | ||
package is then symlinked to /go/src/<.gopath> and the commands then use that | ||
full import path to act on it. This allows for applications to be universally | ||
cloned into a path like "/go/src/app" and then automatically built properly so | ||
that inter-package imports use the existing source instead of downloading it a | ||
second time. This also makes the final binary have the correct name (ie, | ||
instead of being "/go/bin/app", it can be "/go/bin/my-cool-app"), which is why | ||
the "run" command exists here. | ||
In Go 1.4, a feature was introduced to supply the canonical "import path" for a | ||
given package in a comment attached to a package statement | ||
(https://golang.org/s/go14customimport). | ||
|
||
This script allows us to take a generic directory of Go source files such as | ||
"/go/src/app" and determine that the canonical "import path" of where that code | ||
expects to live and reference itself is "github.com/jsmith/my-cool-app". It | ||
will then ensure that "/go/src/github.com/jsmith/my-cool-app" is a symlink to | ||
"/go/src/app", which allows us to build and run it under the proper package | ||
name. | ||
|
||
For compatibility with versions of Go older than 1.4, the "import path" may also | ||
be placed in a file named ".godir". | ||
|
||
Available Commands: | ||
|
||
|
@@ -46,12 +48,14 @@ if ! shift; then | |
exit 1 | ||
fi | ||
|
||
dir="$(pwd -P)" | ||
goBin="$(basename "$dir")" # likely "app" | ||
goDir="$(go list -e -f '{{.ImportComment}}' 2>/dev/null || true)" | ||
|
||
goDir= | ||
if [ -f .godir ]; then | ||
if [ -z "$goDir" -a -s .godir ]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might as well just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (the goal was to avoid the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes whatever works :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the cat version easier to read, what about merging the two in: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because that no longer means the same thing. "go list" won't necessarily have a non-zero exit code when it has no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh ok, I assumed it returned non-zero exit code because of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Go 1.3 and lower, it returns non-zero because "ImportComment" doesn't |
||
goDir="$(cat .godir)" | ||
fi | ||
|
||
dir="$(pwd -P)" | ||
if [ "$goDir" ]; then | ||
goPath="${GOPATH%%:*}" # this just grabs the first path listed in GOPATH, if there are multiple (which is the detection logic "go get" itself uses, too) | ||
goDirPath="$goPath/src/$goDir" | ||
mkdir -p "$(dirname "$goDirPath")" | ||
|
@@ -62,6 +66,8 @@ if [ -f .godir ]; then | |
exit 1 | ||
fi | ||
goBin="$goPath/bin/$(basename "$goDir")" | ||
else | ||
goBin="$(basename "$dir")" # likely "app" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe move or inline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ln -sfv "$dir" "$goDirPath" (a few lines up) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also was that intentional to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the code further up, this is just moved: -dir="$(pwd -P)"
-goBin="$(basename "$dir")" # likely "app" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yes, my bad, for some reason I didn't see that part of the diff :) |
||
fi | ||
|
||
case "$cmd" in | ||
|
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.
suprised you didn't went for a cryptic ternary oneliner :)
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.
Hahahaha, nawww,
go list
is the bomb. 👍