Skip to content

Commit

Permalink
Overall refactor
Browse files Browse the repository at this point in the history
Refactor to decouple most of the logic, improve testing and make it
easier to add new features.
  • Loading branch information
diegobernardes committed Apr 8, 2019
1 parent 915264c commit f806716
Show file tree
Hide file tree
Showing 45 changed files with 2,010 additions and 1,202 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -11,7 +11,7 @@

# generated files
/go.mod.backup
/pipe_dynamic.go
/internal/application/server/service/pipe/dynamic.go

# editor
/.vscode
Expand Down
21 changes: 8 additions & 13 deletions Makefile
@@ -1,6 +1,6 @@
PROJECT_PATH = /opt/pipehub
DOCKER_CI_IMAGE = registry.gitlab.com/pipehub/pipehub/ci
DOCKER_CI_VERSION = 1
DOCKER_CI_VERSION = 2
CONFIG_PATH ?= $(CURDIR)/cmd/pipehub/pipehub.hcl
WORKSPACE_PATH = $(CURDIR)
RAWTAG = $(shell git tag --points-at | head -n1 | cut -c2-)
Expand All @@ -14,13 +14,9 @@ release:
@PIPEHUB_DOCKER_IMAGE_VERSION=$(RAWTAG) goreleaser release --rm-dist

build:
@go build -tags "$(TAGS)" -o cmd/pipehub/pipehub cmd/pipehub/*.go

generate:
@rm -f pipe_dynamic.go
@GOOS="" GOARCH="" make build
@./cmd/pipehub/pipehub generate -c $(CONFIG_PATH) -w $(WORKSPACE_PATH)
@GOOS=${GOOS} GOARCH=${GOARCH} TAGS=pipe make build
@rm -f internal/application/server/service/pipe/dynamic.go
@go run cmd/pipehub/*.go generate -c $(CONFIG_PATH) -w $(WORKSPACE_PATH)
@go build -o cmd/pipehub/pipehub cmd/pipehub/*.go

pre-pr: go-test go-linter go-linter-vendor docker-linter

Expand All @@ -30,14 +26,14 @@ ifeq ($(EXEC_CONTAINER), false)
@go tool cover -func=test.cover
@rm -f test.cover
else
TARGET=go-test make docker-exec
@TARGET=go-test make docker-exec
endif

go-linter:
ifeq ($(EXEC_CONTAINER), false)
@golangci-lint run -c misc/golangci/golangci.toml
else
TARGET=go-linter make docker-exec
@TARGET=go-linter make docker-exec
endif

go-linter-vendor:
Expand All @@ -46,22 +42,21 @@ ifeq ($(EXEC_CONTAINER), false)
@go mod vendor
@git diff --exit-code
else
TARGET=go-linter-vendor make docker-exec
@TARGET=go-linter-vendor make docker-exec
endif

docker-linter:
ifeq ($(EXEC_CONTAINER), false)
@hadolint misc/docker/ci/Dockerfile
else
TARGET=docker-linter make docker-exec
@TARGET=docker-linter make docker-exec
endif

docker-exec:
@docker run \
-t \
--rm \
-e EXEC_CONTAINER=false \
-e TAGS=$(TAGS) \
-e "TERM=xterm-256color" \
-v $(PWD):$(PROJECT_PATH) \
-w $(PROJECT_PATH) \
Expand Down
149 changes: 0 additions & 149 deletions client.go

This file was deleted.

52 changes: 31 additions & 21 deletions cmd/pipehub/main.go
Expand Up @@ -3,13 +3,16 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"os"

"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"

"github.com/pipehub/pipehub"
"github.com/pipehub/pipehub/internal/application/generator"
"github.com/pipehub/pipehub/internal/application/server"
"github.com/pipehub/pipehub/internal/infra/config"
)

var done = make(chan os.Signal, 1)
Expand Down Expand Up @@ -37,39 +40,40 @@ func cmdStart() *cobra.Command {

func cmdStartRun(configPath *string) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
rawCfg, err := loadConfig(*configPath)
payload, err := ioutil.ReadFile(*configPath)
if err != nil {
err = errors.Wrap(err, "load config error")
err = errors.Wrap(err, "load file error")
fatal(err)
}

if err := rawCfg.valid(); err != nil {
err = errors.Wrap(err, "invalid config")
fatal(err)
}

cfg, err := rawCfg.toClientConfig()
ccfg, err := config.NewConfig(payload)
if err != nil {
err = errors.Wrap(err, "invalid config load")
err = errors.Wrap(err, "config initialization error")
fatal(err)
}

ctxShutdown, ctxShutdownCancel := rawCfg.ctxShutdown()
defer ctxShutdownCancel()

c, err := pipehub.NewClient(cfg)
cfg, err := ccfg.ToServer()
if err != nil {
err = errors.Wrap(err, "pipehub new client error")
err = errors.Wrap(err, "invalid config load")
fatal(err)
}
cfg.Transport.HTTP.AsyncErrorHandler = asyncErrHandler

c := server.NewClient(cfg)
if err := c.Start(); err != nil {
err = errors.Wrap(err, "pipehub start error")
fatal(err)
}

wait()

ctxShutdown, ctxShutdownCancel, err := ccfg.CtxShutdown()
if err != nil {
err = errors.Wrap(err, "context initialization error")
fatal(err)
}
defer ctxShutdownCancel()

go func() {
<-ctxShutdown.Done()
if ctxShutdown.Err() == context.Canceled {
Expand Down Expand Up @@ -103,24 +107,30 @@ func cmdGenerate() *cobra.Command {

func cmdGenerateRun(configPath, workspacePath *string) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
rawCfg, err := loadConfig(*configPath)
payload, err := ioutil.ReadFile(*configPath)
if err != nil {
err = errors.Wrap(err, "load file error")
fatal(err)
}

ccfg, err := config.NewConfig(payload)
if err != nil {
err = errors.Wrap(err, "load config error")
err = errors.Wrap(err, "config initialization error")
fatal(err)
}
cfg := rawCfg.toGenerateConfig()

cfg := ccfg.ToGenerator()
fs := afero.NewBasePathFs(afero.NewOsFs(), *workspacePath)
cfg.Filesystem = fs

g, err := pipehub.NewGenerate(cfg)
g, err := generator.NewClient(cfg)
if err != nil {
err = errors.Wrap(err, "pipehub generate initialization error")
err = errors.Wrap(err, "pipehub generator initialization error")
fatal(err)
}

if err = g.Do(); err != nil {
err = errors.Wrap(err, "pipehub generate execute error")
err = errors.Wrap(err, "pipehub generator execute error")
fatal(err)
}
}
Expand Down

0 comments on commit f806716

Please sign in to comment.