Skip to content
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

Use golang-devtools #22

Merged
merged 1 commit into from
Oct 2, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 0 additions & 25 deletions .github/linters/.golangci.yml

This file was deleted.

73 changes: 73 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
name: CI/CD
on:
pull_request: {}
push:
branches:
- main

jobs:
golang-ci:
name: Go Lang CI
runs-on: ubuntu-latest
env:
docker-compose-service: golang-devtools
XDG_CACHE_HOME: ${{ github.workspace }}/.cache/xdg
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- id: xdg_cache_hash
run: echo "xdg_cache_hash=${{hashFiles('./docker-compose.yml', './docker-compose/Dockerfile', './go.sum')}}" >> $GITHUB_OUTPUT
- name: Cache xdg
uses: actions/cache@v3
with:
path: ${{ env.XDG_CACHE_HOME }}
key: xdg-${{ github.repository }}-${{ github.job }}-${{ steps.xdg_cache_hash.outputs.xdg_cache_hash }}
restore-keys: |
xdg-${{ github.repository }}-${{ github.job }}-${{ steps.xdg_cache_hash.outputs.xdg_cache_hash }}
xdg-${{ github.repository }}-${{ github.job }}-
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Configure access to internal and private GitHub repos
run: git config --global url."https://${{ secrets.REVIEWBOT_GITHUB_TOKEN }}:[email protected]/coopnorge".insteadOf "https://github.com/coopnorge"
- name: Build devtools
run: docker compose build
- name: Validate
run: docker compose run --rm ${{ env.docker-compose-service }} validate VERBOSE=all

validate-old-go:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.20', '1.21' ]
name: Go ${{ matrix.go }} - validate
steps:
- uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- name: Install toolchain
run: make toolchain
- name: Validate
run: make validate

build:
needs:
- golang-ci
- validate-old-go
if: always()
runs-on: ubuntu-latest
steps:
- run: exit 1
name: "Catch errors"
if: |
needs.golang-ci.result == 'failure' ||
needs.validate-old-go.result == 'failure'
32 changes: 0 additions & 32 deletions .github/workflows/lint.yml

This file was deleted.

25 changes: 0 additions & 25 deletions .github/workflows/master-test.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/pr-test.yml

This file was deleted.

76 changes: 76 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# vim: set noexpandtab fo-=t:
# https://www.gnu.org/software/make/manual/make.html
.PHONY: default
default:

########################################################################
# boiler plate
########################################################################
SHELL=bash
current_makefile:=$(lastword $(MAKEFILE_LIST))
current_makefile_dirname:=$(dir $(current_makefile))
current_makefile_dirname_abspath:=$(dir $(abspath $(current_makefile)))
current_makefile_dirname_realpath:=$(dir $(realpath $(current_makefile)))

ifneq ($(filter all vars,$(VERBOSE)),)
dump_var=$(info var $(1)=$($(1)))
dump_vars=$(foreach var,$(1),$(call dump_var,$(var)))
else
dump_var=
dump_vars=
endif

ifneq ($(filter all targets,$(VERBOSE)),)
__ORIGINAL_SHELL:=$(SHELL)
SHELL=$(warning Building $@$(if $<, (from $<))$(if $?, ($? newer)))$(TIME) $(__ORIGINAL_SHELL)
endif

define __newline


endef


skip=
# skipable makes the targets passed to it skipable with skip=foo%
# $(1): targets that should be skipable
skipable=$(filter-out $(skip),$(1))

go_install=GOFLAGS= go install $(if $(filter all commands,$(VERBOSE)),-v) $(go_install_flags)

########################################################################
# variables
########################################################################
GOPATH:=$(shell type go >/dev/null 2>&1 && go env GOPATH)
export PATH:=$(if $(GOPATH),$(GOPATH)/bin:,)$(PATH)

########################################################################
# targets
########################################################################

.PHONY: toolchain
toolchain:
$(go_install) github.com/golangci/golangci-lint/cmd/[email protected]


.PHONY: toolchain-update
toolchain-update: go_get_flags+=-u
toolchain-update: toolchain

.PHONY: validate-static
validate-static:
golangci-lint run $(if $(filter all commands,$(VERBOSE)),-v) ./...

.PHONY: test
test:
go test -cover -race \
-coverprofile=coverage.out -covermode=atomic \
$(if $(filter all commands,$(VERBOSE)),-v) \
$(if $(gotest_files),$(gotest_files),./...) \
$(gotest_args) \

.PHONY: validate-dynamic
validate-dynamic: test

.PHONY: validate
validate: validate-static validate-dynamic
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
# Go Redis Facade

[![Lint](https://github.com/coopnorge/go-redis-facade/actions/workflows/lint.yml/badge.svg)](https://github.com/coopnorge/go-redis-facade/actions/workflows/lint.yml)
[![Build](https://github.com/coopnorge/go-redis-facade/actions/workflows/master-test.yml/badge.svg)](https://github.com/coopnorge/go-redis-facade/actions/workflows/master-test.yml)
[![Validate](https://github.com/coopnorge/go-redis-facade/actions/workflows/cicd.yaml/badge.svg)](https://github.com/coopnorge/go-redis-facade/actions/workflows/cicd.yaml)

Coop Redis Facade wraps simple interaction with
Redis clients for CRUD operations by preventing
race conditions between multiple client instances
Coop Redis Facade wraps simple interaction with Redis clients for CRUD
operations by preventing race conditions between multiple client instances
against singular instances of Redis.

If you are interested in how Sync between clients works,
take a look at
[this post.](https://redis.io/docs/manual/patterns/distributed-locks/)
If you are interested in how Sync between clients works, take a look at [this
post.](https://redis.io/docs/manual/patterns/distributed-locks/)


## Installation

```bash
```console
$ go get -u github.com/coopnorge/go-redis-facade
```

## Quick Start

Add this import line to the file you're working in:

```Go
```go
import "github.com/coopnorge/go-redis-facade"
```

Expand Down Expand Up @@ -83,19 +80,18 @@ func NewUserRepository(s database.KeyValueStorage) *UserRepository {
func (r *UserRepository) Create(ctx context.Context, u model.User) error {
u.ID = uuid.NewUUID()
u.CreatedAt = time.Now()

j, jErr := json.Marshal(cart)
if jErr != nil {
return jErr
}

return r.db.Save(ctx, u.ID, string(j), expirationTime)
}
```

## Mocks

To generate or update mocks use tools
[Eitri](https://github.com/Clink-n-Clank/Eitri)
or use directly
[Mockhandler](github.com/sanposhiho/gomockhandle)
[Eitri](https://github.com/Clink-n-Clank/Eitri) or use directly
[Mockhandler](https://github.com/sanposhiho/gomockhandler)
2 changes: 2 additions & 0 deletions devtools.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BUILD_OCI=false
GOFLAGS=-mod=readonly
40 changes: 40 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
services:
golang-devtools:
build:
context: docker-compose
target: golang-devtools
dockerfile: Dockerfile
privileged: true
security_opt:
- seccomp:unconfined
- apparmor:unconfined
volumes:
- .:/srv/workspace:z
- ${DOCKER_CONFIG:-~/.docker}:/root/.docker
- ${GIT_CONFIG:-~/.gitconfig}:${GIT_CONFIG_GUEST:-/root/.gitconfig}
- ${SSH_CONFIG:-~/.ssh}:/root/.ssh
- ${XDG_CACHE_HOME:-xdg-cache-home}:/root/.cache
# ${x:-y} explained here https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#:~:text=3.5.3%20Shell%20Parameter%20Expansion
environment:
GOMODCACHE: /root/.cache/go-mod
techdocs:
build:
context: docker-compose
dockerfile: Dockerfile
target: techdocs
working_dir: /srv/workspace
environment:
GOOGLE_APPLICATION_CREDENTIALS: ${GOOGLE_APPLICATION_CREDENTIALS:-}
GCLOUD_PROJECT: ${GCLOUD_PROJECT:-}
volumes:
- .:/srv/workspace:z
- ${XDG_CACHE_HOME:-xdg-cache-home}:/root/.cache
- $HOME/.config/gcloud:/root/.config/gcloud
- ${GOOGLE_APPLICATION_CREDENTIALS:-nothing}:${GOOGLE_APPLICATION_CREDENTIALS:-/tmp/empty-GOOGLE_APPLICATION_CREDENTIALS}
ports:
- "127.0.0.1:3000:3000/tcp"
- "127.0.0.1:8000:8000/tcp"
command: serve
volumes:
xdg-cache-home: { }
nothing: { }
2 changes: 2 additions & 0 deletions docker-compose/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ghcr.io/coopnorge/engineering-docker-images/e0/devtools-golang-v1beta1:gitc-1f40b7aec2da97d51ed26c6f1d027bdee0458a0d@sha256:5abec21bdbd1fec9946a50e825692773a64e7db2ddd10d3446e1fc70cfcbca3b AS golang-devtools
FROM ghcr.io/coopnorge/engineering-docker-images/e0/techdocs:gitc-520957c8b01583b35d335a49bc9d46428d3cfc5e@sha256:3c480d1f43b0dc4f46c041518c94c2c58a08406135a7250eb0d6ea70bc15b8f1 as techdocs
8 changes: 5 additions & 3 deletions encryption.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package database

import (
"context"

"github.com/google/tink/go/aead"
"github.com/google/tink/go/core/registry"
"github.com/google/tink/go/integration/gcpkms"
Expand All @@ -22,13 +24,13 @@ type EncryptionClient struct {

// EncryptionConfig is configuration needed to set up the encryption client
type EncryptionConfig struct {
RedisKeyURI string
Aad []byte
RedisKeyURI string
Aad []byte
}

// NewEncryptionClient creates a new client for encrypting and decrypting data
func NewEncryptionClient(c EncryptionConfig) (*EncryptionClient, error) {
kmsClient, err := gcpkms.NewClient(c.RedisKeyURI)
kmsClient, err := gcpkms.NewClientWithOptions(context.Background(), c.RedisKeyURI)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading