-
Notifications
You must be signed in to change notification settings - Fork 1
/
thanks.sh
107 lines (97 loc) · 2.31 KB
/
thanks.sh
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
#!/bin/sh
set -e
[[ -z "$INGEST_KEY" ]] && echo "INGEST_KEY not set" && exit 1
# Default settings
API_URL=${API_URL:-https://api.thanks.dev/v1/ingest}
ENTITY=""
REPO=""
FILE_PATH=""
OWN_MODULES=""
OWN_SCOPES=""
print_help() {
echo "THANKS.DEV CLI manifest uploader."
echo
echo "Usage: INGEST_KEY=<ingest-key> $0 [options]"
echo " options:"
echo " --type (gradle.dependencies,package.json) [required]"
echo " --entity <entity> [required]"
echo " --repo <repo> [required]"
echo " --path <path> [required]"
echo " --own-module <module>"
echo " --own-scope <scope>"
echo
}
main() {
TYPE=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--type)
TYPE="$2"
shift
;;
--entity)
ENTITY="$2"
shift
;;
--repo)
REPO="$2"
shift
;;
--path)
FILE_PATH="$2"
shift
;;
--own-module)
if [[ ! -z "$OWN_MODULES" ]]; then
OWN_MODULES="${OWN_MODULES},"
fi
OWN_MODULES="${OWN_MODULES}${2}"
shift
;;
--own-scope)
if [[ ! -z "$OWN_SCOPES" ]]; then
OWN_SCOPES="${OWN_SCOPES},"
fi
OWN_SCOPES="${OWN_SCOPES}${2}"
shift
;;
--help)
print_help
exit 1
;;
*)
print_help
exit 1
;;
esac
shift
done
[[ -z "$TYPE" ]] && print_help && exit 1
[[ -z "$ENTITY" ]] && print_help && exit 1
[[ -z "$REPO" ]] && print_help && exit 1
[[ -z "$FILE_PATH" ]] && print_help && exit 1
[[ ! -f "$FILE_PATH" ]] && print_help && exit 1
payload=$(jq \
-n \
-c \
--rawfile content "$FILE_PATH" \
--arg entity "$ENTITY" \
--arg repo "$REPO" \
--arg own "$OWN_MODULES" \
--arg type "$TYPE" \
--arg path "$FILE_PATH" \
'{version:2,entity:$entity,repo:$repo,type:$type,ownModules:($own | split(",")),path:$path,content:($content | @base64)}' \
)
tmpfile=$(mktemp "${TMPDIR:-/tmp}/_td.XXXXXX")
echo "$payload" > $tmpfile
curl \
-fsSL \
-XPOST \
-H "content-type: application/json" \
-H "INGEST-KEY: $INGEST_KEY" \
"$API_URL" \
-d "@$tmpfile"
rm "$tmpfile"
}
main "$@"