Skip to content

Instantly share code, notes, and snippets.

@moqmar
Last active October 2, 2019 18:08
Show Gist options
  • Save moqmar/ba77bd778e6ebc956eaa36388be3fcdd to your computer and use it in GitHub Desktop.
Save moqmar/ba77bd778e6ebc956eaa36388be3fcdd to your computer and use it in GitHub Desktop.
Caddy build script
#!/bin/bash
#
# Build script for the Git version of https://caddyserver.com
#
# Requirements:
# - Go
# - jq (if using names instead of import paths for plugins)
# Usage:
# curl https://gist.githubusercontent.com/moqmar/ba77bd778e6ebc956eaa36388be3fcdd/raw | bash -s http.realip http.ipfilter http.cors http.expires http.ratelimit #[...]
set -euo pipefail
IFS=$'\n\t'
echo -e "\e[1;33mCleaning up...\e[0m"
mkdir -p /tmp/caddy-build
cd /tmp/caddy-build
rm -rf gopath caddy
echo -e "\e[1;33mDownloading caddy...\e[0m"
mkdir -p gopath
export GOPATH=$PWD/gopath
export CGO_ENABLED=0
go get github.com/mholt/caddy/caddy
go get github.com/caddyserver/builds
cd gopath/src/github.com/mholt/caddy/caddy
tag=$(git describe --abbrev=0 --tags)
echo -e "\e[1;34mChecking out $tag...\e[0m"
git -c advice.detachedHead=false checkout "$tag"
echo -e "\e[1;33mAdding plugins...\e[0m"
while [ -n "${1+x}" ]; do
# With slash: custom import path
echo "$1" | grep / >/dev/null && plugin="$1" || {
# Without slash: lookup in repository
if [ -z "${repo+x}" ]; then
wget https://caddyserver.com/api/download-page -qO plugins.json
repo="$(jq -c '.plugins[] | { name: .Name, import: .ImportPath }' plugins.json)"
fi
plugin="$(echo "$repo" | grep '[{]"name":"'"$1"'"' | sed 's|^.*"import":"\(.*\)"}|\1|')" || {
echo -e "\e[1;31mError: The plugin $1 could not be found in the repository.\e[0m"
exit 1
}
}
echo -e "\e[1;34m + $plugin\e[0m"
sed -i 's|// This is where other plugins get plugged in (imported)|\0\n\t_ "'"$plugin"'"|' caddymain/run.go
shift
done
rm -f plugins.json
echo -e "\e[1;33mDownloading plugins...\e[0m"
go get
echo -e "\e[1;33mBuilding caddy...\e[0m"
go run build.go
mv caddy ../../../../../../caddy
echo -e "\e[1;33mCleaning up...\e[0m"
cd ../../../../../..
rm -rf gopath
sudo=
[[ `id -u` -eq 0 ]] || { which sudo >/dev/null && sudo=sudo; } || { sudo=false; }
echo -e "\e[1;33mInstalling to \e[0;1m/usr/local/bin/\e[1;33m...\e[0m"
$sudo cp caddy /usr/local/bin/ || {
echo -e "\e[1;32mCaddy is now built, but hasn't been installed yet![0m"
echo -e "\e[1;34mInstall using \e[0;1mcp /tmp/caddy-build/caddy /usr/local/bin/\e[0m"
exit 0
}
echo -e "\e[1;32mCaddy is now installed! \o/\e[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment