util: largely superficial tweaking

This commit is contained in:
Connor Olding 2022-10-04 05:59:38 -07:00
parent 8653d6dd54
commit 9a79aeba8f
2 changed files with 64 additions and 65 deletions

View File

@ -1,73 +1,71 @@
#!/usr/bin/env sh
: \
&& 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 ] || [ "$from" = sourcehut ] \
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 ;} ;}
argue()
{ : \
&& for arg in "$@" \
;do : \
&& rhs="${arg#*=}" \
&& a="${arg%"$rhs"}" \
&& b="${a%=}" \
&& if [ ${#a} = ${#b} ] \
|| [ ${#b} = 0 ] \
;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" \
&& pathy="$repo/archive/$remote_fn" \
&& case "$from" in
(github) remote_url="https://github.com/$pathy" ;; \
(sourcehut) remote_url="https://git.sr.ht/~$pathy" ;; \
esac \
&& sha256="$checksum" \
&& { [ "${#dest}" != 0 ] || dest="${repo##*/}" ;} \
;else : \
&& die "unknown value specified for \"from\": $from" \
&& 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 ] || [ "$from" = sourcehut ] \
;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" \
&& pathy="$repo/archive/$remote_fn" \
&& case "$from" in
(github) remote_url="https://github.com/$pathy" ;; \
(sourcehut) remote_url="https://git.sr.ht/~$pathy" ;; \
esac \
&& sha256="$checksum" \
&& { [ "${#dest}" != 0 ] || dest="${repo##*/}" ;} \
;else : \
&& die "unknown value specified for \"from\": $from" \
;fi \
\
|| exit \
;}
ok=1 from= repo= commit= checksum=
: \
&& { [ "$#" == 0 ] || argue "$@" ;} \
&& { [ -d "/tmp" ] || die '/tmp must be mounted (or just exist)' ;} \
&& missing variable dest "$dest" \
&& missing variable local_fn "$local_fn" \

View File

@ -18,10 +18,11 @@ read -r program <<'EOF'
BEGIN{if(!D){print U;exit 64}E=(Q="'")"\"'\""Q;H=(G=".")(F="/");I=" -- "Q;J="[^/]*/";sub(F"?$",F,D)gsub(Q,E,D)}function C(x){if(X=system(x))exit X}{if((p=$2)==B){print V;exit 65}for(r=z=i=d=B;d!=!i;i++){b=a=p;d=sub("/.*",B,a);sub(J,B,p);if(a==G G)e=!sub(J,B,z);else z=a==G?z:b!=a?z a F:z a;if(e||!i&&!a){print W;exit 65}}P=p=!z?H:z;if((o=O[$1])==p)next;gsub(Q,E,P);M=m=D P;gsub("/[^/]+$",B,M);if(!u[M]++)C("mkdir -p"I M Q);if(o){T=split(o,t,F);S=split(p,s,F);for(i=1;t[i]==s[i];i++);for(j=i;j<S;j++)r=r G H;for(j=i;j<T;j++)r=r t[j]F;r=h?o:r t[T];gsub(Q,E,r);C("ln"(h?I D:" -s"I)r"' '"m Q);next}O[$1]=p;C("cp -i"I P"' '"m Q)}
EOF
die() { printf '\033[1mdedupe:\033[m %s\n' >&2 "$2"; exit "$1" ;}
h=0
: \
&& die() { printf '\033[1mdedupe:\033[m %s\n' >&2 "$2"; exit "$1" ;} \
&& while o="${1#-}" && [ "${#o}" != "${#1}" ] && shift \
;do case "$o" in \
(-) break ;; \