Skip to content

Commit

Permalink
Update packages and fix code issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarvardt committed Mar 25, 2020
1 parent f7a94bb commit a295b92
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 131 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ klepto
/dist
debug
.klepto.toml
coverage.txt

# Vendor stuff
.glide/
Expand Down
21 changes: 11 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ sudo: required
language: go

go:
- 1.13.x
- "1.13"
- "1.14"
- "stable"

services:
- mysql
Expand All @@ -16,9 +18,9 @@ env:
TEST_MYSQL="root@tcp(localhost:3306)/"

install:
- mkdir -p $GOPATH/bin
- go get github.com/go-playground/overalls
- go get github.com/goreleaser/goreleaser
- go get -u github.com/go-playground/overalls
- go get -u github.com/goreleaser/goreleaser
- make deps

script:
- make test
Expand All @@ -29,9 +31,8 @@ after_success:

# calls goreleaser
deploy:
- provider: script
skip_cleanup: true
script: curl -sL http://git.io/goreleaser | bash
on:
tags: true

- provider: script
skip_cleanup: true
script: curl -sL http://git.io/goreleaser | bash
on:
tags: true
19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ all: clean deps build

deps:
@echo "$(OK_COLOR)==> Installing dependencies$(NO_COLOR)"
@go get github.com/goreleaser/goreleaser
@go get -u golang.org/x/lint/golint
@go mod vendor

# Builds the project
build:
@echo "$(OK_COLOR)==> Building... $(NO_COLOR)"
@goreleaser --snapshot --rm-dist --skip-validate

test:
@/bin/sh -c "./build/test.sh $(allpackages)"
test: lint format vet
@echo "$(OK_COLOR)==> Running tests$(NO_COLOR)"
@CGO_ENABLED=0 go test -cover ./... -coverprofile=coverage.txt -covermode=atomic

lint:
@echo "$(OK_COLOR)==> Checking code style with 'golint' tool$(NO_COLOR)"
@go list ./... | xargs -n 1 golint -set_exit_status

format:
@echo "$(OK_COLOR)==> Checking code formating with 'gofmt' tool$(NO_COLOR)"
@gofmt -l -s cmd pkg | grep ".*\.go"; if [ "$$?" = "0" ]; then exit 1; fi

vet:
@echo "$(OK_COLOR)==> Checking code correctness with 'go vet' tool$(NO_COLOR)"
@go vet ./...

test-docker:
docker-compose up -d
Expand Down
50 changes: 0 additions & 50 deletions build/test.sh

This file was deleted.

20 changes: 14 additions & 6 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import (
"os"

"github.com/BurntSushi/toml"
"github.com/hellofresh/klepto/pkg/config"
wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/hellofresh/klepto/pkg/config"
)

// NewInitCmd creates a new init command
func NewInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Create a fresh config file",
Run: func(cmd *cobra.Command, args []string) {
RunInit()
RunE: func(cmd *cobra.Command, args []string) error {
return RunInit()
},
}

return cmd
}

// RunInit runs the init command
func RunInit() {
func RunInit() error {
log.Infof("Initializing %s", configFileName)

_, err := os.Stat(configFileName)
Expand All @@ -33,7 +35,9 @@ func RunInit() {
}

f, err := os.Create(configFileName)
failOnError(err, "Could not create the file")
if err != nil {
return wErrors.Wrap(err, "could not create file")
}

e := toml.NewEncoder(bufio.NewWriter(f))
err = e.Encode(config.Spec{
Expand Down Expand Up @@ -67,7 +71,11 @@ func RunInit() {
},
},
})
failOnError(err, "Could not encode config")
if err != nil {
return wErrors.Wrap(err, "could not encode config")
}

log.Infof("Created %s!", configFileName)

return nil
}
25 changes: 9 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/hellofresh/klepto/pkg/config"
"github.com/hellofresh/klepto/pkg/formatter"
"github.com/pkg/errors"
wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -55,32 +55,25 @@ func initConfig(c *cobra.Command, args []string) error {
// Use config file from the flag.
viper.SetConfigFile(configFile)
} else {
cwd, err := os.Getwd()
if err != nil {
return wErrors.Wrap(err, "can't find current working directory")
}

viper.SetConfigName(".klepto")
viper.AddConfigPath(workingDir())
viper.AddConfigPath(cwd)
viper.AddConfigPath(".")
}

err := viper.ReadInConfig()
if err != nil {
return errors.Wrap(err, "Could not read configurations")
return wErrors.Wrap(err, "could not read configurations")
}

err = viper.Unmarshal(&globalConfig)
if err != nil {
return errors.Wrap(err, "Could not unmarshal config file")
return wErrors.Wrap(err, "could not unmarshal config file")
}

return nil
}
func workingDir() string {
cwd, err := os.Getwd()
failOnError(err, "Can't find the working directory")

return cwd
}

func failOnError(err error, message string) {
if err != nil {
log.WithError(err).Fatal(message)
}
}
29 changes: 22 additions & 7 deletions cmd/steal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hellofresh/klepto/pkg/anonymiser"
"github.com/hellofresh/klepto/pkg/dumper"
"github.com/hellofresh/klepto/pkg/reader"
wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -64,16 +65,24 @@ func NewStealCmd() *cobra.Command {
// RunSteal is the handler for the rootCmd.
func RunSteal(opts *StealOptions) (err error) {
readTimeout, err := time.ParseDuration(opts.readOpts.timeout)
failOnError(err, "Failed to parse read timeout duration")
if err != nil {
return wErrors.Wrap(err, "Failed to parse read timeout duration")
}

writeTimeout, err := time.ParseDuration(opts.readOpts.timeout)
failOnError(err, "Failed to parse write timeout duration")
if err != nil {
return wErrors.Wrap(err, "Failed to parse write timeout duration")
}

readMaxConnLifetime, err := time.ParseDuration(opts.readOpts.maxConnLifetime)
failOnError(err, "Failed to parse the timeout duration")
if err != nil {
return wErrors.Wrap(err, "Failed to parse write timeout duration")
}

writeMaxConnLifetime, err := time.ParseDuration(opts.writeOpts.maxConnLifetime)
failOnError(err, "Failed to parse the timeout duration")
if err != nil {
return wErrors.Wrap(err, "Failed to parse the timeout duration")
}

source, err := reader.Connect(reader.ConnOpts{
DSN: opts.from,
Expand All @@ -82,7 +91,9 @@ func RunSteal(opts *StealOptions) (err error) {
MaxConns: opts.readOpts.maxConns,
MaxIdleConns: opts.readOpts.maxIdleConns,
})
failOnError(err, "Error connecting to reader")
if err != nil {
return wErrors.Wrap(err, "Could not connecting to reader")
}
defer source.Close()

source = anonymiser.NewAnonymiser(source, globalConfig.Tables)
Expand All @@ -93,15 +104,19 @@ func RunSteal(opts *StealOptions) (err error) {
MaxConns: opts.writeOpts.maxConns,
MaxIdleConns: opts.writeOpts.maxIdleConns,
}, source)
failOnError(err, "Error creating dumper")
if err != nil {
return wErrors.Wrap(err, "Error creating dumper")
}
defer target.Close()

log.Info("Stealing...")

done := make(chan struct{})
defer close(done)
start := time.Now()
failOnError(target.Dump(done, globalConfig, opts.concurrency), "Error while dumping")
if err := target.Dump(done, globalConfig, opts.concurrency); err != nil {
return wErrors.Wrap(err, "Error while dumping")
}

<-done
log.WithField("total_time", time.Since(start)).Info("Done!")
Expand Down
18 changes: 12 additions & 6 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hellofresh/updater-go"
"github.com/palantir/stacktrace"
wErrors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -31,8 +32,8 @@ func NewUpdateCmd() *cobra.Command {
Use: "update",
Aliases: []string{"self-update"},
Short: "Check for new versions of kepto",
Run: func(cmd *cobra.Command, args []string) {
RunUpdate(opts)
RunE: func(cmd *cobra.Command, args []string) error {
return RunUpdate(opts)
},
}

Expand All @@ -44,7 +45,7 @@ func NewUpdateCmd() *cobra.Command {
}

// RunUpdate runs the update command
func RunUpdate(opts *UpdateOptions) {
func RunUpdate(opts *UpdateOptions) error {
log.Info("Checking for new versions of Klepto!")

if opts.token == "" {
Expand All @@ -69,19 +70,24 @@ func RunUpdate(opts *UpdateOptions) {
// fatal exits with code 1
log.Fatal("Unable to access the Klepto! repository.")
}
failOnError(err, "failed to retrieve the update release")
if err != nil {
return wErrors.Wrap(err, "failed to retrieve the update release")
}

if updateTo.Name != version {
// Fetch the release and update
if !opts.dryRun {
err = updater.SelfUpdate(updateTo)
failOnError(err, "failed to update to version %s")
if err := updater.SelfUpdate(updateTo); err != nil {
return wErrors.Wrapf(err, "failed to update to version %s", updateTo.Name)
}
}

log.Infof("Klepto! updated to version %s", updateTo.Name)
} else {
log.Infof("No updates available for your version %s", version)
}

return nil
}

func newReleaseLocator(token string, filter updater.ReleaseFilter) updater.ReleaseLocator {
Expand Down
6 changes: 3 additions & 3 deletions features/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (s *MysqlTestSuite) createDatabase(name string) string {
_, err := s.rootConnection.Exec(fmt.Sprintf("CREATE DATABASE %s", name))
s.Require().NoError(err, "Unable to create db")

dbUrl, _ := mysql.ParseDSN(s.rootDSN)
dbUrl.DBName = name
dbURL, _ := mysql.ParseDSN(s.rootDSN)
dbURL.DBName = name

return dbUrl.FormatDSN()
return dbURL.FormatDSN()
}

func (s *MysqlTestSuite) dropDatabase(name string) {
Expand Down
6 changes: 3 additions & 3 deletions features/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (s *PostgresTestSuite) createDatabase(name string) string {
_, err := s.rootConnection.Exec(fmt.Sprintf("CREATE DATABASE %s", name))
s.Require().NoError(err, "Unable to create db")

dbUrl, _ := url.Parse(s.rootDSN)
dbUrl.Path = name
return dbUrl.String()
dbURL, _ := url.Parse(s.rootDSN)
dbURL.Path = name
return dbURL.String()
}

func (s *PostgresTestSuite) dropDatabase(name string) {
Expand Down
Loading

0 comments on commit a295b92

Please sign in to comment.