Skip to content

Big refactoring #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GEEKSONATOR_TELEGRAM_BOT_TOKEN=bot_token_here
GEEKSONATOR_TELEGRAM_TIMEOUT_SECONDS=15
GEEKSONATOR_DEBUG_MODE=false
GEEKSONATOR_DEBUG_TELEGRAM_BOT_TOKEN=debug_bot_token_here
38 changes: 38 additions & 0 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: audit

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
audit:
runs-on: ubuntu-latest

steps:
- name: Checkout GitHub Action
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: ./go.mod
cache-dependency-path: ./go.sum

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.55.2
working-directory: ./

- name: Install dependencies
run: |
go mod tidy
go mod vendor
go install github.com/vektra/mockery/v2@v2.36.0

- name: Run test
run: go test ./...
36 changes: 36 additions & 0 deletions .github/workflows/container.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: deploy to ghcr.io

on:
push:
# run only against tags
tags:
- "*"

permissions:
contents: write

jobs:
push-image:
runs-on: ubuntu-latest
steps:
- name: Checkout GitHub Action
uses: actions/checkout@main

- name: Fetch tags
run: git fetch --force --tags

- name: Display tag
run: echo ${{github.ref_name}}

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}

- name: Build Image
run: docker build . --tag ghcr.io/phpgeeks-club/geeksonator:${{github.ref_name}}

- name: Push Image
run: docker push ghcr.io/phpgeeks-club/geeksonator:${{github.ref_name}}
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: goreleaser

on:
push:
# run only against tags
tags:
- "*"

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout GitHub Action
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Fetch tags
run: git fetch --force --tags

- name: Display tag
run: echo ${{github.ref_name}}

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: stable

- name: Display Go version
run: go version

- name: Create release
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin/
dist/
.env
.gitignore
88 changes: 88 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
run:
concurrency: 4
timeout: 5m

linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- dogsled
- dupl
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- exportloopref
- forcetypeassert
- gci
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- importas
- ineffassign
- maintidx
- makezero
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- reassign
- revive
- staticcheck
- stylecheck
- tagliatelle
- thelper
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- whitespace
- wrapcheck

linters-settings:
errorlint:
errorf: false
gci:
sections:
- standard
- default
- prefix(geeksonator)
gocognit:
min-complexity: 10
gocyclo:
min-complexity: 10
nestif:
min-complexity: 4
unparam:
check-exported: true
15 changes: 15 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project_name: geeksonator
builds:
- id: geeksonator
env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
flags:
- -trimpath
ldflags:
- -s -w
main: ./cmd/geeksonator
binary: ./bin/geeksonator
10 changes: 10 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
quiet: true
with-expecter: true
disable-config-search: true
dir: "{{.InterfaceDir}}/mocks"
mockname: "{{.InterfaceName}}Mock"
filename: "{{.InterfaceName | snakecase}}_mock.go"
outpkg: "mocks"
packages:
geeksonator/internal/observer:
geeksonator/internal/provider/telegram:
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang 1.21.4
golangci-lint 1.55.2
58 changes: 58 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Run
# docker build -f ./Dockerfile -t geeksonator:latest .
# docker run -d --env-file=/path/to/.env --name geeksonator.app geeksonator:latest .

##################################
# STEP 1 build executable binary #
##################################

FROM golang:1.21.4-alpine as builder

LABEL org.opencontainers.image.source="https://github.com/phpgeeks-club/admin-bot"

# Install git + SSL ca certificates.
# Git is required for fetching the dependencies.
# Ca-certificates is required to call HTTPS endpoints.
RUN apk update && apk add --no-cache git ca-certificates tzdata make && update-ca-certificates

# Create appuser.
ENV USER=appuser
ENV UID=10001

# See https://stackoverflow.com/a/55757473/12429735
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"

WORKDIR /app

COPY . .

# Build the binary.
RUN make build

##############################
# STEP 2 build a small image #
##############################

FROM scratch

# Import from builder.
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group

# Copy our static executable.
COPY --from=builder /app/bin/geeksonator /app/geeksonator

# Use an unprivileged user.
USER appuser:appuser

# Run the binary.
ENTRYPOINT ["/app/geeksonator"]
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.EXPORT_ALL_VARIABLES:
GOBIN = $(shell pwd)/bin

.PHONY: deps
deps:
@go mod tidy
@go mod vendor

.PHONY: mocks
mocks: tools
@export PATH="$(shell pwd)/bin:$(PATH)"; mockery --config=.mockery.yaml

.PHONY: lint
lint:
@golangci-lint run

.PHONY: test
test:
@go test ./...

.PHONY: docker_build
docker_build:
@docker build -f ./Dockerfile -t geeksonator_dev .

.PHONY: docker_run
docker_run:
@GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "-s -w" -o ./bin/geeksonator ./cmd/geeksonator

.PHONY: build
build: deps
@GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "-s -w" -o ./bin/geeksonator ./cmd/geeksonator

.PHONY: tools
tools: deps
@go install github.com/vektra/mockery/v2@v2.36.0
@go install github.com/goreleaser/goreleaser@v1.21.2
Loading