Skip to content

Commit

Permalink
OS-4 | Introduce new root command and more tests (#1)
Browse files Browse the repository at this point in the history
* OS-4 | Introduce new root command and more tests

* change GH action

* add more tests

* fix lints

* fix more lints
  • Loading branch information
Hakan Kurtulus authored Jun 1, 2021
1 parent 2479ebe commit 9c6518c
Show file tree
Hide file tree
Showing 27 changed files with 613 additions and 485 deletions.
32 changes: 17 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@ jobs:
- name: Check out the repo
uses: actions/checkout@v2

- name: Import Secrets
id: secrets
uses: hashicorp/[email protected]
with:
url: ${{ secrets.VAULT_ADDR }}
method: github
githubToken: ${{ secrets.GT_VAULT_AUTH_TOKEN }}
secrets: |
github/data/token registry_write | GITHUB_REGISTRY_TOKEN ;
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Build and push to Docker
id: docker_build
uses: docker/build-push-action@v2
with:
context: ./
file: ./Dockerfile
push: true
tags: omegion/db-backup:${{ steps.get_version.outputs.VERSION }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to GitHub
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.CR_PAT }}
password: ${{ steps.secrets.outputs.GITHUB_REGISTRY_TOKEN }}

- name: Build and push to GitHub
id: github_build
Expand All @@ -45,7 +46,8 @@ jobs:
context: ./
file: ./Dockerfile
push: true
tags: ghcr.io/omegion/db-backup:${{ steps.get_version.outputs.VERSION }}
tags: ghcr.io/omegion/db-backup:latest,ghcr.io/omegion/db-backup:${{ steps.get_version.outputs.VERSION }}
platforms: linux/amd64,linux/arm64

- name: Build Changelog
id: github_release
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Code Check

on:
push:
pull_request:

jobs:
Expand Down Expand Up @@ -66,4 +65,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.37.0
version: v1.40.1
67 changes: 7 additions & 60 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ run:
linters-settings:
goimports:
local-prefixes: github.com/omegion/db-backup
golint:
min-confidence: 0
gci:
local-prefixes: github.com/omegion/db-backup
govet:
check-shadowing: true
misspell:
Expand All @@ -31,64 +31,11 @@ issues:
- gosec
- dupl
- funlen
- scopelint
- testpackage

linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- asciicheck
- bodyclose
- depguard
- dogsled
- dupl
- exportloopref
- funlen
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goimports
- golint
- gomnd
- gomodguard
- goprintffuncname
- gosec
- interfacer
- lll
- maligned
- misspell
- nakedret
- nestif
- noctx
- nolintlint
- prealloc
- rowserrcheck
- scopelint
- sqlclosecheck
- stylecheck
- unconvert
- unparam
- whitespace
- wsl
- tparallel
# don't enable:
# - go-header
# - testpackage
disable-all: false
enable-all: true
disable:
- exhaustivestruct
- wrapcheck
17 changes: 10 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
ARG GO_VERSION=1.15-alpine3.12
ARG FROM_IMAGE=alpine:3.11
ARG GO_VERSION=1.16-alpine3.12
ARG FROM_IMAGE=alpine:3.12

FROM golang:${GO_VERSION} AS builder
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS builder

ARG TARGETOS
ARG TARGETARCH

LABEL org.opencontainers.image.source="https://github.com/omegion/do-db-backup"

Expand All @@ -11,11 +14,11 @@ RUN apk update && \
rm -rf /var/cache/apk/* && \
rm -rf /var/tmp/*

COPY ./ /app

WORKDIR /app

RUN make build-for-container
COPY ./ /app

RUN make build TARGETOS=$TARGETOS TARGETARCH=$TARGETARCH

FROM ${FROM_IMAGE}

Expand All @@ -25,6 +28,6 @@ RUN apk update && \
rm -rf /var/cache/apk/* && \
rm -rf /var/tmp/*

COPY --from=builder /app/dist/db-backup-linux /bin/db-backup
COPY --from=builder /app/dist/db-backup /bin/db-backup

ENTRYPOINT ["db-backup"]
34 changes: 19 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
export PATH := $(abspath ./vendor/bin):$(PATH)

BASE_PACKAGE_NAME = github.com/omegion/db-backup
GIT_VERSION = $(shell git describe --tags --always 2> /dev/null || echo 0.0.0)
LDFLAGS = -ldflags "-X $(BASE_PACKAGE_NAME)/pkg/info.Version=$(GIT_VERSION)"
BUFFER := $(shell mktemp)
REPORT_DIR = dist/report
COVER_PROFILE = $(REPORT_DIR)/coverage.out
BASE_PACKAGE_NAME = github.com/omegion/db-backup
GIT_VERSION = $(shell git describe --tags --always 2> /dev/null || echo 0.0.0)
LDFLAGS = -ldflags "-X $(BASE_PACKAGE_NAME)/internal/info.Version=$(GIT_VERSION)"
BUFFER := $(shell mktemp)
REPORT_DIR = dist/report
COVER_PROFILE = $(REPORT_DIR)/coverage.out
TARGETOS = "darwin"
TARGETARCH = "amd64"

.PHONY: build
build:
CGO_ENABLED=0 go build $(LDFLAGS) -installsuffix cgo -o dist/db-backup main.go

build-for-container:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -a -installsuffix cgo -o dist/db-backup-linux main.go
CGO_ENABLED=0 GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) go build $(LDFLAGS) -a -installsuffix cgo -o dist/db-backup main.go

.PHONY: lint
lint:
@echo "Checking code style"
gofmt -l . | tee $(BUFFER)
@! test -s $(BUFFER)
go vet ./...
go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.37.1
go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.40.1
@golangci-lint --version
golangci-lint run
golangci-lint run --fix
go get -u golang.org/x/lint/golint
golint -set_exit_status ./...

.PHONY: test
test:
@echo "Running unit tests"
mkdir -p $(REPORT_DIR)
go test -covermode=count -coverprofile=$(COVER_PROFILE) -tags test -failfast ./...
go test -covermode=count -coverprofile=$(COVER_PROFILE) -tags test -failfast -parallel 4 ./...
go tool cover -html=$(COVER_PROFILE) -o $(REPORT_DIR)/coverage.html

.PHONY: cut-tag
Expand All @@ -40,8 +39,13 @@ cut-tag:
git push origin $(version)

.PHONY: release
release: build-for-container
release: build
@echo "Releasing $(GIT_VERSION)"
docker build -t db-backup . --build-arg VERSION=$(GIT_VERSION)
docker build -t ddclient .
docker tag db-backup:latest omegion/db-backup:$(GIT_VERSION)
docker push omegion/db-backup:$(GIT_VERSION)

.PHONY: docker-image
docker-image:
@echo "Building Docker Image"
docker buildx build -t db-backup --platform linux/amd64,linux/arm64 . --output=type=docker
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ go get github.com/omegion/db-backup
```

```shell
CLI command to generate configs for Kubernetes as a Cluster
CLI command to create backup for provider in local or cloud.

Usage:
db-backup [command]
Expand All @@ -26,7 +26,8 @@ Available Commands:
version Print the version/build number

Flags:
-h, --help help for db-backup
-h, --help help for db-backup
--logLevel string Set the logging level. One of: debug|info|warn|error (default "info")

Use "db-backup [command] --help" for more information about a command.
```
Expand All @@ -43,7 +44,7 @@ db-backup dump s3 \
--type=postgres \
--host=example.com \
--port=1234 \
--database=test \
--provider=test \
--username=test \
--password="12345" \
--bucket-name=$BUCKET_NAME \
Expand Down
24 changes: 12 additions & 12 deletions cmd/local/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package local

import (
"fmt"
"log"
"strings"

"github.com/omegion/db-backup/pkg/backup"
db "github.com/omegion/db-backup/pkg/database"

"github.com/omegion/go-command"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/omegion/db-backup/internal"
"github.com/omegion/db-backup/internal/backup"
db "github.com/omegion/db-backup/internal/provider"
)

// SetupExportCommand sets default flags.
Expand Down Expand Up @@ -47,8 +47,8 @@ func SetupExportCommand(cmd *cobra.Command) {
}
}

// GetDatabaseByType gets database by its type.
func GetDatabaseByType(options db.Options) (db.Database, error) {
// GetDatabaseByType gets provider by its type.
func GetDatabaseByType(options internal.Options) (internal.Database, error) {
if options.Type == "postgres" {
return &db.Postgres{
Host: options.Host,
Expand All @@ -63,11 +63,11 @@ func GetDatabaseByType(options db.Options) (db.Database, error) {
return &db.Postgres{}, db.TypeError{Type: options.Type}
}

// Export exports given tables from database.
// Export exports given tables from provider.
func Export() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Export database table to local",
Short: "Export provider table to local",
RunE: func(cmd *cobra.Command, args []string) error {
dbType, _ := cmd.Flags().GetString("type")
host, _ := cmd.Flags().GetString("host")
Expand All @@ -76,10 +76,10 @@ func Export() *cobra.Command {
username, _ := cmd.Flags().GetString("username")
password, _ := cmd.Flags().GetString("password")

commander := command.Command{}
commander := internal.NewCommander()

for _, databaseName := range strings.Split(databases, ",") {
options := db.Options{
options := internal.Options{
Type: dbType,
Host: host,
Port: port,
Expand Down Expand Up @@ -107,7 +107,7 @@ func Export() *cobra.Command {
return err
}

fmt.Printf("Database %s exported successfully.\n", databaseName)
log.Infoln(fmt.Sprintf("Database %s exported successfully.", databaseName))
}

return nil
Expand Down
Loading

0 comments on commit 9c6518c

Please sign in to comment.