-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_fetchfile_small
executable file
·77 lines (67 loc) · 2.29 KB
/
github_fetchfile_small
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
###############################################################################
## File: github_fetchfile_small
## Purpose: Shell Script to curl-download small files from github
## Notes:
## - Will use your Github CLI Token from the macOS Keychain
## - Will fail if the file is larger than 1MB as it uses the /contents/ API
## - See also: github_fetchfile
###############################################################################
set -e
while getopts ":t:o:r:b:p:O:s:" opt; do
case $opt in
t) TOKEN="$OPTARG"
;;
s) SCRIPT_NAME="$OPTARG"
;;
o) REPO_OWNER="$OPTARG"
;;
r) REPO="$OPTARG"
;;
b) BRANCH="$OPTARG"
;;
p) ASSET_PATH="$OPTARG"
;;
O) OUTPUT_PATH="$OPTARG"
;;
\?) INVALID=1; echo "Invalid Option: -$OPTARG" >&2
;;
esac
done
INVALID=${INVALID:-0}
# Set Defaults
REPO_OWNER=${REPO_OWNER:-"$GITHUB_USER"}
BRANCH=${BRANCH:-"main"}
# Used to get a unique access code for this script
SCRIPT_NAME=${SCRIPT_NAME:-"$(basename "$0")"}
# Test required params
if [[ INVALID -eq 0 ]] && [[ "$REPO_OWNER" == "" ]]; then
echo "Repository owner must be passed in, or ENV[\$GITHUB_USER] must be set!" >&2
INVALID=1
fi
TOKEN=$("$(dirname "${BASH_SOURCE[0]}" )/github_token" -r -s "$SCRIPT_NAME")
if [[ INVALID -eq 0 ]] && [[ "$TOKEN" == "" ]]; then
echo "Couldn't load Github Personal Access Token, aborting… " >&2
exit 1
fi
if [[ INVALID -eq 0 ]] && [[ "$ASSET_PATH" == "" ]]; then
echo "-p ASSET_PATH missing." >&2
INVALID=1
fi
if [[ INVALID -eq 0 ]] && [[ "$OUTPUT_PATH" == "" ]]; then
echo "-O OUTPUT_PATH missing." >&2
INVALID=1
fi
if [ $INVALID -eq 1 ]; then
echo "Usage: $0 [-t TOKEN] [-s SCRIPT_NAME] [-o REPO_OWNER] -r REPO [-b BRANCH] -p ASSET_PATH -O OUTPUT_PATH" >&2
echo " -t Github Personal Access Token with sufficient permissions. Will look at keychain if omitted." >&2
echo " -s Identifier for the Keychain item used to lookup a token for this script." >&2
echo " -b Choose what branch to fetch from. If omitted, the default branch name is 'main'." >&2
exit 1
fi
# echo "$TOKEN $REPO_OWNER $REPO $BRANCH $ASSET_PATH $OUTPUT_PATH" >&1
curl -L \
--header "Authorization: token $TOKEN" \
--header "Accept: application/vnd.github.v3.raw" \
-o "$OUTPUT_PATH" \
"https://api.github.com/repos/$REPO_OWNER/$REPO/contents/$ASSET_PATH?ref=$BRANCH"