diff --git a/.gitignore b/.gitignore index 027aae0..95af3be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ .idea/ .vpass _build/out/* -out/* +dist/* vendor/ diff --git a/.travis.yml b/.travis.yml index cf26d49..3125e2f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,7 @@ language: go go: - - "1.12" - - "1.13" - "1.14" - - stable install: - mkdir -p $GOPATH/bin diff --git a/Makefile b/Makefile index bff3e02..a07f3a2 100644 --- a/Makefile +++ b/Makefile @@ -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/ @@ -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)" diff --git a/build/build.sh b/build/build.sh index e82b023..f29aed5 100755 --- a/build/build.sh +++ b/build/build.sh @@ -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 @@ -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" diff --git a/cmd/kandalf/app.go b/cmd/kandalf.go similarity index 72% rename from cmd/kandalf/app.go rename to cmd/kandalf.go index 450046c..676741c 100644 --- a/cmd/kandalf/app.go +++ b/cmd/kandalf.go @@ -1,6 +1,7 @@ -package main +package cmd import ( + "fmt" "net/url" "os" "path/filepath" @@ -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" @@ -21,17 +21,24 @@ 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") @@ -39,17 +46,25 @@ func RunApp(cmd *cobra.Command, args []string) { }() 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") @@ -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") @@ -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 { @@ -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)) @@ -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 } diff --git a/doc.go b/doc.go deleted file mode 100644 index 84af63c..0000000 --- a/doc.go +++ /dev/null @@ -1,6 +0,0 @@ -/* -Package kandalf is RabbitMQ to Kafka bridge. - -For a full guide visit https://github.com/hellofresh/kandalf -*/ -package kandalf diff --git a/cmd/kandalf/main.go b/main.go similarity index 80% rename from cmd/kandalf/main.go rename to main.go index cb9541f..3903297 100644 --- a/cmd/kandalf/main.go +++ b/main.go @@ -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 ( @@ -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() { @@ -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) + } } diff --git a/pkg/storage/persistent_storage_test.go b/pkg/storage/persistent_storage_test.go index 9b4fef9..d7f6176 100644 --- a/pkg/storage/persistent_storage_test.go +++ b/pkg/storage/persistent_storage_test.go @@ -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) {