From 0f477ca79d571f80226c38d681ec3dae3883647f Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Mon, 19 Apr 2021 04:30:22 +0000 Subject: [PATCH] Replicate the GitHub script interface for convenience --- gitlab.com-list-repos | 75 +++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/gitlab.com-list-repos b/gitlab.com-list-repos index 454ae37..05ff821 100755 --- a/gitlab.com-list-repos +++ b/gitlab.com-list-repos @@ -1,40 +1,59 @@ #!/bin/bash set -euo pipefail -if [[ $# -ne 1 || "$1" == '-h' || "$1" == '--help' ]] +GIT_URLS_MODE='--git-urls' +MODES=("${GIT_URLS_MODE}") + +if [[ $# -lt 1 || ( $# -ge 2 && "$1" == --* && ! " ${MODES[@]} " =~ " ${1} " ) || "$1" == '-h' || "$1" == '--help' ]] then - echo "Usage: $0 USER_OR_GROUP_NAME" >&2 + echo "Usage: $0 [$(printf " | %s" "${MODES[@]}" | sed 's,^ | ,,')] USER_OR_GROUP_NAME [USER_OR_GROUP_NAME...]" >&2 exit 1 fi -name="$1" +mode='' +if [[ $# -ge 2 && "$1" == --* ]] +then + mode="$1" + shift +fi scriptpath="$(cd "$(dirname "$0")"; pwd -P)" -page="$("${scriptpath}/curl-ua" firefox -s "https://gitlab.com/${name}")" -if grep -q -F '&2 - url="https://gitlab.com/api/v4/users/${name}/projects?per_page=100" -elif grep -q -F '
  • &2 - url="https://gitlab.com/api/v4/groups/${name}/projects?per_page=100&include_subgroups=true" -else - echo "Error: unknown page type" >&2 - exit 1 -fi +for name +do + page="$("${scriptpath}/curl-ua" firefox -s "https://gitlab.com/${name}")" + if grep -q -F '&2 + url="https://gitlab.com/api/v4/users/${name}/projects?per_page=100" + elif grep -q -F '
  • &2 + url="https://gitlab.com/api/v4/groups/${name}/projects?per_page=100&include_subgroups=true" + else + echo "Error: unknown page type for ${name}" >&2 + exit 1 + fi -{ - pipe=$(mktemp -u); mkfifo "${pipe}"; exec 3<>"${pipe}"; rm "${pipe}"; unset pipe # For the disgusting HTTP header treatment - "${scriptpath}/curl-ua" firefox -sv "${url}" 2> >(grep '^< x-total-pages: ' | sed 's,^.*: ,,' | tr -d '\r' >&3) - declare -i nPages=$(head -1 <&3) - if [[ ${nPages} -ge 2 ]] + { + pipe=$(mktemp -u); mkfifo "${pipe}"; exec 3<>"${pipe}"; rm "${pipe}"; unset pipe # For the disgusting HTTP header treatment + "${scriptpath}/curl-ua" firefox -sv "${url}" 2> >(grep '^< x-total-pages: ' | sed 's,^.*: ,,' | tr -d '\r' >&3) + declare -i nPages=$(head -1 <&3) + if [[ ${nPages} -ge 2 ]] + then + for ((page=2; page<=${nPages}; ++page)) + do + echo -n $'\r'"Requesting page ${page} of ${nPages}" >&2 + "${scriptpath}/curl-ua" firefox -s "${url}&page=${page}" + done + echo >&2 + fi + } +done | grep -Po '"path_with_namespace":\s*"\K[^"]+' | sed 's,^,https://gitlab.com/,' | { + if [[ -z "${mode}" ]] + then + cat + elif [[ "${mode}" == "${GIT_URLS_MODE}" ]] then - for ((page=2; page<=${nPages}; ++page)) - do - echo -n $'\r'"Requesting page ${page} of ${nPages}" >&2 - "${scriptpath}/curl-ua" firefox -s "${url}&page=${page}" - done - echo >&2 + awk '{ print $0 ".git"; print $0 ".wiki.git"; }' fi -} | grep -Po '"path_with_namespace":\s*"\K[^"]+' | sed 's,^,https://gitlab.com/,' +}