Last active
November 21, 2024 22:32
-
-
Save dotysan/50382ddb10555b2079fab4dfd92e2c7a to your computer and use it in GitHub Desktop.
Installs latest docker "desktop" buildx & compose, with support for cloud builders.
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
.env* |
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 | |
# | |
# Installs latest docker "desktop" buildx & compose, with support for cloud builders. | |
# | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
set -o xtrace | |
PS4func() { | |
local lineno="$1" | |
local i f='' | |
local c="\033[0;36m" y="\033[0;33m" n="\033[0m" | |
local d=$((${#FUNCNAME[@]}-2)) | |
if [[ $lineno == 1 ]] | |
then lineno=0 | |
fi | |
for ((i=d; i>0; i--)) | |
do printf -v f "%s%s()" "$f" "${FUNCNAME[i]}" | |
done | |
printf "$y%s:%04d$c%s$n " "${BASH_SOURCE[1]##*/}" "$lineno" "$f" | |
} | |
PS4='\r$(PS4func $LINENO)' | |
arch=$(uname -m) | |
kern=$(uname -s) | |
kern="${kern,,}" | |
main() { | |
chk_deps | |
mkdir --parents "$HOME/.docker/cli-plugins" | |
cd "$HOME/.docker" | |
#get_plugin docker/buildx buildx | |
get_plugin docker/buildx-desktop buildx | |
#get_plugin docker/compose compose | |
get_plugin docker/compose-desktop compose | |
} | |
chk_deps() { | |
if ! hash jq | |
then | |
echo 'ERROR: missing jq.' | |
echo 'To install, run this: sudo apt update && sudo apt install jq' | |
exit 1 | |
fi >&2 | |
if ! hash gh | |
then | |
echo 'ERROR: missing gh cli.' | |
echo 'To install, run this: source ~/src/sh/gh-lib.sh && ghinit && ghauth' | |
exit 1 | |
fi >&2 | |
if ! gh auth status | |
then | |
echo 'ERROR: You should create a GitHub personal access token and set GH_TOKEN.' | |
echo 'Or alternatively, you could run: gh auth login --hostname github.com' | |
exit 1 | |
fi >&2 | |
} | |
get_plugin() { | |
local repo="$1" | |
local name="$2" | |
local ver=$(gh_rel "$repo") | |
if [[ "$ver" ]] | |
then | |
if [[ "$arch" == "x86_64" ]] | |
then local url=$(get_url_x86 "$repo" "$ver") | |
elif [[ "$arch" == "aarch64" ]] | |
then local url=$(get_url_arm "$repo" "$ver") | |
else local url=$(get_url "$repo" "$ver") | |
fi | |
if [[ "$url" ]] | |
then | |
wget -m "$url" | |
basename "$url" | |
dirname "$url" | |
local path="${url#*://}" | |
chmod +x "$path" | |
ln --symbolic --force "../$path" "$HOME/.docker/cli-plugins/docker-$name" | |
fi | |
fi | |
} | |
gh_rel() { | |
local repo="$1" | |
gh release list --repo "$repo" |awk '$2=="Latest"{print$1}' | |
} | |
get_url() { | |
local repo="$1" | |
local tag="$2" | |
gh release view --repo "$repo" "$tag" --json assets --jq " | |
.assets |map(select(.name |endswith(\"$kern-$arch\"))) | |
" |jq --raw-output '.[].url' | |
} | |
get_url_x86() { | |
local repo="$1" | |
local tag="$2" | |
local url=$(get_url "$repo" "$tag") | |
if [[ -z "$url" ]] | |
then | |
arch=amd64 | |
url=$(get_url "$repo" "$tag") | |
fi | |
echo "$url" | |
} | |
get_url_arm() { | |
local repo="$1" | |
local tag="$2" | |
local url=$(get_url "$repo" "$tag") | |
if [[ -z "$url" ]] | |
then | |
arch=arm64 | |
url=$(get_url "$repo" "$tag") | |
fi | |
echo "$url" | |
} | |
main | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment