From 93eea1fad0d5e7f2aa981ec031d343b46f69babb Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 5 Sep 2023 16:44:53 +0200 Subject: [PATCH] initial commit --- cookiecutter.json | 7 ++++++ hooks/post_gen_project.sh | 9 ++++++++ {{cookiecutter.project_slug}}/.dockerignore | 1 + {{cookiecutter.project_slug}}/.gitignore | 1 + {{cookiecutter.project_slug}}/Dockerfile | 22 +++++++++++++++++++ {{cookiecutter.project_slug}}/Makefile | 21 ++++++++++++++++++ {{cookiecutter.project_slug}}/README.md | 10 +++++++++ .../cmd/{{cookiecutter.project_slug}}.go | 19 ++++++++++++++++ .../internal/{{cookiecutter.project_slug}}.go | 12 ++++++++++ 9 files changed, 102 insertions(+) create mode 100644 cookiecutter.json create mode 100644 hooks/post_gen_project.sh create mode 120000 {{cookiecutter.project_slug}}/.dockerignore create mode 100644 {{cookiecutter.project_slug}}/.gitignore create mode 100644 {{cookiecutter.project_slug}}/Dockerfile create mode 100644 {{cookiecutter.project_slug}}/Makefile create mode 100644 {{cookiecutter.project_slug}}/README.md create mode 100644 {{cookiecutter.project_slug}}/cmd/{{cookiecutter.project_slug}}.go create mode 100644 {{cookiecutter.project_slug}}/internal/{{cookiecutter.project_slug}}.go diff --git a/cookiecutter.json b/cookiecutter.json new file mode 100644 index 0000000..10f6418 --- /dev/null +++ b/cookiecutter.json @@ -0,0 +1,7 @@ +{ + "project_name": "Go Project", + "project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '-')}}", + "mod_name": "{{ cookiecutter.project_name.lower().replace(' ', '').replace('-', '')}}", + "docker_username": "djeeberjr", + "mod_base": "git.kapelle.org/niklas/{{cookiecutter.project_slug}}" +} diff --git a/hooks/post_gen_project.sh b/hooks/post_gen_project.sh new file mode 100644 index 0000000..7c88bee --- /dev/null +++ b/hooks/post_gen_project.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env sh + +set -e + +git init + +go mod init {{cookiecutter.mod_base}} + +go mod tidy diff --git a/{{cookiecutter.project_slug}}/.dockerignore b/{{cookiecutter.project_slug}}/.dockerignore new file mode 120000 index 0000000..3e4e48b --- /dev/null +++ b/{{cookiecutter.project_slug}}/.dockerignore @@ -0,0 +1 @@ +.gitignore \ No newline at end of file diff --git a/{{cookiecutter.project_slug}}/.gitignore b/{{cookiecutter.project_slug}}/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/{{cookiecutter.project_slug}}/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/{{cookiecutter.project_slug}}/Dockerfile b/{{cookiecutter.project_slug}}/Dockerfile new file mode 100644 index 0000000..6363c34 --- /dev/null +++ b/{{cookiecutter.project_slug}}/Dockerfile @@ -0,0 +1,22 @@ +FROM --platform=$BUILDPLATFORM golang:1.21-alpine as build + +ADD . /app +WORKDIR /app + +ARG TARGETARCH +ARG TARGETOS + +RUN apk add --no-cache make +RUN make build BUILD_ARCH="$TARGETARCH" BUILD_OS="$TARGETOS" + +FROM --platform=$TARGETPLATFORM alpine:latest + +RUN apk add --no-cache make ca-certificates + +WORKDIR /data +COPY --from=build /app/build/{{cookiecutter.project_slug}} /app/{{cookiecutter.project_slug}} + +EXPOSE 3000 +VOLUME [ "/data" ] + +CMD [ "/app/{{cookiecutter.project_slug}}" ] \ No newline at end of file diff --git a/{{cookiecutter.project_slug}}/Makefile b/{{cookiecutter.project_slug}}/Makefile new file mode 100644 index 0000000..7771023 --- /dev/null +++ b/{{cookiecutter.project_slug}}/Makefile @@ -0,0 +1,21 @@ +BINARY = {{cookiecutter.project_slug}} +BUILD_DIR = build +BUILD_ARCH = $(shell go env GOARCH) +BUILD_OS = $(shell go env GOOS) + +.PHONY: all +all: clean build + +.PHONY:build +build: $(BUILD_DIR)/$(BINARY) + +$(BUILD_DIR)/$(BINARY): + GOARCH=$(BUILD_ARCH) GOOS=$(BUILD_OS) go build -o $(BUILD_DIR)/$(BINARY) cmd/{{cookiecutter.project_slug}}.go + +.PHONY:clean +clean: + rm -rf $(BUILD_DIR) + +.PHONY: docker +docker: + docker buildx build --platform linux/arm64,linux/amd64,linux/arm/v7,linux/arm64/v8 -t djeeberjr/{{cookiecutter.project_slug}} . --push diff --git a/{{cookiecutter.project_slug}}/README.md b/{{cookiecutter.project_slug}}/README.md new file mode 100644 index 0000000..a3a0d57 --- /dev/null +++ b/{{cookiecutter.project_slug}}/README.md @@ -0,0 +1,10 @@ +# {{cookiecutter.project_name}} + +## Build + +`make build` + +## Build and push docker image + +`make docker` + diff --git a/{{cookiecutter.project_slug}}/cmd/{{cookiecutter.project_slug}}.go b/{{cookiecutter.project_slug}}/cmd/{{cookiecutter.project_slug}}.go new file mode 100644 index 0000000..9a4ab7e --- /dev/null +++ b/{{cookiecutter.project_slug}}/cmd/{{cookiecutter.project_slug}}.go @@ -0,0 +1,19 @@ +package main + +import ( + {{cookiecutter.mod_name}} "{{cookiecutter.mod_base}}/internal" + "github.com/alexflint/go-arg" +) + +type args struct { + Parameter string `arg:"--parameter,required,env:PARAMETER" placeholder:"PARAMETER"` +} + +func main() { + var args args + arg.MustParse(&args) + + {{cookiecutter.mod_name}}.Start({{cookiecutter.mod_name}}.Config{ + Parameter: args.Parameter, + }) +} diff --git a/{{cookiecutter.project_slug}}/internal/{{cookiecutter.project_slug}}.go b/{{cookiecutter.project_slug}}/internal/{{cookiecutter.project_slug}}.go new file mode 100644 index 0000000..c23c97d --- /dev/null +++ b/{{cookiecutter.project_slug}}/internal/{{cookiecutter.project_slug}}.go @@ -0,0 +1,12 @@ +package {{cookiecutter.mod_name}} + +import "fmt" + +type Config struct { + Parameter string +} + +func Start(config Config) { + fmt.Println("Starting {{cookiecutter.project_slug}}") + fmt.Println("Parameter: ", config.Parameter) +}