-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoctave-push.sh
executable file
·154 lines (123 loc) · 3.67 KB
/
doctave-push.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
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
#!/bin/bash
set -eo pipefail
VERSION="0.1.0"
if [[ -z "${DOCTAVE_HOST}" ]]; then
_DOCTAVE_HOST="https://docs.doctave.com"
else
_DOCTAVE_HOST="$DOCTAVE_HOST"
fi
HELP_TEXT="Doctave Push $VERSION
Upload your documentation to Doctave.com. Add this script to your CI to have
always up-to-date docs. This tool can be configured via environment variables,
or via command line flags.
USAGE:
doctave-push [OPTIONS] PATH
ARGS:
<PATH>:
The directory containing the doctave.yaml file.
OPTIONS:
-t --upload-token:
Upload token linked to the Doctave project. Find yours by going to
doctave.com/settings/<team>/<project>.
Not required if DOCTAVE_UPLOAD_TOKEN environment variable is set"
ZIPPED="/tmp/doctave-push-$RANDOM.zip"
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-t|--upload-token)
UPLOAD_TOKEN="$2"
shift # past argument
shift # past value
;;
-h|--help)
HELP="Y"
shift # past argument
;;
*) # unknown option
DOCS_PATH+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ "$HELP" == "Y" ]; then
echo "$HELP_TEXT"
exit 0
fi
UPLOAD_TOKEN="${UPLOAD_TOKEN:-$DOCTAVE_UPLOAD_TOKEN}"
input_error() {
echo "Error: $1."
echo "Run \`doctave-push --help\` for usage instructions"
exit 1
}
dependency_error() {
echo "Could not find runtime dependency: $1"
exit 1
}
detect_dependencies() {
if ! command -v git &> /dev/null; then
dependency_error "git"
fi
if ! command -v python &> /dev/null; then
dependency_error "python"
fi
if ! command -v zip &> /dev/null; then
dependency_error "zip"
fi
if [[ "$(python -V)" == "Python 2"* ]]; then
PYTHON_VERSION=2
else
PYTHON_VERSION=3
fi
}
validate_dir() {
if [[ ${#DOCS_PATH[@]} == 0 ]]; then
input_error "Missing path to docs directory"
fi
if [[ ${#DOCS_PATH[@]} != 1 ]]; then
input_error "More than one docs directory path passed"
fi
DOCS_PATH="${DOCS_PATH[0]}"
if ! [ -f "$DOCS_PATH/doctave.yaml" ]; then
input_error "Could not find doctave.yaml in the provided path"
fi
if ! [ -d "$DOCS_PATH/docs" ]; then
input_error "Could not find docs directory in the provided path"
fi
}
print_upload_error() {
local json
json="$1"
if ! $(echo $json | python -c 'import json,sys;obj=json.load(sys.stdin);' &> /dev/null); then
echo "Push failed: Unexpected response from server."
echo "$json"
exit 1
fi
echo "Error pushing docs to Doctave:"
if [[ $PYTHON_VERSION == 2 ]]; then
echo "$(echo $json | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["errors"][0]["detail"]')"
else
echo "$(echo $json | python -c 'import json,sys; print(json.load(sys.stdin)["errors"][0]["detail"])')"
fi
}
detect_git_info() {
GIT_SHA="$(git rev-parse HEAD)"
GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
GIT_AUTHOR="$(git show -s --format='%ae' HEAD)"
}
validate_dir
detect_dependencies
detect_git_info
zip -q -r $ZIPPED "$DOCS_PATH/doctave.yaml" "$DOCS_PATH/docs"
resp="$(curl --silent --show-error -w "|%{http_code}" -H "Authorization: Bearer $UPLOAD_TOKEN" -F "git_sha=$GIT_SHA" -F "git_branch=$GIT_BRANCH" -F "git_author=$GIT_AUTHOR" -F docs="@$ZIPPED" "$_DOCTAVE_HOST"/uploads)"
body="$( echo "$resp" | cut -d '|' -f 1 )"
http_code="$( echo "$resp" | cut -d '|' -f 2 )"
if [[ $http_code == "201" ]]; then
echo "Done! Docs uploaded to https://docs.doctave.com"
exit 0
else
print_upload_error "$body"
exit $exit_status
fi