diff --git a/hack/generate-swagger-models.sh b/hack/generate-swagger-models.sh index 72d27c5f7..2db607bbf 100755 --- a/hack/generate-swagger-models.sh +++ b/hack/generate-swagger-models.sh @@ -4,11 +4,60 @@ set -o nounset set -o errexit set -o pipefail +# Get the absolute path of this file +DIR="$( cd "$( dirname "$0" )" && pwd )" + # Here is details of the swagger binary we use. # $ swagger version # version: 0.19.0 +SWAGGER_VERSION=v0.19.0 + +. ./env.sh + +# swagger::check_version checks the command and the version. +swagger::check_version() { + local has_installed version + + has_installed="$(command -v swagger || echo false)" + if [[ "${has_installed}" = "false" ]]; then + echo false + return + fi + + version="$(swagger version | head -n 1 | cut -d " " -f 2)" + if [[ "${SWAGGER_VERSION}" != "${version}" ]]; then + echo false + return + fi + echo true +} + +# swagger::install installs the swagger binary. +swagger::install() { + has_installed="$(swagger::check_version)" + if [[ "${has_installed}" = "true" ]]; then + echo "swagger-${SWAGGER_VERSION} has been installed." + return + fi + + echo ">>>> start to install swagger-${SWAGGER_VERSION} <<<<" + + local url + url="https://github.com/go-swagger/go-swagger/releases/download/${SWAGGER_VERSION}/swagger_${GOOS}_${GOARCH}" + wget --quiet -O /usr/local/bin/swagger "${url}" + chmod +x /usr/local/bin/swagger +} + +# swagger::generate generate the code by go-swagger. +swagger::generate() { + swagger generate model -f "$DIR/../apis/swagger.yml" -t "$DIR/../apis" -m types +} + +main() { + swagger::install + swagger::generate +} + +main -# Get the absolute path of this file -DIR="$( cd "$( dirname "$0" )" && pwd )" -swagger generate model -f "$DIR/../apis/swagger.yml" -t "$DIR/../apis" -m types