diff --git a/config/application.go b/config/application.go index 30b3e8b5c..a7745c38c 100644 --- a/config/application.go +++ b/config/application.go @@ -19,28 +19,26 @@ type Application struct { } func NewApplication(envPath string) *Application { - if !file.Exists(envPath) { - color.Redln("Please create " + envPath + " and initialize it first.") - color.Warnln("Example command: \ncp .env.example .env && go run . artisan key:generate") - os.Exit(0) - } - app := &Application{} app.vip = viper.New() - app.vip.SetConfigType("env") - app.vip.SetConfigFile(envPath) app.vip.AutomaticEnv() - if err := app.vip.ReadInConfig(); err != nil { - color.Redln("Invalid Config error: " + err.Error()) - os.Exit(0) + if file.Exists(envPath) { + app.vip.SetConfigType("env") + app.vip.SetConfigFile(envPath) + + if err := app.vip.ReadInConfig(); err != nil { + color.Redln("Invalid Config error: " + err.Error()) + os.Exit(0) + } } appKey := app.Env("APP_KEY") - if support.Env != support.EnvArtisan { + if !support.IsKeyGenerateCommand { if appKey == nil { color.Redln("Please initialize APP_KEY first.") - color.Warnln("Example command: \ngo run . artisan key:generate") + color.Println("Create a .env file and run command: go run . artisan key:generate") + color.Println("Or set a system variable: APP_KEY={32-bit number} go run .") os.Exit(0) } diff --git a/config/application_test.go b/config/application_test.go index 86c001786..ee331eeac 100644 --- a/config/application_test.go +++ b/config/application_test.go @@ -24,7 +24,9 @@ DB_PORT=3306 `)) temp, err := os.CreateTemp("", "goravel.env") assert.Nil(t, err) + defer temp.Close() defer os.Remove(temp.Name()) + _, err = temp.Write([]byte(` APP_KEY=12345678901234567890123456789012 APP_DEBUG=true @@ -45,22 +47,6 @@ func (s *ApplicationTestSuite) SetupTest() { } -func (s *ApplicationTestSuite) TestOsVariables() { - s.Nil(os.Setenv("APP_KEY", "12345678901234567890123456789013")) - s.Nil(os.Setenv("OS_APP_NAME", "goravel")) - s.Nil(os.Setenv("OS_APP_PORT", "3306")) - s.Nil(os.Setenv("OS_APP_DEBUG", "true")) - - s.Equal("12345678901234567890123456789013", s.config.GetString("APP_KEY")) - s.Equal("12345678901234567890123456789013", s.customConfig.GetString("APP_KEY")) - s.Equal("goravel", s.config.GetString("OS_APP_NAME")) - s.Equal("goravel", s.customConfig.GetString("OS_APP_NAME")) - s.Equal(3306, s.config.GetInt("OS_APP_PORT")) - s.Equal(3306, s.customConfig.GetInt("OS_APP_PORT")) - s.True(s.config.GetBool("OS_APP_DEBUG")) - s.True(s.customConfig.GetBool("OS_APP_DEBUG")) -} - func (s *ApplicationTestSuite) TestEnv() { s.Equal("12345678901234567890123456789012", s.config.Env("APP_KEY").(string)) s.Equal("goravel", s.config.Env("APP_NAME", "goravel").(string)) @@ -137,3 +123,17 @@ func (s *ApplicationTestSuite) TestGetBool() { s.Equal(true, s.config.GetBool("APP_DEBUG")) s.Equal(true, s.customConfig.GetBool("APP_DEBUG")) } + +func TestOsVariables(t *testing.T) { + assert.Nil(t, os.Setenv("APP_KEY", "12345678901234567890123456789013")) + assert.Nil(t, os.Setenv("APP_NAME", "goravel")) + assert.Nil(t, os.Setenv("APP_PORT", "3306")) + assert.Nil(t, os.Setenv("APP_DEBUG", "true")) + + config := NewApplication(".env") + + assert.Equal(t, "12345678901234567890123456789013", config.GetString("APP_KEY")) + assert.Equal(t, "goravel", config.GetString("APP_NAME")) + assert.Equal(t, 3306, config.GetInt("APP_PORT")) + assert.True(t, config.GetBool("APP_DEBUG")) +} diff --git a/foundation/application.go b/foundation/application.go index d97487401..4348c796e 100644 --- a/foundation/application.go +++ b/foundation/application.go @@ -181,7 +181,9 @@ func setEnv() { for _, arg := range args[1:] { if arg == "artisan" { support.Env = support.EnvArtisan - break + } + if arg == "key:generate" { + support.IsKeyGenerateCommand = true } } } diff --git a/support/constant.go b/support/constant.go index adf3488aa..ace70b02e 100644 --- a/support/constant.go +++ b/support/constant.go @@ -9,8 +9,9 @@ const ( ) var ( - Env = EnvRuntime - EnvPath = ".env" - RelativePath string - RootPath string + Env = EnvRuntime + EnvPath = ".env" + IsKeyGenerateCommand = false + RelativePath string + RootPath string )