Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #57 from hellofresh/patch/refactor-cmd
Browse files Browse the repository at this point in the history
EES-3753 Simplified command structure and app building
  • Loading branch information
vgarvardt authored Aug 12, 2020
2 parents de5ff2e + 17000dd commit 417028f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
.idea/
.vpass
_build/out/*
out/*
dist/*
vendor/
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
language: go

go:
- "1.12"
- "1.13"
- "1.14"
- stable

install:
- mkdir -p $GOPATH/bin
Expand Down
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ OK_COLOR=\033[32;01m
ERROR_COLOR=\033[31;01m
WARN_COLOR=\033[33;01m

# The import path is the unique absolute name of your repository.
# All subpackages should always be imported as relative to it.
# If you change this, run `make clean`.
IMPORT_PATH := github.com/hellofresh/kandalf
PKG_SRC := $(IMPORT_PATH)/cmd/kandalf

# Space separated patterns of packages to skip in list, test, format.
IGNORED_PACKAGES := /vendor/

Expand All @@ -18,7 +12,7 @@ all: clean build

build:
@echo "$(OK_COLOR)==> Building... $(NO_COLOR)"
/bin/sh -c "ARCH=$(ARCH) VERSION=${VERSION} PKG_SRC=$(PKG_SRC) ./build/build.sh"
/bin/sh -c "VERSION=${VERSION} ./build/build.sh"

test:
@/bin/sh -c "./build/test.sh $(allpackages)"
Expand Down
6 changes: 3 additions & 3 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ OS_ARCH_ARG=(386 amd64)
for OS in ${OS_PLATFORM_ARG[@]}; do
for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..."
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/kandalf_$OS-$ARCH" $PKG_SRC
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/kandalf_$OS-$ARCH"
done
done

Expand All @@ -27,9 +27,9 @@ OS_ARCH_ARG=(arm arm64)
for OS in ${OS_PLATFORM_ARG[@]}; do
for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..."
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/kandalf_$OS-$ARCH" $PKG_SRC
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/kandalf_$OS-$ARCH"
done
done

echo "Building default binary"
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/kandalf" $PKG_SRC
CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/kandalf"
49 changes: 35 additions & 14 deletions cmd/kandalf/app.go → cmd/kandalf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main
package cmd

import (
"fmt"
"net/url"
"os"
"path/filepath"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/hellofresh/stats-go/hooks"
statsLogger "github.com/hellofresh/stats-go/log"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/hellofresh/kandalf/pkg/amqp"
"github.com/hellofresh/kandalf/pkg/config"
Expand All @@ -21,35 +21,50 @@ import (
)

// RunApp is main application bootstrap and runner
func RunApp(cmd *cobra.Command, args []string) {
func RunApp(version, configPath string) error {
log.WithField("version", version).Info("Kandalf starting...")

globalConfig, err := config.Load(configPath)
failOnError(err, "Failed to load application configuration")
if err != nil {
return fmt.Errorf("failed to load application configuration: %w", err)
}

err = globalConfig.Log.Apply()
failOnError(err, "Failed to configure logger")
if err != nil {
return fmt.Errorf("failed to configure logger: %w", err)
}
defer globalConfig.Log.Flush()

statsClient := initStatsClient(globalConfig.Stats)
statsClient, err := initStatsClient(globalConfig.Stats)
if err != nil {
return err
}
defer func() {
if err := statsClient.Close(); err != nil {
log.WithError(err).Error("Got error on closing stats client")
}
}()

pipesList, err := config.LoadPipesFromFile(globalConfig.Kafka.PipesConfig)
failOnError(err, "Failed to load pipes config")
if err != nil {
return fmt.Errorf("failed to load pipes config: %w", err)
}

storageURL, err := url.Parse(globalConfig.StorageDSN)
failOnError(err, "Failed to parse Storage DSN")
if err != nil {
return fmt.Errorf("failed to load pipes config: %w", err)
}

persistentStorage, err := storage.NewPersistentStorage(storageURL)
failOnError(err, "Failed to establish Redis connection")
if err != nil {
return fmt.Errorf("failed to establish Redis connection: %w", err)
}
// Do not close storage here as it is required in Worker close to store unhandled messages

kafkaProducer, err := producer.NewKafkaProducer(globalConfig.Kafka, statsClient)
failOnError(err, "Failed to establish Kafka connection")
if err != nil {
return fmt.Errorf("failed to establish Kafka connection: %w", err)
}
defer func() {
if err := kafkaProducer.Close(); err != nil {
log.WithError(err).Error("Got error on closing kafka producer")
Expand All @@ -65,7 +80,9 @@ func RunApp(cmd *cobra.Command, args []string) {

queuesHandler := amqp.NewQueuesHandler(pipesList, worker.MessageHandler, statsClient)
amqpConnection, err := amqp.NewConnection(globalConfig.RabbitDSN, queuesHandler)
failOnError(err, "Failed to establish initial connection to AMQP")
if err != nil {
return fmt.Errorf("failed to establish initial connection to AMQP: %w", err)
}
defer func() {
if err := amqpConnection.Close(); err != nil {
log.WithError(err).Error("Got error on closing AMQP connection")
Expand All @@ -78,9 +95,11 @@ func RunApp(cmd *cobra.Command, args []string) {

log.Infof("[*] Waiting for users. To exit press CTRL+C")
<-forever

return nil
}

func initStatsClient(config config.StatsConfig) client.Client {
func initStatsClient(config config.StatsConfig) (client.Client, error) {
statsLogger.SetHandler(func(msg string, fields map[string]interface{}, err error) {
entry := log.WithFields(fields)
if err == nil {
Expand All @@ -91,7 +110,9 @@ func initStatsClient(config config.StatsConfig) client.Client {
})

statsClient, err := stats.NewClient(config.DSN)
failOnError(err, "Failed to init stats client!")
if err != nil {
return nil, fmt.Errorf("failed to init stats client: %w", err)
}

log.AddHook(hooks.NewLogrusHook(statsClient, config.ErrorsSection))

Expand All @@ -103,5 +124,5 @@ func initStatsClient(config config.StatsConfig) client.Client {
_, appFile := filepath.Split(os.Args[0])
statsClient.TrackMetric("app", bucket.MetricOperation{"init", host, appFile})

return statsClient
return statsClient, nil
}
6 changes: 0 additions & 6 deletions doc.go

This file was deleted.

18 changes: 9 additions & 9 deletions cmd/kandalf/main.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"fmt"
"log"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/hellofresh/kandalf/cmd"
)

var (
Expand All @@ -14,12 +16,6 @@ var (
versionFlag bool
)

func failOnError(err error, msg string) {
if err != nil {
log.WithError(err).Panic(msg)
}
}

func main() {
versionString := "Kandalf v" + version
cobra.OnInitialize(func() {
Expand All @@ -35,11 +31,15 @@ func main() {
Long: versionString + `. RabbitMQ to Kafka bridge.
Complete documentation is available at https://github.com/hellofresh/kandalf`,
Run: RunApp,
RunE: func(c *cobra.Command, args []string) error {
return cmd.RunApp(version, configPath)
},
}
RootCmd.Flags().StringVarP(&configPath, "config", "c", "", "Source of a configuration file")
RootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Print application version")

err := RootCmd.Execute()
failOnError(err, "Failed to execute root command")
if err != nil {
log.Fatal(err)
}
}
3 changes: 2 additions & 1 deletion pkg/storage/persistent_storage_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package storage

import (
"github.com/stretchr/testify/assert"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewPersistentStorage_ErrUnknownStorage(t *testing.T) {
Expand Down

0 comments on commit 417028f

Please sign in to comment.