-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_spec.sh
More file actions
executable file
·83 lines (76 loc) · 3.24 KB
/
Copy pathgen_spec.sh
File metadata and controls
executable file
·83 lines (76 loc) · 3.24 KB
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
#!/bin/bash
PrgName=`basename $0`
# Determine absolute path to location from which we are running.
export RUN_DIR=`pwd`
export PRG_RELPATH=`dirname $0`
cd $PRG_RELPATH/.
export PRG_PATH=`pwd`
cd $RUN_DIR
MODULE_NAME=${1}
SPEC_PATH=${2}
OUTPUT_FORMAT=${3:-"yaml"} # default is "yaml"; can be "yaml" or "json"
if [ -z "$MODULE_NAME" ] || [ -z "$SPEC_PATH" ]; then
echo "Usage: $PrgName <module_name> <spec_path> [output_format]"
echo " <module_name> Name of the module (required)"
echo " <spec_path> Path or URL to OpenAPI spec (required)"
echo " [output_format] Output format: yaml or json (default: yaml)"
exit 1
fi
export OUTPUT_DIR=$PRG_PATH/${MODULE_NAME}-client/target
mkdir -p $OUTPUT_DIR
TMP_DIR=$(mktemp -d)
mkdir -p $TMP_DIR/${MODULE_NAME}
INPUT_FORMAT=${SPEC_PATH##*.}
TMP_SPEC_FILE=$TMP_DIR/${MODULE_NAME}/openapi_v3.${INPUT_FORMAT}
OUTPUT_API_NAME=openapi_v3.${OUTPUT_FORMAT}
# if specPath starts with 'http' then download it, if starts with 'file://' then copy it, else exit with error
if [[ $SPEC_PATH == http* ]]; then
echo "Spec path is a URL, will download it: $SPEC_PATH"
# Download latest openapi spec from repo
# Dev yaml
curl -o $TMP_SPEC_FILE $SPEC_PATH
elif [[ $SPEC_PATH == file://* ]]; then
echo "Spec path is a file URL, will copy it: $SPEC_PATH"
SPEC_PATH=$(echo $SPEC_PATH | sed -e 's|^file://||')
cp -f $SPEC_PATH $TMP_SPEC_FILE
else
# exit with error
echo "Spec path is not a valid URL or file path: $SPEC_PATH"
echo "Proceed as taking in a bare local file path"
if [ -f "$SPEC_PATH" ]; then
cp -f $SPEC_PATH $TMP_SPEC_FILE
echo "Copied raw local file to $TMP_SPEC_FILE"
else
echo "Error: Local file does not exist: $SPEC_PATH"
exit 1
fi
fi
# Convert to desired output format if needed
if [[ ($INPUT_FORMAT == "yaml" || $INPUT_FORMAT == "yml") && $OUTPUT_FORMAT == "json" ]]; then
echo "Converting from $INPUT_FORMAT to $OUTPUT_FORMAT using swagger-cli..."
set -xv
docker run --rm -v "$TMP_SPEC_FILE:/swagger-api/input/openapi_v3.$INPUT_FORMAT" \
tapis/swagger-cli bundle -r "/swagger-api/input/openapi_v3.$INPUT_FORMAT" > "$TMP_DIR/${MODULE_NAME}/$OUTPUT_API_NAME"
cp "$TMP_DIR/${MODULE_NAME}/$OUTPUT_API_NAME" "$OUTPUT_DIR/$OUTPUT_API_NAME"
# Copy original yml file to release/openapi_specs
cp "$TMP_SPEC_FILE" ../release/openapi_specs/${MODULE_NAME}
rm -rf "$TMP_DIR"
exit 0
elif [[ $INPUT_FORMAT == "json" && ($OUTPUT_FORMAT == "yaml" || $OUTPUT_FORMAT == "yml") ]]; then
echo "Converting from $INPUT_FORMAT to $OUTPUT_FORMAT using swagger-cli..."
set -xv
docker run --rm -v "$TMP_SPEC_FILE:/swagger-api/input/openapi_v3.$INPUT_FORMAT" \
tapis/swagger-cli bundle -r "/swagger-api/input/openapi_v3.$INPUT_FORMAT" --type yaml > "$TMP_DIR/${MODULE_NAME}/$OUTPUT_API_NAME"
cp "$TMP_DIR/${MODULE_NAME}/$OUTPUT_API_NAME" "$OUTPUT_DIR/$OUTPUT_API_NAME"
rm -rf "$TMP_DIR"
exit 0
else
echo "No conversion needed, just copy it to output dir"
cp -f $TMP_SPEC_FILE "$OUTPUT_DIR/$OUTPUT_API_NAME"
rm -rf "$TMP_DIR"
exit 0
fi
# If we reach here, the conversion is not supported
echo "Error: Unsupported conversion from $INPUT_FORMAT to $OUTPUT_FORMAT"
echo "Supported conversions: yaml<->json, yml<->json"
exit 1