Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
Go 1.17 and goreleaser
Browse files Browse the repository at this point in the history
Fix typo and error handling
Go 1.17
  • Loading branch information
stanislavb committed Mar 5, 2022
1 parent 13a7e25 commit c0aa5fe
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 51 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: build

on: # yamllint disable-line rule:truthy
pull_request:
push:
tags:
- 'v*.*.*'

permissions:
contents: write

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

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

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Build & install
run: |
go mod tidy
go build -v ./...
go install
- name: Tests
run: |
sudo apt-get install -y bats
go test -v ./...
go vet ./...
bats ./bats
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
if: success() && startsWith(github.ref, 'refs/tags/')
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34 changes: 34 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
project_name: couchlock
builds:
- env:
- CGO_ENABLED=0
ldflags:
- -s -w -X main.Version={{.Version}}
goos:
- linux
- darwin
goarch:
- amd64
- arm64
dockers:
- image_templates:
- "tomologic/couchlock:{{ .Version }}"
dockerfile: Dockerfile
build_flag_templates:
- --label=org.opencontainers.image.created={{.Date}}
- --label=org.opencontainers.image.title={{ .ProjectName }}
- --label=org.opencontainers.image.description={{ .ProjectName }}
- --label=org.opencontainers.image.url=https://github.com/tomologic/couchlock
- --label=org.opencontainers.image.source=https://github.com/tomologic/couchlock
- --label=org.opencontainers.image.version={{ .Version }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}
- --label=org.opencontainers.image.licenses=UNLICENSE
nfpms:
- maintainer: Tomologic Ops <[email protected]>
description: Couchlock
homepage: https://github.com/tomologic/couchlock
license: UNLICENSE
formats:
- deb
- rpm
- apk
42 changes: 0 additions & 42 deletions .travis.yml

This file was deleted.

6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM golang:1.9-alpine as builder
FROM golang:1.17-alpine as builder
WORKDIR /go/src/couchlock
RUN apk --no-cache add git
COPY *.go ./
COPY *.go go.mod ./
RUN go build -v

FROM alpine:3.6
FROM alpine:3.13
RUN apk --no-cache add ca-certificates
COPY --from=builder /go/src/couchlock/couchlock /usr/bin/

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ VERSION = $(shell git describe --tags --match 'v[0-9]*\.[0-9]*\.[0-9]*' | sed 's
build: build_darwin build_linux

compile = bash -c "env GOOS=$(1) GOARCH=$(2) go build -a \
-ldflags \"-w -X main.VERSION='$(VERSION)'\" \
-ldflags \"-w -X main.Version='$(VERSION)'\" \
-o $(BUILDDIR)/$(NAME)-$(VERSION)-$(1)-$(2)"

build_darwin:
Expand Down
20 changes: 15 additions & 5 deletions couchlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
fmt.Printf("\n")

fmt.Printf("Commands:\n")
fmt.Printf("\tlock\t\tAquire lock\n")
fmt.Printf("\tlock\t\tAcquire lock\n")
fmt.Printf("\tunlock\t\tUnlock lock\n")
fmt.Printf("\tlist-queue\tList queue for lock\n")
fmt.Printf("\tversion\t\tPrint current version\n\n")
Expand Down Expand Up @@ -121,6 +121,9 @@ func verifyDesignUpdate() {
// Create design document in couchdb
buf := bytes.NewBuffer(designDocument)
req, err := http.NewRequest("PUT", designLocksURL, buf)
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
panic(err)
Expand All @@ -147,6 +150,9 @@ func createLock() *lock {
json1, _ := json.Marshal(lock)
buf := bytes.NewBuffer(json1)
req, err := http.NewRequest("POST", config.couchdb+"/_design/locks/_update/create/", buf)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -180,6 +186,9 @@ func lockLock(lock *lock) {

// change status of lock in couchdb to 'locked'
req, err := http.NewRequest("POST", config.couchdb+"/_design/locks/_update/lock/"+lock.ID, nil)
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
panic(err)
Expand All @@ -190,7 +199,7 @@ func lockLock(lock *lock) {
fmt.Printf("ERROR: %d %s\n", resp.StatusCode, buf.String())
os.Exit(1)
}
fmt.Printf("INFO: Lock '%s' aquired.\n", config.lock)
fmt.Printf("INFO: Lock '%s' acquired.\n", config.lock)
}

func unlockLock() {
Expand Down Expand Up @@ -230,6 +239,9 @@ func unlockLock() {
}

req, err = http.NewRequest("POST", config.couchdb+"/_design/locks/_update/unlock/"+lock.ID, nil)
if err != nil {
panic(err)
}
resp, err = client.Do(req)
if err != nil {
panic(err)
Expand All @@ -253,7 +265,7 @@ func waitForLock(lock *lock) bool {
fmt.Printf("INFO: Waiting for lock '%s' to be available.\n", config.lock)

// wait until our lock is top of list
for true {
for {
resp, err := client.Do(req)
if err != nil {
panic(err)
Expand All @@ -276,8 +288,6 @@ func waitForLock(lock *lock) bool {

time.Sleep(time.Duration(config.interval) * 1000 * time.Millisecond)
}

return false
}

func listQueue() {
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/tomologic/couchlock

go 1.17

0 comments on commit c0aa5fe

Please sign in to comment.