Skip to content

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

Merged
merged 1 commit into from
Nov 26, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions go-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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)"
Copy link

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

Copy link
Member Author

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


goDir=
if [ -f .godir ]; then
if [ -z "$goDir" -a -s .godir ]; then
Copy link

Choose a reason for hiding this comment

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

like: ${godir:-$(test -s .godir && cat .godir)} :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Might as well just cat .godir 2>/dev/null, eh? 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

(the goal was to avoid the cat altogether if the file isn't even there, but I guess it doesn't really matter much -- I can change it if you think that's easier to read/understand)

Copy link

Choose a reason for hiding this comment

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

yes whatever works :)

Copy link

Choose a reason for hiding this comment

The 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:
goDir="$((go list -e -f '{{.ImportComment}}' || cat .godir) 2>/dev/null)"

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 that no longer means the same thing.

"go list" won't necessarily have a non-zero exit code when it has no
output, for example.

Copy link

Choose a reason for hiding this comment

The 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 || true, nvm :)

Copy link
Member Author

Choose a reason for hiding this comment

The 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
exist (hence the "2>/dev/null" and the "|| true"), but on Go 1.4, it
returns successfully because the "ImportComment" field exists in the
struct, but is empty, so it just has no output.

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")"
Expand All @@ -62,6 +66,8 @@ if [ -f .godir ]; then
exit 1
fi
goBin="$goPath/bin/$(basename "$goDir")"
else
goBin="$(basename "$dir")" # likely "app"
Copy link

Choose a reason for hiding this comment

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

maybe move or inline dir= here, since you don't need it anywhere else.

Copy link
Member Author

Choose a reason for hiding this comment

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

        ln -sfv "$dir" "$goDirPath"

(a few lines up)

Copy link

Choose a reason for hiding this comment

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

also was that intentional to remove$goPath/bin prefix?

Copy link
Member Author

Choose a reason for hiding this comment

The 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"

Copy link

Choose a reason for hiding this comment

The 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
Expand Down