Skip to content

Commit 5a66872

Browse files
committed
Merge pull request #10 from infosiftr/go-wrapper
Add simple "go-wrapper" script for building and running, especially to accommodate ".godir" support
2 parents 9fdbcc4 + 9ff2ccc commit 5a66872

File tree

17 files changed

+554
-15
lines changed

17 files changed

+554
-15
lines changed

1.2.1/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ RUN mkdir -p /go/src
2020
ENV GOPATH /go
2121
ENV PATH /go/bin:$PATH
2222
WORKDIR /go
23+
24+
COPY go-wrapper /usr/local/bin/

1.2.1/go-wrapper

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
set -e
3+
4+
usage() {
5+
base="$(basename "$0")"
6+
cat <<EOUSAGE
7+
8+
usage: $base command [args]
9+
10+
This script assumes that is is run from the root of your Go package (for
11+
example, "/go/src/app" if your GOPATH is set to "/go").
12+
13+
The main feature of this wrapper over the native "go" tool is that it supports
14+
the addition of a ".godir" file in your package directory which is a plain text
15+
file that is the import path your application expects (ie, it would be something
16+
like "github.com/jsmith/my-cool-app"). If this file is found, the current
17+
package is then symlinked to /go/src/<.gopath> and the commands then use that
18+
full import path to act on it. This allows for applications to be universally
19+
cloned into a path like "/go/src/app" and then automatically built properly so
20+
that inter-package imports use the existing source instead of downloading it a
21+
second time. This also makes the final binary have the correct name (ie,
22+
instead of being "/go/bin/app", it can be "/go/bin/my-cool-app"), which is why
23+
the "run" command exists here.
24+
25+
Available Commands:
26+
27+
$base download
28+
$base download -u
29+
(equivalent to "go get -d [args] [godir]")
30+
31+
$base install
32+
$base install -race
33+
(equivalent to "go install [args] [godir]")
34+
35+
$base run
36+
$base run -app -specific -arguments
37+
(assumes "GOPATH/bin" is in "PATH")
38+
39+
EOUSAGE
40+
}
41+
42+
# "shift" so that "$@" becomes the remaining arguments and can be passed along to other "go" subcommands easily
43+
cmd="$1"
44+
if ! shift; then
45+
usage >&2
46+
exit 1
47+
fi
48+
49+
dir="$(pwd -P)"
50+
goBin="$(basename "$dir")" # likely "app"
51+
52+
goDir=
53+
if [ -f .godir ]; then
54+
goDir="$(cat .godir)"
55+
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)
56+
goDirPath="$goPath/src/$goDir"
57+
mkdir -p "$(dirname "$goDirPath")"
58+
if [ -e "$goDirPath" -a ! -L "$goDirPath" ]; then
59+
echo >&2 "error: $goDirPath already exists but is unexpectedly not a symlink!"
60+
exit 1
61+
fi
62+
ln -sfv "$dir" "$goDirPath"
63+
goBin="$goPath/bin/$(basename "$goDir")"
64+
fi
65+
66+
case "$cmd" in
67+
download)
68+
execCommand=( go get -v -d "$@" )
69+
if [ "$goDir" ]; then execCommand+=( "$goDir" ); fi
70+
set -x; exec "${execCommand[@]}"
71+
;;
72+
73+
install)
74+
execCommand=( go install -v "$@" )
75+
if [ "$goDir" ]; then execCommand+=( "$goDir" ); fi
76+
set -x; exec "${execCommand[@]}"
77+
;;
78+
79+
run)
80+
set -x; exec "$goBin" "$@"
81+
;;
82+
83+
*)
84+
echo >&2 'error: unknown command:' "$cmd"
85+
usage >&2
86+
exit 1
87+
;;
88+
esac

1.2.1/onbuild/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ RUN mkdir -p /go/src/app
44
WORKDIR /go/src/app
55

66
# this will ideally be built by the ONBUILD below ;)
7-
CMD ["/go/bin/app"]
7+
CMD ["go-wrapper", "run"]
88

99
ONBUILD COPY . /go/src/app
10-
ONBUILD RUN go get -d -v ./...
11-
ONBUILD RUN go build -v ./...
10+
ONBUILD RUN go-wrapper download
11+
ONBUILD RUN go-wrapper install

1.2.2/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ RUN mkdir -p /go/src
2020
ENV GOPATH /go
2121
ENV PATH /go/bin:$PATH
2222
WORKDIR /go
23+
24+
COPY go-wrapper /usr/local/bin/

1.2.2/go-wrapper

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
set -e
3+
4+
usage() {
5+
base="$(basename "$0")"
6+
cat <<EOUSAGE
7+
8+
usage: $base command [args]
9+
10+
This script assumes that is is run from the root of your Go package (for
11+
example, "/go/src/app" if your GOPATH is set to "/go").
12+
13+
The main feature of this wrapper over the native "go" tool is that it supports
14+
the addition of a ".godir" file in your package directory which is a plain text
15+
file that is the import path your application expects (ie, it would be something
16+
like "github.com/jsmith/my-cool-app"). If this file is found, the current
17+
package is then symlinked to /go/src/<.gopath> and the commands then use that
18+
full import path to act on it. This allows for applications to be universally
19+
cloned into a path like "/go/src/app" and then automatically built properly so
20+
that inter-package imports use the existing source instead of downloading it a
21+
second time. This also makes the final binary have the correct name (ie,
22+
instead of being "/go/bin/app", it can be "/go/bin/my-cool-app"), which is why
23+
the "run" command exists here.
24+
25+
Available Commands:
26+
27+
$base download
28+
$base download -u
29+
(equivalent to "go get -d [args] [godir]")
30+
31+
$base install
32+
$base install -race
33+
(equivalent to "go install [args] [godir]")
34+
35+
$base run
36+
$base run -app -specific -arguments
37+
(assumes "GOPATH/bin" is in "PATH")
38+
39+
EOUSAGE
40+
}
41+
42+
# "shift" so that "$@" becomes the remaining arguments and can be passed along to other "go" subcommands easily
43+
cmd="$1"
44+
if ! shift; then
45+
usage >&2
46+
exit 1
47+
fi
48+
49+
dir="$(pwd -P)"
50+
goBin="$(basename "$dir")" # likely "app"
51+
52+
goDir=
53+
if [ -f .godir ]; then
54+
goDir="$(cat .godir)"
55+
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)
56+
goDirPath="$goPath/src/$goDir"
57+
mkdir -p "$(dirname "$goDirPath")"
58+
if [ -e "$goDirPath" -a ! -L "$goDirPath" ]; then
59+
echo >&2 "error: $goDirPath already exists but is unexpectedly not a symlink!"
60+
exit 1
61+
fi
62+
ln -sfv "$dir" "$goDirPath"
63+
goBin="$goPath/bin/$(basename "$goDir")"
64+
fi
65+
66+
case "$cmd" in
67+
download)
68+
execCommand=( go get -v -d "$@" )
69+
if [ "$goDir" ]; then execCommand+=( "$goDir" ); fi
70+
set -x; exec "${execCommand[@]}"
71+
;;
72+
73+
install)
74+
execCommand=( go install -v "$@" )
75+
if [ "$goDir" ]; then execCommand+=( "$goDir" ); fi
76+
set -x; exec "${execCommand[@]}"
77+
;;
78+
79+
run)
80+
set -x; exec "$goBin" "$@"
81+
;;
82+
83+
*)
84+
echo >&2 'error: unknown command:' "$cmd"
85+
usage >&2
86+
exit 1
87+
;;
88+
esac

1.2.2/onbuild/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ RUN mkdir -p /go/src/app
44
WORKDIR /go/src/app
55

66
# this will ideally be built by the ONBUILD below ;)
7-
CMD ["/go/bin/app"]
7+
CMD ["go-wrapper", "run"]
88

99
ONBUILD COPY . /go/src/app
10-
ONBUILD RUN go get -d -v ./...
11-
ONBUILD RUN go build -v ./...
10+
ONBUILD RUN go-wrapper download
11+
ONBUILD RUN go-wrapper install

1.2/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ RUN mkdir -p /go/src
2020
ENV GOPATH /go
2121
ENV PATH /go/bin:$PATH
2222
WORKDIR /go
23+
24+
COPY go-wrapper /usr/local/bin/

1.2/go-wrapper

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
set -e
3+
4+
usage() {
5+
base="$(basename "$0")"
6+
cat <<EOUSAGE
7+
8+
usage: $base command [args]
9+
10+
This script assumes that is is run from the root of your Go package (for
11+
example, "/go/src/app" if your GOPATH is set to "/go").
12+
13+
The main feature of this wrapper over the native "go" tool is that it supports
14+
the addition of a ".godir" file in your package directory which is a plain text
15+
file that is the import path your application expects (ie, it would be something
16+
like "github.com/jsmith/my-cool-app"). If this file is found, the current
17+
package is then symlinked to /go/src/<.gopath> and the commands then use that
18+
full import path to act on it. This allows for applications to be universally
19+
cloned into a path like "/go/src/app" and then automatically built properly so
20+
that inter-package imports use the existing source instead of downloading it a
21+
second time. This also makes the final binary have the correct name (ie,
22+
instead of being "/go/bin/app", it can be "/go/bin/my-cool-app"), which is why
23+
the "run" command exists here.
24+
25+
Available Commands:
26+
27+
$base download
28+
$base download -u
29+
(equivalent to "go get -d [args] [godir]")
30+
31+
$base install
32+
$base install -race
33+
(equivalent to "go install [args] [godir]")
34+
35+
$base run
36+
$base run -app -specific -arguments
37+
(assumes "GOPATH/bin" is in "PATH")
38+
39+
EOUSAGE
40+
}
41+
42+
# "shift" so that "$@" becomes the remaining arguments and can be passed along to other "go" subcommands easily
43+
cmd="$1"
44+
if ! shift; then
45+
usage >&2
46+
exit 1
47+
fi
48+
49+
dir="$(pwd -P)"
50+
goBin="$(basename "$dir")" # likely "app"
51+
52+
goDir=
53+
if [ -f .godir ]; then
54+
goDir="$(cat .godir)"
55+
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)
56+
goDirPath="$goPath/src/$goDir"
57+
mkdir -p "$(dirname "$goDirPath")"
58+
if [ -e "$goDirPath" -a ! -L "$goDirPath" ]; then
59+
echo >&2 "error: $goDirPath already exists but is unexpectedly not a symlink!"
60+
exit 1
61+
fi
62+
ln -sfv "$dir" "$goDirPath"
63+
goBin="$goPath/bin/$(basename "$goDir")"
64+
fi
65+
66+
case "$cmd" in
67+
download)
68+
execCommand=( go get -v -d "$@" )
69+
if [ "$goDir" ]; then execCommand+=( "$goDir" ); fi
70+
set -x; exec "${execCommand[@]}"
71+
;;
72+
73+
install)
74+
execCommand=( go install -v "$@" )
75+
if [ "$goDir" ]; then execCommand+=( "$goDir" ); fi
76+
set -x; exec "${execCommand[@]}"
77+
;;
78+
79+
run)
80+
set -x; exec "$goBin" "$@"
81+
;;
82+
83+
*)
84+
echo >&2 'error: unknown command:' "$cmd"
85+
usage >&2
86+
exit 1
87+
;;
88+
esac

1.2/onbuild/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ RUN mkdir -p /go/src/app
44
WORKDIR /go/src/app
55

66
# this will ideally be built by the ONBUILD below ;)
7-
CMD ["/go/bin/app"]
7+
CMD ["go-wrapper", "run"]
88

99
ONBUILD COPY . /go/src/app
10-
ONBUILD RUN go get -d -v ./...
11-
ONBUILD RUN go build -v ./...
10+
ONBUILD RUN go-wrapper download
11+
ONBUILD RUN go-wrapper install

1.3.1/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ RUN mkdir -p /go/src
2020
ENV GOPATH /go
2121
ENV PATH /go/bin:$PATH
2222
WORKDIR /go
23+
24+
COPY go-wrapper /usr/local/bin/

0 commit comments

Comments
 (0)