-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfigure
executable file
·173 lines (147 loc) · 4.26 KB
/
configure
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
#!/bin/sh
# Exit script on errors
set -ue
# Read arguments
show_help=
for arg in "$@"; do
case "$arg" in
"-h"|--help)
show_help=stdin
;;
*)
>&2 echo "Unrecognized argument: $arg"
show_help=stderr
break
;;
esac
done
# Display help if necessary
help() {
echo "Usage: $(basename "$0") [options]"
echo " -h, --help print this message and exit"
echo
echo "Report bugs at https://github.com/alyssais/humans/issues"
exit
}
[ "$show_help" = "stdin" ] && help
[ "$show_help" = "stderr" ] && >&2 help
# Declare config variables in case config.sh doesn't
owner=
repo=
file=
name=
email=
message=
access_token=
[ -f config.sh ] && source config.sh
check() {
$@ > /dev/null 2>&1;
local ret="$?"
if [ "$ret" = 0 ]; then
echo "\033[32;32mok\033[0m" # ok
else
echo "\033[31;31merror\033[0m"
fi
return $ret
}
while :; do
while :; do
printf "GitHub repository owner: "
[ -n "$owner" ] && printf "($owner) "
read -r i_owner
[ -z "$i_owner" ] && i_owner="$owner"
[ -n "$i_owner" ] && break
done
while :; do
printf "GitHub repository name: "
[ -n "$repo" ] && printf "($repo) "
read -r i_repo
[ -z "$i_repo" ] && i_repo="$repo"
[ -n "$i_repo" ] && break
done
# TODO: check no output
printf "checking $i_owner/$i_repo exists... "
check curl -Isf "https://api.github.com/repos/$i_owner/$i_repo" && break
# Invalid repository specified
read -rp "Is $i_owner/$i_repo definitely correct? (y/N) " yn
if [ "$yn" = y ] || [ "$yn" = Y ]; then
owner="$i_owner"
repo="$i_repo"
read -rp "Is $i_owner/$i_repo a private repository? (y/N) " yn
[ "$yn" != y ] && [ "$yn" != Y ] && break
if [ -n "$access_token" ]; then
echo "An API token is already set:"
echo " $access_token"
read -rp "Should this token be replaced? (y/N) " yn
[ "$yn" != y ] && [ "$yn" != Y ] && break
fi
# Repository is private
while :; do
echo "humans needs to be authorized to access $i_owner/$i_repo."
echo "Please create a token with 'repo' permissions here:"
echo " \033[4;34mhttps://github.com/settings/tokens/new\033[0m"
echo "Alternatively, hit return without entering anything to have humans" \
"create one for you."
printf "GitHub API token: "
read -r i_access_token
[ -n "$i_access_token" ] && break
# Generate an API token
read -rp "Your GitHub username: ($i_owner) " i_auth_user
read -rsp "Your GitHub password: " i_auth_password
echo # output blank line after password
[ "$i_auth_user" ] || i_auth_user="$i_owner"
github() {
curl -s https://api.github.com/$1 \
-u "$i_auth_user:$i_auth_password" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.github.v3+json" \
$(shift 1; echo $@)
}
response=$(github authorizations -X POST -d @- <<-JSON
{
"scopes": [
"repo"
],
"note": "humans@$(hostname)$(pwd)",
"note_url": "https://github.com/alyssais/humans"
}
JSON
)
if echo "$response" | grep '"token"'; then
echo "$response" | jq -r .token | read i_access_token
else
printf "\033[31;31mError: \033[0m"
echo "$response" | jq -r .message
echo "You'll need to create a GitHub API token manually."
fi
[ -n "$i_access_token" ] && break
done
[ -n "$i_access_token" ] && access_token="$i_access_token"
break
fi
done
[ -z "$file" ] && file=CONTRIBUTORS
printf "Contributors file path: ($file) "
read i_file
[ -n "$i_file" ] && file="$i_file"
[ -z "$name" ] && name="A Friendly Bot"
printf "Bot name: ($name) "
read i_name
[ -n "$i_name" ] && name="$i_name"
[ -z "$email" ] &&
email="$(echo "$name" | sed "s/ /./g" | tr '[:upper:]' '[:lower:]')@example"
printf "Bot email: ($email) "
read i_email
[ -n "$i_email" ] && email="$i_email"
[ -z "$message" ] && message="Update $file"
printf "Commit message: ($message) "
read i_message
[ -n "$i_message" ] && message="$i_message"
printf "owner=%q\n" "$owner" > config.sh
printf "repo=%q\n" "$repo" >> config.sh
printf "file=%q\n" "$file" >> config.sh
printf "name=%q\n" "$name" >> config.sh
printf "email=%q\n" "$email" >> config.sh
printf "message=%q\n" "$message" >> config.sh
printf "access_token=%q\n" "$access_token" >> config.sh
echo "Successfully wrote configuration to config.sh"