-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from v0ctor/feature/improve-generation
Improve code generation
- Loading branch information
Showing
17 changed files
with
269 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Docker | ||
#USER_ID=1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.DS_Store | ||
.env | ||
.gobincache/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
ARG GO_VERSION=1.19.5 | ||
ARG ALPINE_VERSION=3.17 | ||
ARG GOIMPORTS_VERSION=0.5.0 | ||
ARG DELVE_VERSION=1.20.1 | ||
ARG SWAGGER_VERSION=0.30.3 | ||
|
||
|
||
## Base image | ||
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS base | ||
|
||
WORKDIR /go/src/app | ||
|
||
ENV CGO_ENABLED=0 | ||
|
||
|
||
## Development image | ||
FROM base AS development | ||
|
||
ARG GOIMPORTS_VERSION | ||
ARG DELVE_VERSION | ||
ARG SWAGGER_VERSION | ||
|
||
RUN apk add \ | ||
bash \ | ||
curl \ | ||
git \ | ||
jq \ | ||
python3 \ | ||
zsh \ | ||
&& go install golang.org/x/tools/cmd/goimports@v${GOIMPORTS_VERSION} \ | ||
&& go install github.com/go-delve/delve/cmd/dlv@v${DELVE_VERSION} \ | ||
&& go install github.com/go-swagger/go-swagger/cmd/swagger@v${SWAGGER_VERSION} | ||
|
||
ARG USER_ID=1000 | ||
ENV USER_NAME=default | ||
|
||
ENV PROMPT="%B%F{cyan}%n%f@%m:%F{yellow}%~%f %F{%(?.green.red[%?] )}>%f %b" | ||
|
||
RUN adduser -D -u $USER_ID ${USER_NAME} \ | ||
&& chown -R ${USER_NAME}: /go/pkg | ||
|
||
USER ${USER_NAME} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,40 @@ | ||
deps: | ||
go mod download | ||
go install github.com/go-swagger/go-swagger/cmd/swagger@latest | ||
# Helpers | ||
IS_DARWIN := $(filter Darwin,$(shell uname -s)) | ||
|
||
generate: deps | ||
swagger generate client --target=./netbox --copyright-file=copyright_header.txt | ||
define set_env | ||
sed $(if $(IS_DARWIN),-i "",-i) -e "s/^#*\($(1)=\).*/$(if $(2),,#)\1$(2)/" .env | ||
endef | ||
|
||
clean: | ||
rm -rf netbox/client netbox/models | ||
EXEC := docker compose exec app | ||
|
||
integration: | ||
go test ./... -tags=integration | ||
# Environment recipes | ||
.PHONY: default | ||
default: init up | ||
|
||
.PHONY: init | ||
init: | ||
test -f .env || cp .env.example .env | ||
$(call set_env,USER_ID,$(shell id -u)) | ||
|
||
.PHONY: up | ||
up: | ||
DOCKER_BUILDKIT=1 docker compose up -d --build | ||
|
||
.PHONY: down | ||
down: | ||
docker compose down | ||
|
||
.PHONY: shell | ||
shell: | ||
$(EXEC) zsh | ||
|
||
# Project recipes | ||
.PHONY: build | ||
build: | ||
$(EXEC) ./scripts/set-versions.sh $(NETBOX_VERSION) $(NETBOX_DOCKER_VERSION) | ||
./scripts/fetch-spec.sh $$(cat api/netbox_version) $$(cat api/netbox_docker_version) | ||
$(EXEC) ./scripts/generate-code.sh | ||
|
||
.PHONY: test | ||
test: | ||
$(EXEC) go test ./... -tags=integration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.3.10 |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
services: | ||
app: | ||
build: | ||
context: . | ||
target: development | ||
args: | ||
USER_ID: ${USER_ID:-1000} | ||
volumes: | ||
- .:/go/src/app | ||
- cache:/go/pkg | ||
env_file: .env | ||
hostname: app | ||
tty: true | ||
|
||
volumes: | ||
cache: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
NETBOX_VERSION="$1" | ||
NETBOX_DOCKER_VERSION="$2" | ||
|
||
REPO_DIR='/tmp/netbox-docker' | ||
|
||
rm -rf "${REPO_DIR}" | ||
|
||
git clone https://github.com/netbox-community/netbox-docker.git \ | ||
--config advice.detachedHead=false \ | ||
--branch ${NETBOX_DOCKER_VERSION} \ | ||
--depth=1 \ | ||
--quiet \ | ||
"${REPO_DIR}" | ||
|
||
mv "${REPO_DIR}/docker-compose.override.yml.example" "${REPO_DIR}/docker-compose.override.yml" | ||
|
||
export VERSION="v${NETBOX_VERSION}" | ||
docker compose --project-directory="${REPO_DIR}" up --detach --quiet-pull | ||
|
||
curl --silent http://127.0.0.1:8000/api/docs/?format=openapi > api/openapi.json | ||
|
||
docker compose --project-directory="${REPO_DIR}" down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
export SRC_DIR='./netbox' | ||
|
||
# Prepare environment | ||
rm -rf netbox/client netbox/models | ||
go mod download | ||
|
||
# Run pre-generation hooks | ||
for SCRIPT in scripts/pre-generation/*; do | ||
"${SCRIPT}" | ||
done | ||
|
||
# Generate code | ||
swagger generate client -f api/openapi.json --target="${SRC_DIR}" --copyright-file=assets/copyright_header.txt | ||
go mod tidy | ||
|
||
# Run post-generation hooks | ||
for SCRIPT in scripts/post-generation/*; do | ||
"${SCRIPT}" | ||
done | ||
|
||
# Format generated code and fix imports | ||
goimports -w "${SRC_DIR}" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Maximum and minimum values in spec that correspond to | ||
# `math.MaxInt64` and `math.MinInt64`, respectively, are not rendered | ||
# properly by "go-swagger", thus causing the following errors. | ||
# | ||
# cannot use -9.223372036854776e+18 (untyped float constant -9.22337e+18) as int64 value in argument to validate.MinimumInt (truncated) | ||
# cannot use 9.223372036854776e+18 (untyped float constant 9.22337e+18) as int64 value in argument to validate.MaximumInt (truncated) | ||
# | ||
# See: https://github.com/go-swagger/go-swagger/issues/2755 | ||
|
||
set -euo pipefail | ||
|
||
find "${SRC_DIR}" -type f -name '*.go' -exec \ | ||
sed -i \ | ||
-e 's/-9.223372036854776e+18/math.MinInt64/' \ | ||
-e 's/9.223372036854776e+18/math.MaxInt64/' \ | ||
{} \; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env bash | ||
|
||
NETBOX_VERSION='latest' | ||
NETBOX_DOCKER_VERSION='auto' | ||
|
||
set -euo pipefail | ||
|
||
# Parse Netbox version | ||
if [ "$#" -gt 0 ]; then | ||
NETBOX_VERSION="$1" | ||
fi | ||
|
||
if [ "${NETBOX_VERSION}" == 'latest' ]; then | ||
NETBOX_VERSION=$(curl --silent https://api.github.com/repos/netbox-community/netbox/releases/latest | | ||
jq '.tag_name' | | ||
sed -E 's/"v([^"]+)"/\1/') | ||
fi | ||
|
||
# Parse Netbox Docker version | ||
if [ "$#" -gt 1 ]; then | ||
NETBOX_DOCKER_VERSION="$2" | ||
fi | ||
|
||
if [ "${NETBOX_DOCKER_VERSION}" == 'auto' ]; then | ||
NEXT='https://registry.hub.docker.com/v2/repositories/netboxcommunity/netbox/tags/?page=1&page_size=1000' | ||
NETBOX_DOCKER_VERSION='' | ||
|
||
while [ "${NEXT}" != "null" ] && [ "${NETBOX_DOCKER_VERSION}" == "" ]; do | ||
RESPONSE=$(curl --silent "${NEXT}") | ||
NEXT=$(echo -E "${RESPONSE}" | jq '.next') | ||
|
||
NETBOX_DOCKER_VERSION=$(echo -E "${RESPONSE}" | | ||
jq -r ".results[] | select(.name | startswith(\"v${NETBOX_VERSION}-\")) | .name" | | ||
cut -d "-" -f 2) | ||
done | ||
|
||
unset NEXT | ||
unset RESPONSE | ||
fi | ||
|
||
# Print variables | ||
echo ${NETBOX_VERSION} > api/netbox_version | ||
echo ${NETBOX_DOCKER_VERSION} > api/netbox_docker_version |