Skip to content

Commit b28e6fd

Browse files
committed
Better error handling for args in run_python.sh
1 parent 7bba8a5 commit b28e6fd

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

examples/docker/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
# Docker Image for Python Examples
33
---
44
### Summary
5-
To simplify the running of the Python examples, the Dockerfile will build an image and install the latest rosette-api library from composer.
5+
To simplify the running of the Python examples, the Dockerfile will build an image and install the latest rosette-api library.
66

77
### Basic Usage
88
Build the docker image, e.g. `docker build -t basistech/python:1.1 .`
99

1010
Run an example as `docker run -e API_KEY=api-key -v "path-to-example-source:/source" basistech/python:1.1`
1111

12-
To test against an alternate url, add `-e ALT_URL=alternate_url` before the `-v`
12+
To test against a specific source file, add `-e FILENAME=filename` before the `-v`
13+
14+
Also, to test against an alternate url, add `-e ALT_URL=alternate_url` before the `-v`

examples/docker/run_python.sh

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
#!/bin/bash
22

3+
#Gets called when the user doesn't provide any args
4+
function HELP {
5+
echo "usage: source_file.py --key API_KEY [--url ALT_URL]"
6+
echo " API_KEY - Rosette API key (required)"
7+
echo " FILENAME - Python source file (optional)"
8+
echo " ALT_URL - Alternate service URL (optional)"
9+
echo "Compiles and runs the source file(s) using the published rosette-api"
10+
exit 1
11+
}
12+
13+
#Gets API_KEY, FILENAME and ALT_URL if present
14+
while getopts ":API_KEY:FILENAME:ALT_URL" arg; do
15+
case "${arg}" in
16+
API_KEY)
17+
API_KEY=${OPTARG}
18+
usage
19+
;;
20+
ALT_URL)
21+
ALT_URL=${OPTARG}
22+
usage
23+
;;
24+
FILENAME)
25+
FILENAME=${OPTARG}
26+
usage
27+
;;
28+
esac
29+
done
30+
331
# reference the API
432
curl "https://api.rosette.com/rest/v1/ping" -H "user_key: $1"
533

34+
#Copy the mounted content in /source to current WORKDIR
635
cp /source/*.* .
736

8-
if [ ! -z "$2" ]; then
9-
find -maxdepth 1 -name '*.py' -print -exec tox -- {} --key $1 --url $2 \;
10-
else
11-
find -maxdepth 1 -name '*.py' -print -exec tox -- {} --key $1 \;
37+
#Run the examples
38+
if [ ! -z ${API_KEY} ]; then
39+
if [ ! -z ${FILENAME} ]; then
40+
if [ ! -z ${ALT_URL} ]; then
41+
tox -- ${FILENAME} --key ${API_KEY} --url ${ALT_URL}
42+
else
43+
tox -- ${FILENAME} --key ${API_KEY}
44+
fi
45+
elif [ ! -z ${ALT_URL} ]; then
46+
find -maxdepth 1 -name '*.py' -print -exec tox -- {} --key ${API_KEY} --url ${ALT_URL} \;
47+
else
48+
find -maxdepth 1 -name '*.py' -print -exec tox -- {} --key ${API_KEY} \;
49+
fi
50+
else
51+
HELP
1252
fi

0 commit comments

Comments
 (0)