-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathgo.sh
executable file
·137 lines (129 loc) · 3.1 KB
/
go.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# To test this script before building an image:
# docker run --rm -v $PWD:/app -w /app treeder/go-dind sh go.sh version
# Original command to vendor
# docker run --rm -it -v "$PWD":/go/src/x/y/z -w /go/src/x/y/z -e "GOPATH=/go/src/x/y/z/vendor:/go" golang go get
# Original command to build
# docker run --rm -it -v $PWD:/go/src/x/y/z -w /go/src/x/y/z -e "GOPATH=/go/src/x/y/z/vendor:/go" golang go build -o hello
# Original command to cross compile:
set -e
cmd="$1"
# echo "Go Args: $*"
if [ "$#" -lt 1 ]
then
echo "No command provided."
exit 1
fi
if [ -n "$GITCRED" ]; then
echo "creds defined"
echo $GITCRED >> ~/.git-credentials
ls -al ~
cat ~/.git-credentials
fi
if [ -n "$GITCONFIG" ]; then
echo "gitconfig defined"
echo $GITCONFIG >> ~/.gitconfig
ls -al ~
cat ~/.gitconfig
fi
# echo $PWD
wd=$PWD
defSrcPath="x/y/z"
if [ -z "$SRCPATH" ]; then
SRCPATH=$defSrcPath
fi
# echo "srcpath $SRCPATH ---"
p=/go/src/$SRCPATH
mkdir -p $p
# ls -al
if [ "$(ls -A $wd)" ]
then
# only if files exist, errors otherwise
cp -r * $p
fi
cd $p
# Add vendor to the GOPATH so get will pull it in the right spot
export GOPATH=$p/vendor:/go
# Pass in: $# MIN_ARGS
validate () {
if [ "$1" -lt $2 ]
then
echo "No command provided."
exit 1
fi
}
vendor () {
go get
cp -r $1/vendor $2
chmod -R a+rw $2/vendor
# cd $wd
}
build () {
# echo "build: $1 $2"
go build $1
cp app $2
chmod a+rwx $wd/app
}
case "$1" in
vendor) echo "Vendoring dependencies..."
vendor $p $wd
;;
build) echo "Building..."
build "-o app" $wd
;;
fmt) echo "Formatting..."
cd $wd
go fmt
;;
cross) echo "Cross compiling..."
for GOOS in darwin linux windows; do
for GOARCH in 386 amd64; do
echo "Building $GOOS-$GOARCH"
export GOOS=$GOOS
export GOARCH=$GOARCH
go build -o bin/app-$GOOS-$GOARCH
done
done
cp -r bin $wd
chmod -R a+rw $wd/bin
# ls -al $wd/bin
;;
static) echo "Building static binary..."
CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o static
cp static $wd
chmod a+rwx $wd/static
;;
remote) echo "Building binary from $2"
validate $# 2
userwd=$wd
cd
git clone $2 repo
cd repo
wd=$PWD
# Need to redo some initial setup here:
cp -r * $p
cd $p
vendor $p $wd
build "-o app" $wd
cp $wd/app $userwd
chmod a+rwx $userwd/app
;;
image) echo "Building Docker image '$2'..."
validate $# 2
ls -al /usr/bin/docker
build "-o app" $wd
/usr/bin/docker version
cp /scripts/lib/Dockerfile $p
/usr/bin/docker build -t $2 .
# perhaps an alternative to this would be to do dind, replace the FROM with treeder/go-dind for example:
# cp /scripts/lib/Dockerfile $p
# /usr/bin/dockerlaunch /usr/bin/docker -d -D &
# sleep 3
# docker build -t $2 .
;;
version)
go version
;;
*) echo "Invalid command, see https://github.com/treeder/dockers/tree/master/go for reference."
;;
esac
exit 0