Skip to content

Commit c8da27b

Browse files
committed
chore: init repo
0 parents  commit c8da27b

File tree

2,150 files changed

+622232
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,150 files changed

+622232
-0
lines changed

.ameba.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
Excluded:
3+
- vendor/
4+
5+
Naming/BlockParameterName:
6+
Enabled: false
7+
8+
Lint/UselessAssign:
9+
Excluded:
10+
- src/netbox_extractor/cli.cr

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*.cr]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true

.github/workflows/ci.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
name: CI
3+
4+
on:
5+
- push
6+
- pull_request
7+
8+
9+
jobs:
10+
test_linux:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Git checkout
14+
uses: actions/checkout@v5
15+
16+
- name: Setup Crystal
17+
uses: crystal-lang/install-crystal@v1
18+
with:
19+
crystal: 1.18.2
20+
21+
- name: Install dependencies
22+
run: make deps
23+
24+
- name: Run tests
25+
run: make spec
26+
27+
- name: Run static code analysis
28+
run: make ameba
29+
30+
test_macos:
31+
runs-on: macos-latest
32+
steps:
33+
- name: Git checkout
34+
uses: actions/checkout@v5
35+
36+
- name: Setup Crystal
37+
uses: crystal-lang/install-crystal@v1
38+
with:
39+
crystal: 1.18.2
40+
41+
- name: Install dependencies
42+
run: make deps
43+
44+
- name: Run tests
45+
run: make spec
46+
47+
- name: Run static code analysis
48+
run: make ameba

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Ignore generated data
2+
/docs/
3+
/lib/
4+
/packages/
5+
6+
# Ignore all bin files
7+
/bin/*
8+
!/bin/.keep
9+
10+
# Ignore config files
11+
netbox-extractor.yml
12+
13+
# Ignore env files
14+
.env
15+
16+
# Ignore MacOS files
17+
.DS_Store
18+
19+
# Ignore generated config
20+
/generated
21+
22+
# Ignore OpenAPI jar
23+
openapi-generator-cli.jar

.tool-versions

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
crystal 1.18.2
2+
java openjdk-25

Brewfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# because we heavily rely on it
4+
brew 'bash'
5+
6+
# to run some tasks
7+
brew 'make'
8+
9+
# to install other tools
10+
brew 'asdf'
11+
12+
# to download OpenAPI cli
13+
brew 'curl'

Dockerfile

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
###########
2+
# CRYSTAL #
3+
###########
4+
5+
FROM alpine:3.23 AS crystal
6+
7+
RUN apk add --update --no-cache \
8+
make \
9+
crystal=~1.18 \
10+
shards \
11+
gc-dev \
12+
gc-static \
13+
libxml2-dev \
14+
libxml2-static \
15+
openssl-dev \
16+
openssl-libs-static \
17+
gmp-dev \
18+
gmp-static \
19+
pcre2-dev \
20+
pcre2-static \
21+
xz-dev \
22+
xz-static \
23+
yaml-dev \
24+
yaml-static \
25+
zlib-dev \
26+
zlib-static \
27+
upx
28+
29+
#########
30+
# BUILD #
31+
#########
32+
33+
FROM crystal AS build-binary-file
34+
35+
# Fetch platforms variables from ARGS
36+
ARG TARGETPLATFORM
37+
ARG TARGETOS
38+
ARG TARGETARCH
39+
ARG TARGETVARIANT
40+
41+
# Export them to build binary files with the right name: netbox-extractor-linux-amd64
42+
ENV \
43+
TARGETPLATFORM=${TARGETPLATFORM} \
44+
TARGETOS=${TARGETOS} \
45+
TARGETARCH=${TARGETARCH} \
46+
TARGETVARIANT=${TARGETVARIANT}
47+
48+
# Set build environment
49+
WORKDIR /build
50+
COPY shard.yml shard.lock /build/
51+
COPY Makefile.release /build/Makefile
52+
COPY src/ /build/src/
53+
COPY vendor/ /build/vendor/
54+
COPY templates/ /build/templates/
55+
RUN mkdir /build/bin
56+
57+
# Build the binary
58+
RUN make release
59+
60+
# Extract binary from Docker image
61+
FROM scratch AS binary-file
62+
ARG TARGETOS
63+
ARG TARGETARCH
64+
COPY --from=build-binary-file /build/bin/netbox-extractor-${TARGETOS}-${TARGETARCH} /
65+
66+
###########
67+
# RUNTIME #
68+
###########
69+
70+
# Build distroless images \o/
71+
FROM gcr.io/distroless/static-debian12 AS docker-image
72+
73+
# Fetch platforms variables from ARGS
74+
ARG TARGETOS
75+
ARG TARGETARCH
76+
77+
# Grab netbox-extractor binary from **binary-file** step and inject it in the final image
78+
COPY --from=build-binary-file /build/bin/netbox-extractor-${TARGETOS}-${TARGETARCH} /usr/bin/netbox-extractor
79+
80+
# Set runtime environment
81+
USER nonroot
82+
ENV USER=nonroot
83+
ENV HOME=/home/nonroot
84+
WORKDIR /home/nonroot
85+
ENTRYPOINT ["netbox-extractor"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Nicolas Rodriguez <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Ensure that every command in this Makefile
2+
# will run with bash instead of the default sh
3+
SHELL := /usr/bin/env bash
4+
5+
#############
6+
# Constants #
7+
#############
8+
9+
PREFIX ?= /usr/local
10+
INSTALL_DIR = $(PREFIX)/bin
11+
12+
APP_NAME = netbox-extractor
13+
SOURCE_FILE = src/$(APP_NAME).cr
14+
OUTPUT_DIR = bin
15+
OUTPUT_FILE = $(APP_NAME)
16+
17+
SPEC_OPTS =
18+
COMPILE_OPTS_DEV = --threads 4
19+
COMPILE_OPTS_RELEASE = --threads 4 --release --error-trace
20+
21+
# Use sudo to install/uninstall built binary if current user is not root
22+
ifneq ($(UID), 0)
23+
sudo = sudo
24+
else
25+
sudo =
26+
endif
27+
28+
ifeq ($(shell tty -s && echo true),true)
29+
SPEC_OPTS += --verbose
30+
COMPILE_OPTS_DEV += --progress
31+
COMPILE_OPTS_RELEASE += --progress
32+
endif
33+
34+
OPENAPI_VERSION := 7.17.0
35+
OPENAPI_CLI_URL := https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/$(OPENAPI_VERSION)/openapi-generator-cli-$(OPENAPI_VERSION).jar
36+
37+
NETBOX_VERSION := 4.4.8
38+
NETBOX_LIB_DIR := vendor/netbox-client.$(NETBOX_VERSION)
39+
NETBOX_PATCH := vendor/netbox-client.$(NETBOX_VERSION).diff
40+
NETBOX_API_FILE := vendor/netbox-rest.$(NETBOX_VERSION).yml
41+
42+
################
43+
# Public tasks #
44+
################
45+
46+
# This is the default task
47+
all: help
48+
49+
.PHONY: all
50+
51+
#####################
52+
# Development tasks #
53+
#####################
54+
55+
setup: ## Setup local environment
56+
brew bundle install
57+
asdf plugin add crystal || true
58+
asdf plugin add java || true
59+
asdf install
60+
asdf current
61+
62+
build: ## Compile to development binary
63+
crystal build $(COMPILE_OPTS_DEV) -o $(OUTPUT_DIR)/$(OUTPUT_FILE) $(SOURCE_FILE)
64+
65+
deps: ## Install development dependencies
66+
shards install
67+
68+
clean: ## Cleanup environment
69+
rm -rf bin/*
70+
rm -rf lib/
71+
72+
spec: ## Run Crystal spec
73+
crystal spec $(SPEC_OPTS)
74+
75+
doc: ## Generate project documentation
76+
rm -rf docs
77+
crystal doc
78+
79+
format: ## Format code
80+
crystal tool format
81+
82+
ameba: ## Run static code analysis
83+
bin/ameba
84+
85+
.PHONY: setup build deps clean spec doc ameba
86+
87+
88+
################################
89+
# Netbox API Development tasks #
90+
################################
91+
92+
netbox-api-build: netbox-api-generate netbox-api-patch netbox-api-format ## Download, generate, patch, format
93+
94+
netbox-api-download-openapi-cli: ## Download OpenAPI jar
95+
curl $(OPENAPI_CLI_URL) --output openapi-generator-cli.jar
96+
97+
netbox-api-generate: ## Generate NetboxClient lib
98+
rm -rf $(NETBOX_LIB_DIR)
99+
export _JAVA_OPTIONS=-DmaxYamlCodePoints=99999999 && \
100+
java -jar openapi-generator-cli.jar generate \
101+
-i $(NETBOX_API_FILE) \
102+
-g crystal \
103+
-o $(NETBOX_LIB_DIR) \
104+
--additional-properties moduleName=NetboxClient \
105+
--additional-properties shardName=netbox-client \
106+
--type-mappings "Object=Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time"
107+
108+
netbox-api-patch: ## Patch NetboxClient code
109+
sed -i 's/: Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time/: Hash(String, Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time | Hash(String, Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time))/g' $(shell grep -rn 'type: Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time' vendor/netbox-client.$(NETBOX_VERSION)/src/netbox-client/models | cut -d ':' -f 1 | sort | uniq)
110+
sed -i 's/Array(Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time)/Array(Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time | Hash(String, Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time | Hash(String, Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time)))/g' $(shell grep -rn 'Array(Nil | String | Bool | Int32 | Int64 | Float32 | Float64 | Time)' vendor/netbox-client.$(NETBOX_VERSION)/src/netbox-client/models | cut -d ':' -f 1 | sort | uniq)
111+
git apply $(NETBOX_PATCH)
112+
echo "ok"
113+
114+
netbox-api-create-patch: ## Create patch for NetboxClient code
115+
git diff vendor/ >> $(NETBOX_PATCH)
116+
117+
netbox-api-format: ## Format NetboxClient code
118+
crystal tool format $(NETBOX_LIB_DIR)/spec
119+
crystal tool format $(NETBOX_LIB_DIR)/src
120+
crystal tool format $(NETBOX_LIB_DIR)/spec
121+
crystal tool format $(NETBOX_LIB_DIR)/src
122+
123+
.PHONY: netbox-api-build netbox-api-download-openapi-cli netbox-api-generate netbox-api-patch netbox-api-format
124+
125+
126+
############################
127+
# Docker Development tasks #
128+
############################
129+
130+
docker-image: ## Build local multiplatforms Docker image for local development
131+
docker buildx bake docker-image
132+
133+
.PHONY: docker-image
134+
135+
136+
#################
137+
# Release tasks #
138+
#################
139+
140+
release: ## Compile to production binary
141+
crystal build $(COMPILE_OPTS_RELEASE) -o $(OUTPUT_DIR)/$(OUTPUT_FILE) $(SOURCE_FILE)
142+
cd bin; for f in *; do shasum --algorithm 256 $$f > $$f.sha256; done
143+
144+
deps-release: ## Install production dependencies
145+
shards install --production
146+
147+
install: ## Install binary in $(INSTALL_DIR)
148+
$(sudo) cp --verbose $(OUTPUT_DIR)/$(OUTPUT_FILE) $(INSTALL_DIR)/$(OUTPUT_FILE)
149+
150+
uninstall: ## Uninstall binary from $(INSTALL_DIR)
151+
$(sudo) rm --verbose --force $(INSTALL_DIR)/$(OUTPUT_FILE)
152+
153+
release-static: ## Build static binary with Docker
154+
docker buildx bake binary
155+
mv packages/linux_arm64/$(OUTPUT_FILE)-linux-arm64 packages/
156+
mv packages/linux_amd64/$(OUTPUT_FILE)-linux-amd64 packages/
157+
rmdir packages/linux_arm64/ packages/linux_amd64/
158+
rm -f packages/*.sha256
159+
cd packages; for f in *; do shasum --algorithm 256 $$f > $$f.sha256; done
160+
161+
.PHONY: release deps-release install uninstall release-static
162+
163+
164+
#################
165+
# Private tasks #
166+
#################
167+
168+
help:
169+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
170+
171+
.PHONY: help

0 commit comments

Comments
 (0)