-
Notifications
You must be signed in to change notification settings - Fork 572
/
Copy pathbuild_go_vendor_cache.sh
executable file
·148 lines (128 loc) · 3.78 KB
/
build_go_vendor_cache.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
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Quit on failure
set -e
PKG_VERSION=""
SRC_TARBALL=""
OUT_FOLDER="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# parameters:
#
# --srcTarball : src tarball file
# this file contains the 'initial' source code of the component
# and should be replaced with the new/modified src code
# --outFolder : folder where to copy the new tarball(s)
# --pkgVersion : package version
# --vendorVersion : vendor version
#
PARAMS=""
while (( "$#" )); do
case "$1" in
--srcTarball)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SRC_TARBALL=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--outFolder)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
OUT_FOLDER=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--pkgVersion)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
PKG_VERSION=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--vendorVersion)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
VENDOR_VERSION=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
if [ -z "$SRC_TARBALL" ]; then
echo "--srcTarball parameter cannot be empty"
exit 1
fi
if [ -z "$PKG_VERSION" ]; then
echo "--pkgVersion parameter cannot be empty"
exit 1
fi
if [ -z "$VENDOR_VERSION" ]; then
echo "--vendorVersion parameter cannot be empty"
exit 1
fi
echo "--srcTarball -> $SRC_TARBALL"
echo "--outFolder -> $OUT_FOLDER"
echo "--pkgVersion -> $PKG_VERSION"
echo "--vendorVersion -> $VENDOR_VERSION"
temp_dir=$(mktemp -d)
echo "Working in temporary directory '$temp_dir'."
function clean-up {
echo "Cleaning up temporary directory '$temp_dir'."
rm -rf "$temp_dir"
}
trap clean-up EXIT
tarball_name=$(basename "$SRC_TARBALL")
cache_name=${tarball_name%.*}
if [[ "$cache_name" =~ \.tar$ ]]
then
cache_name=${cache_name%.*}
fi
cache_tarball_name="$cache_name-$PKG_VERSION-govendor-v$VENDOR_VERSION.tar.gz"
if [[ -f "$tarball_name" ]]
then
cp "$SRC_TARBALL" "$temp_dir"
else
echo "Tarball '$tarball_name' doesn't exist. Will attempt to download from blobstorage."
if ! wget -q "https://azurelinuxsrcstorage.blob.core.windows.net/sources/core/$tarball_name" -O "$temp_dir/$tarball_name"
then
echo "ERROR: failed to download the source tarball."
exit 1
fi
echo "Download successful."
fi
pushd "$temp_dir" &> /dev/null
echo "Extracting $tarball_name."
tar -xf "$tarball_name"
directory_name=($(ls -d */))
# assume there is only one directory in the tarball
directory_name=${directory_name[0]%//}
pushd "$directory_name" &> /dev/null
echo "Fetching dependencies to a temporary cache in $directory_name."
go mod vendor
echo "Compressing the cache."
tar --sort=name \
--mtime="2021-04-26 00:00Z" \
--owner=0 --group=0 --numeric-owner \
--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \
-czf "$cache_tarball_name" vendor
popd &> /dev/null
popd &> /dev/null
mv "$temp_dir/$directory_name/$cache_tarball_name" "$OUT_FOLDER"
echo "Done:"
sha256sum "$OUT_FOLDER"/"$cache_tarball_name"