Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c78885a
solve potential nil error for console
krishankumar01 Sep 12, 2024
610ac59
solve potential nil error for progress bar
krishankumar01 Sep 12, 2024
51de4fe
solve potential nil error for str.When
krishankumar01 Sep 12, 2024
b4ff711
resolve nil error for filesystem
krishankumar01 Sep 12, 2024
45b921e
resolve nil errors
krishankumar01 Sep 12, 2024
07dd54a
resolve nil errors
krishankumar01 Sep 12, 2024
58d507a
resolve nil errors
krishankumar01 Sep 12, 2024
188012d
fix:lint error
krishankumar01 Sep 12, 2024
7f0dbc9
fix:test error
krishankumar01 Sep 12, 2024
e0d739c
fix nil error
krishankumar01 Sep 12, 2024
6495460
fix nil error
krishankumar01 Sep 12, 2024
8f402dd
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 12, 2024
d04bada
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 13, 2024
4148d38
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 14, 2024
a94e968
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 15, 2024
e359bb7
fix nilaway erro
krishankumar01 Sep 15, 2024
1599d09
fix nil errors
krishankumar01 Sep 15, 2024
c9670a9
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 16, 2024
70f858b
fix nil errors
krishankumar01 Sep 17, 2024
9393c30
optimize file facade setting
krishankumar01 Sep 17, 2024
f131c24
.
krishankumar01 Sep 17, 2024
066ae54
.
krishankumar01 Sep 17, 2024
209f95d
.
krishankumar01 Sep 17, 2024
00616ce
remove nil error from facades
krishankumar01 Sep 17, 2024
e9cbd2e
update crypt facade
krishankumar01 Sep 17, 2024
c670ff7
nilaway:fix for session
krishankumar01 Sep 17, 2024
0ed6949
nilaway:fix for file
krishankumar01 Sep 17, 2024
5b43804
nilaway:fix for file
krishankumar01 Sep 17, 2024
fa97fd2
nilaway:fix for json
krishankumar01 Sep 17, 2024
4a2f4aa
fix:lint
krishankumar01 Sep 18, 2024
f505eed
fix:test
krishankumar01 Sep 18, 2024
0f06b18
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 24, 2024
8e75482
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 26, 2024
c01fbc9
resolve comments
krishankumar01 Sep 27, 2024
70f0754
fix:test
krishankumar01 Sep 27, 2024
58b5a40
fix:nilaway
krishankumar01 Sep 27, 2024
1697335
update recommendation
krishankumar01 Sep 29, 2024
bdcffb4
update aes registration
krishankumar01 Sep 29, 2024
bb2b1af
.
krishankumar01 Sep 29, 2024
403dc12
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 29, 2024
346ae15
Merge branch 'master' into kkumar-gcc/#491
krishankumar01 Sep 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (database *ServiceProvider) Boot(app foundation.Application) {
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]contractconsole.Command{
app.Commands([]contractconsole.Command{
console.NewJwtSecretCommand(app.MakeConfig()),
console.NewPolicyMakeCommand(),
})
Expand Down
2 changes: 1 addition & 1 deletion cache/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (database *ServiceProvider) Boot(app foundation.Application) {
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]contractsconsole.Command{
app.Commands([]contractsconsole.Command{
console.NewClearCommand(app.MakeCache()),
})
}
12 changes: 12 additions & 0 deletions console/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,31 @@ func (c *Application) Register(commands []console.Command) {

// Call Run an Artisan console command by name.
func (c *Application) Call(command string) {
if len(os.Args) == 0 {
return
}

commands := []string{os.Args[0]}

if c.isArtisan {
commands = append(commands, "artisan")
}

c.Run(append(commands, strings.Split(command, " ")...), false)
}

// CallAndExit Run an Artisan console command by name and exit.
func (c *Application) CallAndExit(command string) {
if len(os.Args) == 0 {
return
}

commands := []string{os.Args[0]}

if c.isArtisan {
commands = append(commands, "artisan")
}

c.Run(append(commands, strings.Split(command, " ")...), true)
}

Expand Down
10 changes: 8 additions & 2 deletions console/progress_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ func NewProgressBar(total int) *ProgressBar {
}

func (r *ProgressBar) Advance(step ...int) {
var instance *pterm.ProgressbarPrinter

if len(step) > 0 {
r.instance = r.instance.Add(step[0])
instance = r.instance.Add(step[0])
} else {
r.instance = r.instance.Increment()
instance = r.instance.Increment()
}

if instance != nil {
r.instance = instance
}
}

Expand Down
23 changes: 17 additions & 6 deletions console/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/goravel/framework/console/console"
consolecontract "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/support/color"
)

const Binding = "goravel.console"
Expand All @@ -25,12 +26,22 @@ func (receiver *ServiceProvider) Boot(app foundation.Application) {
}

func (receiver *ServiceProvider) registerCommands(app foundation.Application) {
artisan := app.MakeArtisan()
config := app.MakeConfig()
artisan.Register([]consolecontract.Command{
console.NewListCommand(artisan),
console.NewKeyGenerateCommand(config),
artisanFacade := app.MakeArtisan()
if artisanFacade == nil {
color.Yellow().Println("Warning: Artisan Facade is not initialized. Skipping command registration.")
return
}

configFacade := app.MakeConfig()
if configFacade == nil {
color.Yellow().Println("Warning: Config Facade is not initialized. Skipping certain command registrations.")
return
}

artisanFacade.Register([]consolecontract.Command{
console.NewListCommand(artisanFacade),
console.NewKeyGenerateCommand(configFacade),
console.NewMakeCommand(),
console.NewBuildCommand(config),
console.NewBuildCommand(configFacade),
})
}
16 changes: 10 additions & 6 deletions crypt/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"

"github.com/goravel/framework/contracts/config"
Expand All @@ -20,24 +21,27 @@ type AES struct {
}

// NewAES returns a new AES hasher.
func NewAES(config config.Config, json foundation.Json) *AES {
func NewAES(config config.Config, json foundation.Json) (*AES, error) {
key := config.GetString("app.key")

// Don't use AES in artisan when the key is empty.
if support.Env == support.EnvArtisan && len(key) == 0 {
return nil
return nil, ErrAppKeyNotSetInArtisan
}

keyLength := len(key)
// check key length before using it
if len(key) != 16 && len(key) != 24 && len(key) != 32 {
color.Red().Println("[Crypt] Empty or invalid APP_KEY, please reset it.\nExample command:\ngo run . artisan key:generate")
return nil
if keyLength != 16 && keyLength != 24 && keyLength != 32 {
color.Red().Printf("[Crypt] Invalid APP_KEY length. Expected 16, 24, or 32 bytes, but got %d bytes.\n", len(key))
color.Red().Println("Please reset it using the following command:\ngo run . artisan key:generate")
return nil, fmt.Errorf("%w: %d bytes", ErrInvalidAppKeyLength, keyLength)
}

keyBytes := []byte(key)
return &AES{
key: keyBytes,
json: json,
}
}, nil
}

// EncryptString encrypts the given string, and returns the iv and ciphertext as base64 encoded strings.
Expand Down
19 changes: 16 additions & 3 deletions crypt/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crypt
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/goravel/framework/foundation/json"
Expand All @@ -17,8 +18,12 @@ type AesTestSuite struct {
func TestAesTestSuite(t *testing.T) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes, err := NewAES(mockConfig, json.NewJson())

assert.NoError(t, err)

suite.Run(t, &AesTestSuite{
aes: NewAES(mockConfig, json.NewJson()),
aes: aes,
})
mockConfig.AssertExpectations(t)
}
Expand Down Expand Up @@ -61,7 +66,11 @@ func (s *AesTestSuite) TestDecryptString() {
func Benchmark_EncryptString(b *testing.B) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes := NewAES(mockConfig, json.NewJson())
aes, err := NewAES(mockConfig, json.NewJson())
if err != nil {
b.Fatal(err)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -75,7 +84,11 @@ func Benchmark_EncryptString(b *testing.B) {
func Benchmark_DecryptString(b *testing.B) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes := NewAES(mockConfig, json.NewJson())
aes, err := NewAES(mockConfig, json.NewJson())
if err != nil {
b.Fatal(err)
}

payload, err := aes.EncryptString("Goravel")
if err != nil {
b.Error(err)
Expand Down
12 changes: 12 additions & 0 deletions crypt/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package crypt

import (
"errors"
)

var (
ErrConfigNotSet = errors.New("config must not be nil")
ErrJsonParserNotSet = errors.New("JSON parser must not be nil")
ErrAppKeyNotSetInArtisan = errors.New("APP_KEY is required in artisan environment")
ErrInvalidAppKeyLength = errors.New("invalid APP_KEY length")
)
12 changes: 11 additions & 1 deletion crypt/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ type ServiceProvider struct {

func (crypt *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
return NewAES(app.MakeConfig(), app.GetJson()), nil
c := app.MakeConfig()
if c == nil {
return nil, ErrConfigNotSet
}

j := app.GetJson()
if j == nil {
return nil, ErrJsonParserNotSet
}

return NewAES(c, j)
})
}

Expand Down
12 changes: 8 additions & 4 deletions database/gorm/dialector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func (s *DialectorTestSuite) TestMysql() {
s.mockConfig.On("GetString", "database.connections.mysql.loc").
Return("Local").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(mysql.New(mysql.Config{
DSN: fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=%t&loc=%s&multiStatements=true",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "utf8mb4", true, "Local"),
}), dialectors[0])
s.Nil(err)
}

func (s *DialectorTestSuite) TestPostgres() {
Expand All @@ -62,20 +63,22 @@ func (s *DialectorTestSuite) TestPostgres() {
s.mockConfig.On("GetString", "database.connections.postgres.timezone").
Return("UTC").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(postgres.New(postgres.Config{
DSN: fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "disable", "UTC"),
}), dialectors[0])
s.Nil(err)
}

func (s *DialectorTestSuite) TestSqlite() {
dialector := NewDialectorImpl(s.mockConfig, orm.DriverSqlite.String())
s.mockConfig.On("GetString", "database.connections.sqlite.driver").
Return(orm.DriverSqlite.String()).Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Equal(sqlite.Open(fmt.Sprintf("%s?multi_stmts=true", s.config.Database)), dialectors[0])
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(sqlite.Open(fmt.Sprintf("%s?multi_stmts=true", s.config.Database)), dialectors[0])
}

func (s *DialectorTestSuite) TestSqlserver() {
Expand All @@ -85,9 +88,10 @@ func (s *DialectorTestSuite) TestSqlserver() {
s.mockConfig.On("GetString", "database.connections.sqlserver.charset").
Return("utf8mb4").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(sqlserver.New(sqlserver.Config{
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s&charset=%s&MultipleActiveResultSets=true",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "utf8mb4"),
}), dialectors[0])
s.Nil(err)
}
4 changes: 4 additions & 0 deletions database/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (r *GormImpl) Make() (*gormio.DB, error) {
return nil, fmt.Errorf("init gorm dialector error: %v", err)
}

if len(writeDialectors) == 0 {
return nil, errors.New("no write dialectors found")
}

if err := r.init(writeDialectors[0]); err != nil {
return nil, err
}
Expand Down
35 changes: 18 additions & 17 deletions database/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,22 @@ func (database *ServiceProvider) Boot(app foundation.Application) {
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
config := app.MakeConfig()
seeder := app.MakeSeeder()
artisan := app.MakeArtisan()
app.MakeArtisan().Register([]consolecontract.Command{
console.NewMigrateMakeCommand(config),
console.NewMigrateCommand(config),
console.NewMigrateRollbackCommand(config),
console.NewMigrateResetCommand(config),
console.NewMigrateRefreshCommand(config, artisan),
console.NewMigrateFreshCommand(config, artisan),
console.NewMigrateStatusCommand(config),
console.NewModelMakeCommand(),
console.NewObserverMakeCommand(),
console.NewSeedCommand(config, seeder),
console.NewSeederMakeCommand(),
console.NewFactoryMakeCommand(),
})
if artisanFacade := app.MakeArtisan(); artisanFacade != nil {
config := app.MakeConfig()
seeder := app.MakeSeeder()
artisanFacade.Register([]consolecontract.Command{
console.NewMigrateMakeCommand(config),
console.NewMigrateCommand(config),
console.NewMigrateRollbackCommand(config),
console.NewMigrateResetCommand(config),
console.NewMigrateRefreshCommand(config, artisanFacade),
console.NewMigrateFreshCommand(config, artisanFacade),
console.NewMigrateStatusCommand(config),
console.NewModelMakeCommand(),
console.NewObserverMakeCommand(),
console.NewSeedCommand(config, seeder),
console.NewSeederMakeCommand(),
console.NewFactoryMakeCommand(),
})
}
}
2 changes: 1 addition & 1 deletion event/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (receiver *ServiceProvider) Boot(app foundation.Application) {
}

func (receiver *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]console.Command{
app.Commands([]console.Command{
&eventConsole.EventMakeCommand{},
&eventConsole.ListenerMakeCommand{},
})
Expand Down
6 changes: 5 additions & 1 deletion facades/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ import (
)

func App() foundationcontract.Application {
return foundation.App
if foundation.App == nil {
panic(ErrApplicationNotSet)
} else {
return foundation.App
}
}
7 changes: 7 additions & 0 deletions facades/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package facades

import "errors"

var (
ErrApplicationNotSet = errors.New("application instance not initialized")
)
7 changes: 7 additions & 0 deletions filesystem/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package filesystem

import "errors"

var (
ErrStorageFacadeNotSet = errors.New("storage facade not set")
)
Loading