Initial commit
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
82
scripts/list.sh
Executable file
82
scripts/list.sh
Executable file
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# shellcheck source=lib/common.sh
|
||||
source "${SCRIPT_DIR}/lib/common.sh"
|
||||
|
||||
FILTER=""
|
||||
FORMAT="table"
|
||||
|
||||
usage() {
|
||||
cat >&2 <<'EOF'
|
||||
Usage: list.sh [--private|--public] [--format table|tsv|name]
|
||||
|
||||
--private 只列出 private repo
|
||||
--public 只列出 public repo
|
||||
--format FORMAT table (default) | tsv | name
|
||||
EOF
|
||||
exit 1
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--private) FILTER="private"; shift ;;
|
||||
--public) FILTER="public"; shift ;;
|
||||
--format)
|
||||
[[ $# -ge 2 ]] || { log_err "--format 需要一個值"; exit 1; }
|
||||
FORMAT="$2"; shift 2 ;;
|
||||
-h|--help) usage ;;
|
||||
*) log_err "未知參數:$1"; usage ;;
|
||||
esac
|
||||
done
|
||||
|
||||
case "$FORMAT" in
|
||||
table|tsv|name) ;;
|
||||
*) log_err "--format 只能是 table / tsv / name"; exit 1 ;;
|
||||
esac
|
||||
|
||||
load_config
|
||||
require_cmd jq column
|
||||
|
||||
PAGE=1
|
||||
LIMIT=50
|
||||
ALL_ITEMS="$(mktemp)"
|
||||
trap 'rm -f "$ALL_ITEMS"; _cleanup_common' EXIT
|
||||
|
||||
while :; do
|
||||
gitea_api GET "/user/repos?limit=${LIMIT}&page=${PAGE}"
|
||||
if [[ "$HTTP_CODE" != "200" ]]; then
|
||||
log_err "GET /user/repos 失敗 (HTTP ${HTTP_CODE})"
|
||||
cat "$GITEA_API_RESP_FILE" >&2
|
||||
exit 1
|
||||
fi
|
||||
jq -c '.[]' < "$GITEA_API_RESP_FILE" >> "$ALL_ITEMS"
|
||||
COUNT="$(jq 'length' < "$GITEA_API_RESP_FILE")"
|
||||
[[ "$COUNT" -lt "$LIMIT" ]] && break
|
||||
PAGE=$((PAGE + 1))
|
||||
done
|
||||
|
||||
filter_jq="."
|
||||
case "$FILTER" in
|
||||
private) filter_jq='select(.private == true)' ;;
|
||||
public) filter_jq='select(.private == false)' ;;
|
||||
esac
|
||||
|
||||
TOTAL="$(jq -s "map($filter_jq) | length" < "$ALL_ITEMS")"
|
||||
log_info "抓到 ${TOTAL} 個 repo"
|
||||
|
||||
case "$FORMAT" in
|
||||
name)
|
||||
jq -r "$filter_jq | .name" < "$ALL_ITEMS"
|
||||
;;
|
||||
tsv)
|
||||
jq -r "$filter_jq | [.name, (if .private then \"private\" else \"public\" end), (.description // \"\")] | @tsv" < "$ALL_ITEMS"
|
||||
;;
|
||||
table)
|
||||
{
|
||||
printf 'NAME\tVISIBILITY\tDESCRIPTION\n'
|
||||
jq -r "$filter_jq | [.name, (if .private then \"private\" else \"public\" end), (.description // \"\")] | @tsv" < "$ALL_ITEMS"
|
||||
} | column -t -s $'\t'
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user