Skip to content

Commit

Permalink
refactor(command): add a db driver env var to the function
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Apr 11, 2021
1 parent 8848566 commit 0d8ff58
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package command

import "os"

// Options is a struct that stores the provided commands by the user
// Options is a struct that stores the provided commands by the user.
type Options struct {
Driver string
URL string
Expand All @@ -21,6 +21,10 @@ func SetDefault(opts Options) Options {
opts.URL = os.Getenv("DATABASE_URL")
}

if opts.Driver == "" {
opts.Driver = os.Getenv("DB_DRIVER")
}

if opts.Host == "" {
opts.Host = os.Getenv("DB_HOST")
}
Expand All @@ -30,7 +34,7 @@ func SetDefault(opts Options) Options {
}

if opts.Pass == "" {
opts.Pass = os.Getenv("DB_PASS")
opts.Pass = os.Getenv("DB_PASSWORD")
}

if opts.DBName == "" {
Expand Down
6 changes: 5 additions & 1 deletion pkg/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func TestSetEnvValues(t *testing.T) {
t.Fatal(err)
}

if err := os.Setenv("DB_DRIVER", "postgres"); err != nil {
t.Fatal(err)
}

if err := os.Setenv("DB_HOST", "localhost"); err != nil {
t.Fatal(err)
}
Expand All @@ -20,7 +24,7 @@ func TestSetEnvValues(t *testing.T) {
t.Fatal(err)
}

if err := os.Setenv("DB_PASS", "password"); err != nil {
if err := os.Setenv("DB_PASSWORD", "password"); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit 0d8ff58

Please sign in to comment.