|
| 1 | +const SWAGGER_UI_IMAGE = "swaggerapi/swagger-ui" |
| 2 | + |
| 3 | +docker_cmd(; use_sudo::Bool=false) = use_sudo ? `sudo docker` : `docker` |
| 4 | + |
| 5 | +function stop_swagger_ui(; use_sudo::Bool=false) |
| 6 | + docker = docker_cmd(; use_sudo=use_sudo) |
| 7 | + find_cmd = `$docker ps -a -q -f ancestor=$SWAGGER_UI_IMAGE` |
| 8 | + container_id = strip(String(read(find_cmd))) |
| 9 | + |
| 10 | + if !isempty(container_id) |
| 11 | + stop_cmd = `$docker stop $container_id` |
| 12 | + stop_res = strip(String(read(stop_cmd))) |
| 13 | + |
| 14 | + if stop_res == container_id |
| 15 | + @debug("Stopped Swagger UI container") |
| 16 | + elseif isempty(stop_res) |
| 17 | + @debug("Swagger UI container not running") |
| 18 | + else |
| 19 | + @error("Failed to stop Swagger UI container: $stop_res") |
| 20 | + return false |
| 21 | + end |
| 22 | + |
| 23 | + container_id = strip(String(read(find_cmd))) |
| 24 | + if !isempty(container_id) |
| 25 | + rm_cmd = `$docker rm $container_id` |
| 26 | + rm_res = strip(String(read(rm_cmd))) |
| 27 | + |
| 28 | + if rm_res == container_id |
| 29 | + @debug("Removed Swagger UI container") |
| 30 | + elseif isempty(rm_res) |
| 31 | + @debug("Swagger UI container not found") |
| 32 | + else |
| 33 | + @error("Failed to remove Swagger UI container: $rm_res") |
| 34 | + return false |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + return true |
| 39 | + else |
| 40 | + @debug("Swagger UI container not found") |
| 41 | + end |
| 42 | + |
| 43 | + return false |
| 44 | +end |
| 45 | + |
| 46 | +function swagger_ui(spec::String; port::Int=8080, use_sudo::Bool=false) |
| 47 | + docker = docker_cmd(; use_sudo=use_sudo) |
| 48 | + stop_swagger_ui(; use_sudo=use_sudo) |
| 49 | + cmd = `$docker run -d --rm -p $port:8080 -e SWAGGER_JSON=/tmp/spec.json -v $spec:/tmp/spec.json $SWAGGER_UI_IMAGE` |
| 50 | + run(cmd) |
| 51 | + return "http://localhost:$port" |
| 52 | +end |
0 commit comments