Initial commit

This commit is contained in:
IamTheFij 2024-01-04 04:39:47 +00:00
commit 8fad7198b8
11 changed files with 375 additions and 0 deletions

99
.drone.yml Normal file
View File

@ -0,0 +1,99 @@
---
kind: pipeline
name: test
steps:
- name: test
image: golang:1.21
environment:
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
commands:
- make test
- name: check
image: iamthefij/drone-pre-commit:personal
---
kind: pipeline
name: publish
depends_on:
- test
trigger:
event:
- push
- tag
refs:
- refs/heads/master
- refs/tags/v*
steps:
- name: build all binaries
image: golang:1.21
environment:
VERSION: ${DRONE_TAG:-${DRONE_COMMIT}}
commands:
- make all
- name: compress binaries for release
image: ubuntu
commands:
- find ./dist -type f -executable -execdir tar -czvf {}.tar.gz {} \;
when:
event: tag
- name: upload gitea release
image: plugins/gitea-release
settings:
title: ${DRONE_TAG}
files: dist/*.tar.gz
checksum:
- md5
- sha1
- sha256
- sha512
base_url:
from_secret: gitea_base_url
api_key:
from_secret: gitea_token
when:
event: tag
- name: push images
image: thegeeklab/drone-docker-buildx
settings:
repo: ${REPO_OWNER}/${REPO_NAME}
auto_tag: true
platforms:
- linux/amd64
- linux/arm64
username:
from_secret: docker_username
password:
from_secret: docker_password
---
kind: pipeline
name: notify
depends_on:
- test
- publish
trigger:
status:
- failure
steps:
- name: notify
image: drillster/drone-email
settings:
host:
from_secret: SMTP_HOST # pragma: whitelist secret
username:
from_secret: SMTP_USER # pragma: whitelist secret
password:
from_secret: SMTP_PASS # pragma: whitelist secret
from: drone@iamthefij.com

20
.gitignore vendored Normal file
View File

@ -0,0 +1,20 @@
# ---> Go
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Compiled output
dist/
nomad-var-dirsync

48
.golangci.yml Normal file
View File

@ -0,0 +1,48 @@
---
linters:
enable:
- asciicheck
- bodyclose
- dogsled
- dupl
- exhaustive
- gochecknoinits
- gocognit
- gocritic
- gocyclo
- goerr113
- gofumpt
- goimports
- gomnd
- goprintffuncname
# - gosec
# - ifshort
- interfacer
- maligned
- misspell
- nakedret
- nestif
- nlreturn
- noctx
- unparam
- wsl
# - errorlint
disable:
- gochecknoglobals
linters-settings:
gosec:
excludes:
- G204
# gomnd:
# settings:
# mnd:
# ignored-functions: math.*
issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck
- gosec
- maligned

22
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,22 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: check-yaml
args:
- --allow-multiple-documents
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-merge-conflict
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
- id: go-fmt
- id: go-imports
- id: golangci-lint
- repo: https://github.com/hadolint/hadolint
rev: v2.12.1-beta
hooks:
- id: hadolint

18
Dockerfile Normal file
View File

@ -0,0 +1,18 @@
ARG TARGETOS
ARG TARGETARCH
FROM alpine:3.19
RUN mkdir /app
WORKDIR /app/
# Add user for running as non-root
RUN addgroup -S nomad-var-dirsync && adduser -S nomad-var-dirsync -G nomad-var-dirsync
# Copy binary in
COPY ./dist/nomad-var-dirsync-TARGETOS-TARGETARCH ./nomad-var-dirsync
# Drop to non-root user
USER nomad-var-dirsync
ENTRYPOINT [ "./nomad-var-dirsync" ]

35
Dockerfile.multi-stage Normal file
View File

@ -0,0 +1,35 @@
ARG TARGETOS
ARG TARGETARCH
FROM golang:1.21-alpine AS builder
RUN apk add --no-cache git=~2
RUN mkdir /app
WORKDIR /app
COPY ./go.mod ./go.sum /app/
RUN go mod download
COPY ./*.go /app/
ARG VERSION=dev
ENV CGO_ENABLED=0 GOOS=TARGETOS GOARCH=TARGETARCH
RUN go build -ldflags "-X main.version=VERSION" -a -installsuffix nocgo -o nomad-var-dirsync .
FROM REPO/alpine:3.19
RUN mkdir /app
WORKDIR /app/
# Copy binary in
COPY --from=builder /app/nomad-var-dirsync .
# Add user for running as non-root
RUN addgroup -S nomad-var-dirsync && adduser -S nomad-var-dirsync -G nomad-var-dirsync
# Drop to non-root user
USER nomad-var-dirsync
ENTRYPOINT [ "./nomad-var-dirsync" ]
# vim: set filetype=dockerfile:

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2021 $REPO_OWNER
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

92
Makefile Normal file
View File

@ -0,0 +1,92 @@
APP_NAME = nomad-var-dirsync
DOCKER_TAG ?= $(APP_NAME)-USER
VERSION ?= $(shell git describe --tags --dirty)
GOFILES = *.go
# Multi-arch targets are generated from this
TARGET_ALIAS = $(APP_NAME)-linux-amd64 $(APP_NAME)-linux-arm64 $(APP_NAME)-darwin-amd64 $(APP_NAME)-darwin-arm64
TARGETS = $(addprefix dist/,$(TARGET_ALIAS))
#
# Default make target will run tests
.DEFAULT_GOAL = test
# Build all static Minitor binaries
.PHONY: all
all: $(TARGETS)
# Build all static Linux Minitor binaries
.PHONY: all-linux
all-linux: $(filter dist/$(APP_NAME)-linux-%,$(TARGETS))
# Build nomad-var-dirsync for the current machine
$(APP_NAME): $(GOFILES)
@echo Version: $(VERSION)
go build -ldflags '-X "main.version=$(VERSION)"' -o $(APP_NAME)
.PHONY: build
build: $(APP_NAME)
# Run all tests
.PHONY: test
test:
go test -coverprofile=coverage.out
go tool cover -func=coverage.out
@go tool cover -func=coverage.out | awk -v target=80.0% \
'/^total:/ { print "Total coverage: " $3 " Minimum coverage: " target; if ($3+0.0 >= target+0.0) print "ok"; else { print "fail"; exit 1; } }'
# Installs pre-commit hooks
.PHONY: install-hooks
install-hooks:
pre-commit install --install-hooks
# Runs pre-commit checks on files
.PHONY: check
check:
pre-commit run --all-files
.PHONY: clean
clean:
rm -f ./$(APP_NAME)
rm -f ./coverage.out
rm -fr ./dist
## Multi-arch targets
$(TARGETS): $(GOFILES)
mkdir -p ./dist
GOOS=$(word 2, $(subst -, ,$(@))) GOARCH=$(word 3, $(subst -, ,$(@))) CGO_ENABLED=0 \
go build -ldflags '-X "main.version=$(VERSION)"' -a -installsuffix nocgo \
-o $@
.PHONY: $(TARGET_ALIAS)
$(TARGET_ALIAS):
$(MAKE) $(addprefix dist/,$@)
# Docker targets
.PHONY: docker-build
docker-build:
docker build -f ./Dockerfile.multi-stage -t $(DOCKER_TAG) .
.PHONY: docker-run
docker-run: docker-build
docker run --rm -v $(shell pwd)/config.yml:/root/config.yml $(DOCKER_TAG)
# Arch specific docker build targets
.PHONY: docker-build-arm
docker-build-amd64: dist/$(APP_NAME)-linux-amd64
docker build --platform linux/amd64 . -t DOCKER_TAG-linux-amd64
.PHONY: docker-build-arm64
docker-build-arm64: dist/$(APP_NAME)-linux-arm64
docker build --build-arg REPO=arm64v8 --build-arg ARCH=arm64 . -t DOCKER_TAG-linux-arm64
# Cross run on host architechture
.PHONY: docker-run-amd64
docker-run-amd64: docker-build-amd64
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock --name $(DOCKER_TAG)-run DOCKER_TAG-linux-amd64
.PHONY: docker-run-arm
docker-run-arm: docker-build-arm
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock --name $(DOCKER_TAG)-run DOCKER_TAG-linux-arm
.PHONY: docker-run-arm64
docker-run-arm64: docker-build-arm64
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock --name $(DOCKER_TAG)-run DOCKER_TAG-linux-arm64

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# [nomad-var-dirsync](/iamthefij/nomad-var-dirsync)
Sync a directory with Nomad Variables

6
go.mod Normal file
View File

@ -0,0 +1,6 @@
module git.iamthefij.com/iamthefij/nomad-var-dirsync
go 1.21
require (
)

23
main.go Normal file
View File

@ -0,0 +1,23 @@
package main
import (
"flag"
"fmt"
)
var (
// version of nomad-var-dirsync being run
version = "dev"
)
func main() {
showVersion := flag.Bool("version", false, "Display the version of nomad-var-dirsync and exit")
flag.Parse()
// Print version if flag is provided
if *showVersion {
fmt.Println("nomad-var-dirsync version:", version)
return
}
}