Last active
February 17, 2021 18:29
-
-
Save iiiw/aa53ebfcf8a9fadc3f2a09c5dcb9b372 to your computer and use it in GitHub Desktop.
Adapt mattermost plugins to build ARM executables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# vim: ft=sh | |
set -o errexit | |
cmds=('diff' 'go' 'jq (github.com/stedolan/jq)' 'patch') | |
for cmd in "${cmds[@]}"; do | |
command -v "${cmd%% *}" >/dev/null 2>&1 \ | |
|| { echo "$0: $cmd must be installed" >&2; exit 1; } | |
done | |
case $1 in | |
arm|armv7|armv7l) ARCH=arm;; | |
arm64|aarch64) ARCH=arm64;; | |
*) ARCH="$(go env GOARCH)";; | |
esac | |
DIR="$(pwd)" | |
PLUGIN_ID="$(jq -r '.id' < "$DIR/plugin.json")" | |
declare -a changelog | |
changelog=('# Changelog:') | |
if [[ -s "$DIR/Makefile" ]]; then | |
# Find the server build command and modify it to target linux ARM. | |
GO_BUILD=$(sed -n \ | |
"/env GOOS=linux GOARCH=amd64 \$(GO) build/ { s/amd64/$ARCH/gp;q; }" \ | |
"$DIR/Makefile") | |
# Uncomment if it was commented out. | |
GO_BUILD="${GO_BUILD#*\#}" | |
# Comment out all existing build commands and insert the 'new' one. | |
# A backup of the unmodified Makefile is saved as Makefile.orig | |
RE='[[:alnum:]]\{1,20\}' | |
sed -i.orig \ | |
-e "/env GOOS=$RE GOARCH=$RE \$(GO) build/ s/^[^#]/#&/" \ | |
-e $'/\$(GO) build \$(GO_BUILD_FLAGS) -o dist\/plugin-linux-amd64/ i\\\n'"${GO_BUILD}"$'\n' \ | |
"$DIR/Makefile" | |
dist="$(echo "${GO_BUILD##*-o }" | sed 's/;$//')" | |
changelog+=('Makefile:') | |
changelog+=(" changed \`go build\` to build: $dist") | |
else | |
echo "$0: skipping Makefile: file not found" >&2 | |
fi | |
if [[ -s "$DIR/plugin.json" ]]; then | |
# Replace the path to the server executable with the new path. | |
fn="setpath([\"server\"]; {\"executable\": \"server/dist/plugin-linux-$ARCH\"})" | |
json=$(jq --indent 4 "$fn" < "$DIR/plugin.json") | |
# A backup of the unmodified plugin.json is saved as plugin.json.orig | |
diff -ub "$DIR/plugin.json" <(echo "$json") | patch -bs | |
path="$(jq -c '.server' < "$DIR/plugin.json")" | |
changelog+=('plugin.json:') | |
changelog+=(" modified path: \"server\": $path") | |
else | |
echo "$0: skipping plugin.json: file not found" >&2 | |
fi | |
if (( ${#changelog[@]} > 1 )); then | |
printf '%s\n' "${changelog[@]}" | |
echo | |
echo '# To build and install the plugin:' | |
echo "cd $DIR && make dist" | |
echo "sudo chown mattermost:mattermost dist/$PLUGIN_ID-*.tar.gz" | |
echo "mmctl plugin delete $PLUGIN_ID # if already installed" | |
echo "mmctl plugin add $DIR/dist/$PLUGIN_ID-*.tar.gz" | |
echo "mmctl plugin enable $PLUGIN_ID" | |
fi | |
# NOTE | |
# If `go build` complains about broken dependencies, make sure, the plugin uses a module | |
# aware version of mattermost-server | |
# cf. https://github.com/mattermost/mattermost-plugin-starter-template/blob/master/go.mod | |
# Installing with plain mattermost command (without mmctl): | |
# cd /opt/mattermost | |
# sudo -u mattermost ./bin/mattermost plugin delete $PLUGIN_ID | |
# sudo -u mattermost ./bin/mattermost plugin add $DIR/dist/$PLUGIN_ID-*.tar.gz | |
# sudo -u mattermost ./bin/mattermost plugin enable $PLUGIN_ID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment