-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pkg/command): add a options struct to store the values provided …
…by the user through the flags
- Loading branch information
1 parent
6197d52
commit 8848566
Showing
4 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package command | ||
|
||
import "os" | ||
|
||
// Options is a struct that stores the provided commands by the user | ||
type Options struct { | ||
Driver string | ||
URL string | ||
Host string | ||
Port string | ||
User string | ||
Pass string | ||
DBName string | ||
SSL string | ||
} | ||
|
||
// SetDefault returns a Options struct and fills the empty | ||
// values with environment variables if any. | ||
func SetDefault(opts Options) Options { | ||
if opts.URL == "" { | ||
opts.URL = os.Getenv("DATABASE_URL") | ||
} | ||
|
||
if opts.Host == "" { | ||
opts.Host = os.Getenv("DB_HOST") | ||
} | ||
|
||
if opts.User == "" { | ||
opts.User = os.Getenv("DB_USER") | ||
} | ||
|
||
if opts.Pass == "" { | ||
opts.Pass = os.Getenv("DB_PASS") | ||
} | ||
|
||
if opts.DBName == "" { | ||
opts.DBName = os.Getenv("DB_NAME") | ||
} | ||
|
||
if opts.Port == "" { | ||
opts.Port = os.Getenv("DB_PORT") | ||
} | ||
|
||
return opts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package command | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetEnvValues(t *testing.T) { | ||
if err := os.Setenv("DATABASE_URL", "postgres://user:password@host:port/database?sslmode=disable"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := os.Setenv("DB_HOST", "localhost"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := os.Setenv("DB_USER", "user"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := os.Setenv("DB_PASS", "password"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := os.Setenv("DB_NAME", "database"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := os.Setenv("DB_PORT", "5432"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
opts := Options{} | ||
|
||
result := SetDefault(opts) | ||
|
||
assert.Equal(t, "postgres://user:password@host:port/database?sslmode=disable", result.URL) | ||
assert.Equal(t, "localhost", result.Host) | ||
assert.Equal(t, "user", result.User) | ||
assert.Equal(t, "password", result.Pass) | ||
assert.Equal(t, "database", result.DBName) | ||
assert.Equal(t, "5432", result.Port) | ||
} |