diff --git a/notwa-util/Dockerfile b/notwa-util/Dockerfile index 7fb3928..05c7621 100644 --- a/notwa-util/Dockerfile +++ b/notwa-util/Dockerfile @@ -6,6 +6,7 @@ RUN apk add --no-cache xz COPY --chmod=0755 acquire cosmocc dedupe quickconf shed \ /usr/local/bin/ RUN ln -s /usr/local/bin /nu +WORKDIR /media/common FROM partial as downloader # this demonstrates how to use the `acquire` script. @@ -13,9 +14,8 @@ ENV BUSYBOX_VERSION=1.35.0 ENV BUSYBOX_SHA256=faeeb244c35a348a334f4a59e44626ee870fb07b6884d68c10ae8bc19f83a694 RUN --mount=type=cache,id=common,target=/media/common,sharing=locked \ --mount=type=tmpfs,target=/tmp : \ - && cd /media/common \ && name=busybox \ - && export remote_fn="$name-$BUSYBOX_VERSION.tar.bz2" \ + && remote_fn="$name-$BUSYBOX_VERSION.tar.bz2" \ && export local_fn="$remote_fn" \ && export remote_url="http://busybox.net/downloads/$remote_fn" \ && export dest=/root/$name \ diff --git a/notwa-util/acquire b/notwa-util/acquire index 6e250c6..97d6e83 100755 --- a/notwa-util/acquire +++ b/notwa-util/acquire @@ -1,17 +1,75 @@ #!/usr/bin/env sh : \ - && die() \ - { : \ - && printf '\033[1m%s:\033[m %s\n' acquire "$@" \ - && exit 64 \ - ;} \ + && warn() { printf >&2 '\033[1m%s:\033[m %s\n' acquire "$*" ;} \ + && die() { warn "$@"; exit 64 ;} \ + && missing() +{ : \ + && [ ${#3} != 0 ] \ + || { warn "missing a required $1: $2"; ok=0 ;} \ + ;} \ + && ok=1 from= repo= commit= checksum= \ +\ + && if [ "$#" != 0 ] \ + ;then : \ + && for arg in "$@" \ + ;do : \ + && rhs="${arg#*=}" \ + && a="${arg%"$rhs"}" \ + && b="${a%=}" \ + && if [ ${#a} = ${#b} ] \ + || [ ${#b} = 0 ] \ + ;then : \ + && warn "ignoring bare argument: $arg" \ + && continue \ + ;fi \ + && lhs="${b#*[!-0-9A-Z_a-z]}" \ + && if [ ${#lhs} != ${#b} ] \ + ;then : \ + && warn "ignoring invalid key: $b" \ + && warn "it became invalid at: ${b#"${b%?"$lhs"}"}" \ + && continue \ + ;fi \ + && case "$lhs" in \ + (from) from="$rhs" ;; \ + (repo) repo="$rhs" ;; \ + (dest) dest="$rhs" ;; \ + (commit) commit="$rhs" ;; \ + (checksum) checksum="$rhs" ;; \ + (*) warn "ignoring unknown key: $lhs" ;; \ + esac \ + || exit \ + ;done \ + && { [ "${#from}" != 0 ] || die "missing a required key: from" ;} \ + && if [ "$from" = github ] \ + ;then : \ + && missing key repo "$repo" \ + && missing key commit "$commit" \ + && missing key checksum "$checksum" \ + && { [ $ok = 1 ] || exit 64 ;} \ + && username="${repo%%/*}" && project="${repo##*/}" \ + && if [ ".$username/$project" != ".$repo" ] \ + ;then : \ + && warn "invalid value for key: repo=$repo" \ + && die "expected format: repo=username/project" \ + ;fi \ + && remote_fn="$commit.tar.gz" \ + && local_fn="$username!$project-$remote_fn" \ + && remote_url="https://github.com/$repo/archive/$remote_fn" \ + && sha256="$checksum" \ + && { [ "${#dest}" != 0 ] || dest="${repo##*/}" ;} \ + ;else : \ + && die "unknown value specified for \"from\": $from" \ + ;fi \ + || exit \ + ;fi \ +\ && { [ -d "/tmp" ] || die '/tmp must be mounted (or just exist)' ;} \ - && { [ -n "$dest" ] || die 'dest must be set' ;} \ - && { [ -n "$local_fn" ] || die 'local_fn must be set' ;} \ - && { [ -n "$remote_fn" ] || die 'remote_fn must be set' ;} \ - && { [ -n "$remote_url" ] || die 'remote_url must be set' ;} \ - && { [ -n "$sha256" ] || die 'sha256 must be set' ;} \ + && missing variable dest "$dest" \ + && missing variable local_fn "$local_fn" \ + && missing variable remote_url "$remote_url" \ + && missing variable sha256 "$sha256" \ + && { [ $ok = 1 ] || exit 64 ;} \ && { : \ && printf '%s %s\n' "$sha256" "$local_fn" | sha256sum -c - >/dev/null 2>&1 \ || wget -q "$remote_url" -O "$local_fn" \