Skip to content

Instantly share code, notes, and snippets.

@YooWaan
Last active August 16, 2022 00:30
Show Gist options
  • Save YooWaan/6675843f9cae844abcb133b8edf04af3 to your computer and use it in GitHub Desktop.
Save YooWaan/6675843f9cae844abcb133b8edf04af3 to your computer and use it in GitHub Desktop.
openapi json to html shell
version="4.13.2"
while getopts "f:u:s:h" OPTION
do
case ${OPTION} in
f) flag="f"
oapi=${OPTARG}
;;
u) flag="u"
oapi=${OPTARG}
;;
s) version=${OPTARG}
;;
h) printf "Usage: %s: [-f file -u url] (-s <cdn version>)\n" $(basename $0) >&2
exit 2
;;
esac
done
if [ $flag = "f" ]; then
oapi_json=$(cat $oapi | jq -c)
else
oapi_json=$(curl $oapi | jq -c)
fi
cat <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/${version}/swagger-ui.css" >
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body {
margin:0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/${version}/swagger-ui-bundle.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/${version}/swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {
var spec = ${oapi_json};
// Build a system
const ui = SwaggerUIBundle({
spec: spec,
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}
</script>
</body>
</html>
EOF

usage

gist_url=https://gist.githubusercontent.com/YooWaan/6675843f9cae844abcb133b8edf04af3/raw/409fdd36c6d9daa4f7cb4dd80ac20d3abdecf108/opa-ht.sh

# url
$ curl $gist_url | bash -s -- -u https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v2.0/json/api-with-examples.json > sample.html
# file
$ curl $gist_url | bash -s -- -f oapi.json > sample.html

## (cannot use) use short url

# url
# $ curl https://shorturl.at/mNPR8 | bash -s -- -u https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v2.0/json/api-with-examples.json > sample.html
# file
# $ curl https://shorturl.at/mNPR8 | bash -s -- -f oapi.json > sample.html

requirements

  • jq
  • curl
  • getopts
  • cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment