-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdevbase.sh
More file actions
executable file
·118 lines (100 loc) · 3.44 KB
/
devbase.sh
File metadata and controls
executable file
·118 lines (100 loc) · 3.44 KB
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
#!/usr/bin/env bash
# This script ensures we have a copy of devbase
set -e
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
libDir="$DIR/../.bootstrap"
lockfile="$DIR/../stencil.lock"
serviceYaml="$DIR/../service.yaml"
gojqVersion="v0.12.17"
# get_absolute_path returns the absolute path of a file
get_absolute_path() {
realpath "$1"
}
# gojq returns the path to a JIT-downloaded gojq binary.
gojq() {
set -uo pipefail
local gjDir
gjDir="${XDG_CACHE_HOME:-$HOME/.cache}/devbase/gojq"
local gojq="$gjDir/gojq-${gojqVersion}"
if [[ ! -x $gojq ]]; then
local platform arch
mkdir -p "$gjDir"
platform="$(uname -s | awk '{print tolower($0)}')"
arch="$(uname -m)"
case $arch in
x86_64)
arch=amd64
;;
aarch64)
arch=arm64
;;
esac
local basename="gojq_${gojqVersion}_${platform}_${arch}"
local ext
if [[ $platform == linux ]]; then
ext="tar.gz"
else
ext="zip"
fi
local archive="$basename.$ext"
local gojqURL="https://github.com/itchyny/gojq/releases/download/$gojqVersion/$archive"
local archivePath="$gjDir/$archive"
if [[ ! -e $archivePath ]]; then
if command -v busybox >/dev/null; then
busybox wget --quiet --output-document "$archivePath" "$gojqURL"
elif command -v wget >/dev/null; then
wget --quiet --output-document "$archivePath" "$gojqURL"
elif command -v curl >/dev/null; then
curl --fail --location --silent --output "$archivePath" "$gojqURL"
else
echo "No download tool found (looked for busybox, wget, curl)" >&2
exit 1
fi
fi
if [[ ! -e $archivePath ]]; then
echo "Failed to download gojq ($gojqURL)" >&2
exit 1
fi
if [[ $ext == "zip" ]]; then
# Explanation of flags:
# quiet, junk paths/dont make directories, extract to directory
unzip -q -j -d "$gjDir" "$archivePath" "$basename/gojq"
else
tar --strip-components=1 --directory="$gjDir" --extract --file="$archivePath" "$basename/gojq"
fi
mv "$gjDir"/gojq "$gojq"
fi
echo "$gojq"
set +uo pipefail
}
# get_field_from_yaml reads a field from a yaml file via a JIT-downloaded gojq.
get_field_from_yaml() {
local field="$1"
local file="$2"
"$(gojq)" --yaml-input -r "$field" <"$file"
}
# Use the version of devbase from stencil
version=$(get_field_from_yaml '.modules[] | select(.name == "github.com/getoutreach/devbase") | .version' "$lockfile")
existingVersion=$(cat "$libDir/.version" 2>/dev/null || true)
if [[ ! -e $libDir ]] || [[ $existingVersion != "$version" ]] || [[ ! -e "$libDir/.version" ]]; then
rm -rf "$libDir"
if [[ $version == "local" ]]; then
# If we're using a local version, we should use ln
# to map the local devbase into the bootstrap dir
path=$(get_field_from_yaml '.replacements["github.com/getoutreach/devbase"]' "$serviceYaml")
absolute_path=$(get_absolute_path "$path")
ln -sf "$absolute_path" "$libDir"
else
git clone -q --single-branch --branch "$version" git@github.com:getoutreach/devbase \
"$libDir" >/dev/null
fi
echo -n "$version" >"$libDir/.version"
fi
# If we're not using a local version, ensure that service.yaml doesn't exist
# in the library directory. This is because of the repo detection logic
# which looks for the base directory through the existence of that file.
if [[ $version != "local" ]]; then
if [[ -e "$libDir/service.yaml" ]]; then
rm "$libDir/service.yaml"
fi
fi