diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..152a140 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +DEB_USER= +DEB_TOKEN= +# example gitea debian package registry: +DEB_REGISTRY=https:///api/packages//debian/pool/bookworm/stable/upload diff --git a/.gitignore b/.gitignore index 3d8c52b..e2bc34e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,12 @@ +.env ringlog test.log +.vscode/settings.json +build/ +# Editor +*~ +.emacs.d/ +*.swp +*.swo +.vim/ +__* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..75567f0 --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +# Load variables from .env if it exists +ifneq (,$(wildcard .env)) + include .env + export $(shell sed 's/=.*//' .env) +endif + +BINARY_NAME=ringlog +GO_MODULE=github.com/anti-spin/ringlog +GOOS=linux +GOARCH=amd64 +CGO_ENABLED=0 + +# Get the latest git tag or commit hash +VERSION := $(shell git describe --tags --always --dirty) + +BUILD_DIR=build +DEB_DIR=$(BUILD_DIR)/$(BINARY_NAME)_$(VERSION)_amd64 + +# Default target +all: clean package upload + +# Build the binary with static linking +build: + @echo "Building $(BINARY_NAME) $(VERSION)..." + GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) go build -ldflags="-X $(GO_MODULE)/cmd.version=$(VERSION)" -o $(BINARY_NAME) + @echo "Build completed: $(BINARY_NAME)" + +# Clean the binary +clean: + @echo "Cleaning up..." + rm -f $(BINARY_NAME) + rm -rf $(BUILD_DIR) + @echo "Clean completed." + +package-prepare: build + mkdir -p $(DEB_DIR)/DEBIAN + install -m 755 -D $(BINARY_NAME) $(DEB_DIR)/usr/local/bin/$(BINARY_NAME) + install -m 644 -D debian/$(BINARY_NAME).1 $(DEB_DIR)/usr/share/man/man1/$(BINARY_NAME).1 + sed "s/@VERSION@/$(VERSION)/g" < debian/control.template > $(DEB_DIR)/DEBIAN/control + if [ -f debian/postinst ]; then install -m 755 -D debian/postinst $(DEB_DIR)/DEBIAN/postinst; fi + if [ -f debian/postrm ]; then install -m 755 -D debian/postrm $(DEB_DIR)/DEBIAN/postrm; fi + +package: package-prepare + @echo "Packaging $(BINARY_NAME)..." + dpkg-deb --build $(DEB_DIR) + @echo "Created $(BINARY_NAME)_$(VERSION)_amd64.deb" + +upload: + @echo "Checking for DEB_REGISTRY..." + @if [ -n "$(DEB_REGISTRY)" ]; then \ + echo "Uploading to registry $(DEB_REGISTRY)"; \ + ./upload-deb.sh $(BUILD_DIR)/$(BINARY_NAME)_$(VERSION)_amd64.deb; \ + else \ + echo "DEB_REGISTRY not set, skipping upload"; \ + fi diff --git a/upload-deb.sh b/upload-deb.sh new file mode 100755 index 0000000..a7009cc --- /dev/null +++ b/upload-deb.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +# read DEB_USER, DEB_TOKEN, and DEB_REGISTRY from .env file +set -a +. ./.env +set +a + +if [ $# -eq 0 ]; then + echo "Error: Please provide a file path as an argument." + echo "Usage: $0 " + exit 1 +fi + +file_path="$1" + +if [ ! -f "$file_path" ]; then + echo "Error: File '$file_path' does not exist." + exit 1 +fi + +curl --user $DEB_USER:$DEB_TOKEN \ + --upload-file $file_path \ + $DEB_REGISTRY + +# Check the curl exit status +if [ $? -eq 0 ]; then + echo "Uploaded to debian registry $DEB_REGISTRY" +else + echo "Error: File upload failed." +fi