-
Notifications
You must be signed in to change notification settings - Fork 0
/
getraw
executable file
·174 lines (142 loc) · 2.76 KB
/
getraw
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
set -e
usage() {
echo "Usage:"
echo " getraw [options] <username>/<repository>/[<path>/]<file>"
echo ""
echo "Options:"
echo " -b, --branch <branch> Use <branch> instead of default"
echo " -d, --develop Use develop branch instead of default branch"
echo " --dry-run Only prints the curl command line and exits"
echo " -h, --help Display this help message"
echo " -p, --provider=<provider> Use SCM <provider>, default is github. Supported"
echo " providers are chisel, gitea, github, gitlab, bitbucket"
echo " -x, --execute instead of printing to stdout, directly execute the file"
exit 1
}
build_cmdline() {
case "$PROVIDER" in
chisel)
CMDLINE="$CMD https://chiselapp.com/user/$USER/repository/$REPO/raw?ci=$BRANCH&filename=$FILE"
;;
gitea*)
CMDLINE="$CMD https://$PROVIDER/$USER/$REPO/branch/$BRANCH/$FILE"
;;
github)
CMDLINE="$CMD https://github.com/$USER/$REPO/raw/$BRANCH/$FILE"
;;
gitlab)
CMDLINE="$CMD https://gitlab.com/$USER/$REPO/-/blob/$BRANCH/$FILE"
;;
bitbucket)
CMDLINE="$CMD https://bitbucket.org/$USER/$REPO/raw/$BRANCH/$FILE"
;;
*)
echo "Unknown provider $PROVIDER"
exit 1
;;
esac
}
parse_args() {
USAGE=0
PROVIDER="github"
DRY_RUN=0
EXECUTE=0
while [ $# -gt 0 ]; do
case "$1" in
--help | -h)
USAGE=1
;;
--branch=*)
BRANCH="${1#*=}"
;;
-b | --branch)
shift
BRANCH="$1"
;;
--provider=*)
PROVIDER="${1#*=}"
;;
-p | --provider)
shift
PROVIDER="$1"
;;
--develop | -d)
BRANCH="develop"
;;
--dry-run)
DRY_RUN=1
;;
--execute | -x)
EXECUTE=1
;;
*)
if [ -z "$REPO" ]; then
USER=$(echo "$1" | cut -d/ -f1)
REPO=$(echo "$1" | cut -d/ -f2)
FILE=$(echo "$1" | cut -d/ -f3-)
else
echo "to many arguments: $1"
USAGE=1
fi
;;
esac
shift
done
if [ -z "$BRANCH" ]; then
case "$PROVIDER" in
chisel)
BRANCH="trunk"
;;
gitea)
BRANCH="master"
;;
*)
BRANCH="HEAD"
;;
esac
fi
if [ $USAGE -eq 1 ]; then
usage
exit
fi
if [ -z "$REPO" ] || [ -z "$FILE" ]; then
echo "Missing arguments!"
usage
exit
fi
}
determine_download_tool() {
if [ -n "$(command -v curl)" ]; then
CMD="curl -fsSL"
elif [ -n "$(command -v wget)" ]; then
CMD="wget -O- -q"
else
echo "no downloader found! Please install either wget or curl." 1>&2
exit 1
fi
}
execute_remote_file() {
FILE=$(mktemp)
$CMDLINE >"$FILE"
chmod +x "$FILE"
$FILE
rm "$FILE"
}
execute() {
echo "$CMDLINE" >&2
if [ $DRY_RUN -eq 1 ]; then
: # do nothing on dry-run
elif [ $EXECUTE -eq 1 ]; then
execute_remote_file
else
$CMDLINE
fi
}
main() {
parse_args "$@"
determine_download_tool
build_cmdline
execute
}
main "$@"