1
0
Fork 0

add new script to clone my projects

This commit is contained in:
Luca Bilke 2024-02-12 04:14:21 +01:00
parent cd4149f0e4
commit 9ba4e72210
4 changed files with 231 additions and 21 deletions
.config/clonedev
.local/bin

78
.config/clonedev/config Executable file
View file

@ -0,0 +1,78 @@
#!/bin/sh
# TODO: Rewrite this in a proper language
# shellcheck disable=2034
set -a
BASE="$XDG_DOCUMENTS_DIR/dev"
# TOKEN has to be an API token (PAT) with
# the following permissions (for the default configuration)
# read:organization
# read:repository
# read:user
TOKEN="$(secret-tool lookup gitea api_token)"
# Store with `secret-tool store --label=clonedev gitea api_token`
# URL hit by curl is constructed like this: API_URL/TARGET/ENDPOINT
# TARGET is extracted from a space separated list stored in TARGETS
API_URL="https://git.snaile.de/api/v1"
TARGETS="orgs/snailed users/luca"
ENDPOINT="repos"
HEADERS="
-H 'accept: application/json'
-H 'Authorization: token $TOKEN'
"
# The output of this request are filtered using this jq command:
FILTER=".[].ssh_url"
# After filtering, the output passed to a git clone command with these flags:
CLONE_FLAGS="--recursive -j8"
# If the repo is listed in one of these variables, it is moved to
# the dir in the variable name. The dirname must match this regex: "^DEVDIRS_([A-Za-z_-]*)(?==)"
# The following config would result in the testrepo repo being placed like this:
# testdir/testrepo
# test_block would be blocked from getting cloned
# DEVDIRS_testdir="
# testrepo
#
# BLOCKDIRS="test_block"
DEVDIRS_desktop="
bootstrapper
dmenu-custom
dwm-custom
dwmblocks-custom
st-custom
tokyonight-icons
tokyonight-theme
"
DEVDIRS_server="
ansible-example
ansible-homelab
dotfiles-server
void-packages
void-packages-custom
qbittorrent-natpmp
xbps-builder
matrix-docker-ansible-deploy
server-resources
"
DEVDIRS_misc="
tidal-scraper
"
DEVDIRS_nvim="
taolf
"
BLOCK_UNSORTED=false
BLOCK_REPOS="dotfiles pinentry-dmenu-custom"
EXTRA_REPOS="
https://github.com/void-linux/void-packages
https://github.com/spantaleev/matrix-docker-ansible-deploy
"

View file

@ -0,0 +1,2 @@
#!/bin/sh
./xbps-src binary-bootstrap

View file

@ -0,0 +1,2 @@
#!/bin/sh
./xbps-src bootstrap-update

View file

@ -1,30 +1,158 @@
#!/bin/sh
TARGETS="${TARGETS:-"orgs:snailed users:luca"}"
API_URL="${API_URL:-"https://git.snaile.de/api/v1"}"
ENDPOINT="${ENDPOINT:-"repos"}"
FILTER="${FILTER:-".[].ssh_url"}"
COMMAND="${COMMAND:-"git clone --recursive -j8"}"
TOKEN="${TOKEN:-"$(secret-tool lookup gitea api_token)"}"
# NOTE: Store with `secret-tool store --label=clonedev gitea api_token`
# NOTE: Gitea API Token needs the following permissions:
# read:organization
# read:repository
# read:user
# TODO: Rewrite this in a proper language
set -eu
list() {
# shellcheck disable=2086
curl -s -X "GET" \
"$API_URL/$1/$2/$ENDPOINT" \
-H "accept: application/json" \
-H "Authorization: token $TOKEN" |
"$1" \
$HEADERS |
jq -r "$FILTER"
}
for target in $TARGETS; do
type=$(echo "$target" | cut -d ':' -f 1)
name=$(echo "$target" | cut -d ':' -f 2)
for repo in $(list "$type" "$name"); do
_VAR_REGEX='(?<=^DEVDIRS_)[A-Za-z_-]*(?==)'
get_path() {
reponame=$(echo "$1" | rev | cut -d '/' -f 1 | rev)
echo " $BLOCK_REPOS " | grep -q " $reponame " && return 1
for var in $(env | grep -oP "$_VAR_REGEX"); do
repos=$(eval "echo \$DEVDIRS_$var | xargs -r")
dir=$(echo "$var" | cut -d '_' -f 2)
if echo " $repos " | grep -q " $reponame "; then
return
fi
done
if ! $BLOCK_UNSORTED; then
echo "$BASE/unsorted/$reponame"
return
fi
}
warn() {
tput el
tput setaf 1
tput bold
echo "$@"
tput sgr0
}
info() {
tput el
tput setaf 4
tput bold
echo "$@"
tput sgr0
}
success() {
tput el
tput setaf 2
tput bold
echo "$@"
tput sgr0
}
scroll() {
# info "executing $*"
tput setaf 8
tput sc
while read -r line; do
tput el
echo "$line"
tput rc
done
tput sgr0
}
pull() {
repo=$1
dir=$(get_path "$repo") || {
warn "$repo blocked ✗"
return
}
[ -z "$dir" ] && return
if [ -d "$dir/.git" ]; then
git -C "$dir" status | grep -iq 'working tree clean' || {
warn "$repo unclean tree "
git -C "$dir" status | tail -n=+3 | head -n=+2
return
}
tput sc
info "$repo getting pulled 󰚭"
git -C "$dir" pull 2>&1 | scroll || {
tput rc
warn "$repo pull failed "
return
}
tput el
tput cuu1
success "$repo pulled "
target=$(echo "$repo" | rev | cut -d'/' -f1 | rev)
if [ -x "$config_dir/hooks/update/$target" ]; then
cd "$dir"
tput sc
info "Running $target update hook"
"$config_dir/hooks/update/$target" | scroll || {
warn "failed to run update hook for $target ✗"
}
tput el
tput cuu1
success "Ran $target update hook successfully ✓"
cd -
fi
return
fi
mkdir -p "$dir" || {
warn "mkdir -p $dir failed ✗"
return
}
tput sc
info "$repo getting cloned 󰚭"
# shellcheck disable=2086
echo $COMMAND "$repo"
git clone $CLONE_FLAGS "$repo" "$dir" 2>&1 | scroll || {
tput rc
warn "$repo clone failed ✗"
return
}
tput el
tput cuu1
success "$repo cloned "
target=$(echo "$repo" | rev | cut -d'/' -f1 | rev)
if [ -x "$config_dir/hooks/install/$target" ]; then
cd "$dir"
tput sc
info "Running $target install hook"
"$config_dir/hooks/install/$target" | scroll || {
warn "failed to run install hook for $target ✗"
}
tput el
tput cuu1
success "Ran $target install hook successfully ✓"
cd -
fi
return
}
# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
# ░ ░░░░ ░░░ ░░░ ░░ ░░░ ░
# ▒ ▒▒ ▒▒ ▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒ ▒
# ▓ ▓▓ ▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ▓ ▓ ▓
# █ █ █ ██ █████ █████ ██ █
# █ ████ ██ ████ ██ ██ ███ █
# ████████████████████████████████████████
# shellcheck disable=1091
. "${XDG_CONFIG_HOME:-"$HOME/.config"}/clonedev/config"
config_dir=${XDG_CONFIG_HOME:-$HOME/.config}/clonedev
# shellcheck disable=2015
mkdir -p "$BASE" && cd "$BASE" || {
warn "Failed to create/cd $BASE ✗"
}
for target in $TARGETS; do
for repo in $(list "$API_URL/$target/$ENDPOINT" | sed 's|.git$||' | xargs); do
pull "$repo"
done
done
for repo in $(echo "$EXTRA_REPOS" | xargs); do
pull "$repo"
done