Skip to content

Commit

Permalink
chore: remove build faile if env file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashchoudhary07 committed Sep 7, 2024
1 parent 389fe6a commit beb6ccf
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions config/core-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,39 @@ import (
)

var (
Env string
UserMaxUrlCount int
Domain string
AuthRedirectUrl string
DbUrl string
Env string
UserMaxUrlCount int
Domain string
AuthRedirectUrl string
DbUrl string
DbMaxOpenConnections int
GoogleClientId string
GoogleClientSecret string
GoogleRedirectUrl string
TokenValidity int
JwtSecret string
JwtValidity int
JwtIssuer string
AllowedOrigin string
Port string
GoogleClientId string
GoogleClientSecret string
GoogleRedirectUrl string
TokenValidity int
JwtSecret string
JwtValidity int
JwtIssuer string
AllowedOrigin string
Port string
)

func findAndLoadEnv(envFile string) {
func findAndLoadEnv(envFile string) error {
cwd, err := os.Getwd()
if err != nil {
logger.Fatal("Could not get current working directory:", err)
}

logger.Info("Starting search for .env file from:", cwd)

for {
envPath := filepath.Join(cwd, envFile)
if _, err := os.Stat(envPath); err == nil {
if err := godotenv.Load(envPath); err != nil {
logger.Fatal("Error loading .env file:", err)
logger.Error("Error loading .env file:", err)
}
logger.Info("Loaded environment variables from:", envPath)
return
return nil
}

parent := filepath.Dir(cwd)
Expand All @@ -51,7 +53,7 @@ func findAndLoadEnv(envFile string) {
cwd = parent
}

logger.Fatal("Could not find .env file at:", envFile)
return fmt.Errorf("could not find .env file: %s", envFile)
}

func loadEnv() {
Expand All @@ -65,7 +67,10 @@ func loadEnv() {
envFile = "environments/test.env"
}

findAndLoadEnv(envFile)
if err := findAndLoadEnv(envFile); err != nil {
logger.Error("Failed to load .env file:", err)
return
}
}

func init() {
Expand Down Expand Up @@ -109,6 +114,7 @@ func getEnvVar(key string) string {
value := os.Getenv(key)
if value == "" {
logger.Fatal(fmt.Sprintf("Environment variable %s not set", key))
os.Exit(1)
}
return value
}
Expand Down

0 comments on commit beb6ccf

Please sign in to comment.