-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathredacteur
executable file
·221 lines (199 loc) · 4.13 KB
/
redacteur
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
api_url="$JOURNALIST_API_URL"
depcheck() {
dep="$1"
if ! type "$dep" > /dev/null
then
printf "%s missing, please install\n" "$dep"
exit 1
fi
}
usage() {
printf "Redacteur, the Journalist API client\n"
printf "https://github.com/mrusme/journalist\n"
printf "\n"
printf "usage: %s <subcommand> [args...]\n" "$0"
printf "\n"
printf "SUBCOMMANDS\n"
printf "\n"
printf " perform: Perform raw API call\n"
printf " args:\n"
printf " <action> on <endpoint> as <username> <password> "
printf "[with <payload>]\n"
printf "\n"
printf " action: get, post, put, delete\n"
printf " endpoint: users[/<id>], tokens[/<id>], feeds[/<id>]\n"
printf " payload: JSON string\n"
printf "\n"
printf " env: JOURNALIST_API_URL\n"
printf "\n"
printf " EXAMPLES\n"
printf "\n"
printf " %s perform get \\ \n" "$0"
printf " on users/d83807a0-22ec-4c9f-94bf-fee5cf882d6e\\ \n"
printf " as admin \$(pass show journalist/admin)\n"
printf "\n"
printf " %s perform post \\ \n" "$0"
printf " on tokens \\ \n"
printf " as myuser mypassword \\ \n"
printf " with '{ \"name\": \"mytoken\" }'\n"
printf "\n"
printf " add: Helper for adding users, tokens, feeds, etc.\n"
printf " args:\n"
printf " <entity>\n"
printf "\n"
printf " entity: user, token, feed\n"
printf "\n"
printf " env: JOURNALIST_API_URL, JOURNALIST_API_USERNAME,\n"
printf " JOURNALIST_API_PASSWORD\n"
printf "\n"
printf " EXAMPLES\n"
printf "\n"
printf " %s add user\n" "$0"
printf "\n"
printf " %s add token\n" "$0"
printf "\n"
printf "ENVIRONMENT\n"
printf "\n"
printf " JOURNALIST_API_URL: Journalist API endpoint, "
printf "e.g. http://127.0.0.1:8000/api\n"
printf " JOURNALIST_API_USERNAME: user to use for API requests\n"
printf " JOURNALIST_API_PASSWORD: password for API user\n"
printf "\n"
}
perform() {
if [ "$#" -lt 6 ]
then
usage
exit 1
fi
action=$(printf "%s" "$1" | tr '[:lower:]' '[:upper:]')
# "on"=$2
endpoint="$3"
# "as"=$4
user="$5"
pass="$6"
# "with"=$7
payload="$8"
if [ "$payload" = "" ]
then
json=$(curl \
-s \
-u "$user:$pass" \
-H 'Content-Type: application/json; charset=utf-8' \
-X "$action" \
"$api_url/$endpoint")
else
json=$(curl \
-s \
-u "$user:$pass" \
-H 'Content-Type: application/json; charset=utf-8' \
-X "$action" \
"$api_url/$endpoint" \
-d "$payload")
fi
if [ $? -ne 0 ]
then
printf "{}"
return 1
fi
printf "%s" "$json"
if printf "%s" "$json" | jq '.success' | grep "false" > /dev/null
then
return 2
fi
return 0
}
add() {
if [ "$#" -lt 1 ]
then
usage
exit 1
fi
case "$1" in
"user")
add_user
;;
"token")
add_token
;;
"feed")
add_feed
;;
esac
}
add_user() {
printf "Username: "
read -r username
printf "Password: "
read -r password
printf "Role (admin/[user]): "
read -r role
if [ "$role" = "" ]
then
role="user"
fi
perform post \
on users \
as "$JOURNALIST_API_USERNAME" "$JOURNALIST_API_PASSWORD" \
with "{
\"username\": \"$username\",
\"password\": \"$password\",
\"role\": \"$role\"
}"
exit $?
}
add_token() {
printf "Token name: "
read -r tokenname
perform post \
on tokens \
as "$JOURNALIST_API_USERNAME" "$JOURNALIST_API_PASSWORD" \
with "{
\"name\": \"$tokenname\"
}"
exit $?
}
add_feed() {
printf "URL: "
read -r url
printf "Name: "
read -r feedname
printf "Group: "
read -r group
perform post \
on feeds \
as "$JOURNALIST_API_USERNAME" "$JOURNALIST_API_PASSWORD" \
with "{
\"name\": \"$feedname\",
\"url\": \"$url\",
\"group\": \"$group\"
}"
exit $?
}
depcheck curl
depcheck jq
if [ "$#" -lt 1 ]
then
usage
exit 1
fi
subcommand="$1"
case "$subcommand" in
"perform")
perform "${@:2}"
exit $?
;;
"add")
add "${@:2}"
exit $?
;;
"help")
usage
exit 0
;;
*)
usage
exit 1
;;
esac