Skip to content

Commit

Permalink
ENV vars refactoring with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
bayucandra committed Nov 15, 2019
1 parent 754db2f commit e74499f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
16 changes: 8 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
PG_HOST=localhost
PG_PORT=5432
PG_USER=postgres
PG_PASSWORD=s3cr3tp4sw0rd
PG_DBNAME=db_name
PG_SCHEMA=public
PG_SSLMODE=disable
GO_SEED_PG_HOST=localhost
GO_SEED_PG_PORT=5432
GO_SEED_PG_USER=postgres
GO_SEED_PG_PASSWORD=s3cr3t
GO_SEED_PG_DBNAME=db_name
GO_SEED_PG_SCHEMA=public
GO_SEED_PG_SSLMODE=disable

SOURCE_PATH=seed-data
GO_SEED_SOURCE_PATH=seed-data
12 changes: 6 additions & 6 deletions db/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func Init() {

func connect() (db *sql.DB, err error) {
var (
host = os.Getenv("PG_HOST")
port = os.Getenv("PG_PORT")
user = os.Getenv("PG_USER")
password = os.Getenv("PG_PASSWORD")
dbname = os.Getenv("PG_DBNAME")
sslmode = os.Getenv("PG_SSLMODE")
host = os.Getenv("GO_SEED_PG_HOST")
port = os.Getenv("GO_SEED_PG_PORT")
user = os.Getenv("GO_SEED_PG_USER")
password = os.Getenv("GO_SEED_PG_PASSWORD")
dbname = os.Getenv("GO_SEED_PG_DBNAME")
sslmode = os.Getenv("GO_SEED_PG_SSLMODE")
)

if host == "docker-host" && os.Getenv("GO_ENV") == "development" {
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func loadEnv(fileName string) error {
func checkEnv(envNames ...string) (notfound string, err error) {

envVars := []string{
"PG_HOST",
"PG_PORT",
"PG_USER",
"PG_PASSWORD",
"PG_DBNAME",
"PG_SSLMODE",
"GO_SEED_PG_HOST",
"GO_SEED_PG_PORT",
"GO_SEED_PG_USER",
"GO_SEED_PG_PASSWORD",
"GO_SEED_PG_DBNAME",
"GO_SEED_PG_SSLMODE",
}

if len(envNames) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion seed-operations/sql-insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func sqlInsert( cols []string, record []string, table string ) (err error){
}

qry := fmt.Sprintf(
`INSERT INTO "%s"."%s" %s VALUES %s`, os.Getenv("PG_SCHEMA"),
`INSERT INTO "%s"."%s" %s VALUES %s`, os.Getenv("GO_SEED_PG_SCHEMA"),
table, colsStr, valStr)

res, err := db.DBConn.Exec( qry )
Expand Down

0 comments on commit e74499f

Please sign in to comment.