Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#491] Fix the errors thrown by Lint/nilaway CI #642

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

func (database *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]contractconsole.Command{
console.NewJwtSecretCommand(app.MakeConfig()),
console.NewPolicyMakeCommand(),
})
if artisanFacade := app.MakeArtisan(); artisanFacade != nil {
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
artisanFacade.Register([]contractconsole.Command{
console.NewJwtSecretCommand(app.MakeConfig()),
console.NewPolicyMakeCommand(),
})

Check warning on line 39 in auth/service_provider.go

View check run for this annotation

Codecov / codecov/patch

auth/service_provider.go#L35-L39

Added lines #L35 - L39 were not covered by tests
}
}
8 changes: 5 additions & 3 deletions cache/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]contractsconsole.Command{
console.NewClearCommand(app.MakeCache()),
})
if artisanFacade := app.MakeArtisan(); artisanFacade != nil {
artisanFacade.Register([]contractsconsole.Command{
console.NewClearCommand(app.MakeCache()),
})

Check warning on line 32 in cache/service_provider.go

View check run for this annotation

Codecov / codecov/patch

cache/service_provider.go#L29-L32

Added lines #L29 - L32 were not covered by tests
}
}
24 changes: 16 additions & 8 deletions console/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,28 @@

// Call Run an Artisan console command by name.
func (c *Application) Call(command string) {
commands := []string{os.Args[0]}
if c.isArtisan {
commands = append(commands, "artisan")
if len(os.Args) > 0 {
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
commands := []string{os.Args[0]}

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

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

// CallAndExit Run an Artisan console command by name and exit.
func (c *Application) CallAndExit(command string) {
commands := []string{os.Args[0]}
if c.isArtisan {
commands = append(commands, "artisan")
if len(os.Args) > 0 {
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
commands := []string{os.Args[0]}

Check warning on line 64 in console/application.go

View check run for this annotation

Codecov / codecov/patch

console/application.go#L63-L64

Added lines #L63 - L64 were not covered by tests

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

Check warning on line 67 in console/application.go

View check run for this annotation

Codecov / codecov/patch

console/application.go#L66-L67

Added lines #L66 - L67 were not covered by tests
}

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

Check warning on line 70 in console/application.go

View check run for this annotation

Codecov / codecov/patch

console/application.go#L70

Added line #L70 was not covered by tests
}
c.Run(append(commands, strings.Split(command, " ")...), true)
}

// Run a command. Args come from os.Args.
Expand Down
2 changes: 2 additions & 0 deletions console/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func TestFlagsToCliFlags(t *testing.T) {
// Convert command flags to CLI flags
cliFlags := flagsToCliFlags(flags)

assert.NotEmpty(t, cliFlags, "cliFlags should not be empty")
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved

// Assert that the number of CLI flags matches the number of command flags
assert.Equal(t, len(cliFlags), len(flags))

Expand Down
5 changes: 5 additions & 0 deletions console/console/key_generate_command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package console

import (
"errors"
"os"
"strings"

Expand Down Expand Up @@ -41,6 +42,10 @@

// Handle Execute the console command.
func (receiver *KeyGenerateCommand) Handle(ctx console.Context) error {
if receiver.config == nil {
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("config facade not set")

Check warning on line 46 in console/console/key_generate_command.go

View check run for this annotation

Codecov / codecov/patch

console/console/key_generate_command.go#L46

Added line #L46 was not covered by tests
}

if receiver.config.GetString("app.env") == "production" {
color.Yellow().Println("**************************************")
color.Yellow().Println("* Application In Production! *")
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 (r *ProgressBar) Advance(step ...int) {
var instance *pterm.ProgressbarPrinter

Check warning on line 22 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L22

Added line #L22 was not covered by tests

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

Check warning on line 25 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L25

Added line #L25 was not covered by tests
} else {
r.instance = r.instance.Increment()
instance = r.instance.Increment()

Check warning on line 27 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L27

Added line #L27 was not covered by tests
}

if instance != nil {
r.instance = instance

Check warning on line 31 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}

Expand Down
17 changes: 9 additions & 8 deletions console/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
}

func (receiver *ServiceProvider) registerCommands(app foundation.Application) {
artisan := app.MakeArtisan()
config := app.MakeConfig()
artisan.Register([]consolecontract.Command{
console.NewListCommand(artisan),
console.NewKeyGenerateCommand(config),
console.NewMakeCommand(),
console.NewBuildCommand(config),
})
if artisanFacade := app.MakeArtisan(); artisanFacade != nil {
config := app.MakeConfig()
artisanFacade.Register([]consolecontract.Command{
console.NewListCommand(artisanFacade),
console.NewKeyGenerateCommand(config),
console.NewMakeCommand(),
console.NewBuildCommand(config),
})

Check warning on line 35 in console/service_provider.go

View check run for this annotation

Codecov / codecov/patch

console/service_provider.go#L28-L35

Added lines #L28 - L35 were not covered by tests
}
}
24 changes: 18 additions & 6 deletions crypt/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"

"github.com/goravel/framework/contracts/config"
Expand All @@ -20,24 +21,35 @@
}

// 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) {
if config == nil {
return nil, ErrConfigNotSet

Check warning on line 26 in crypt/aes.go

View check run for this annotation

Codecov / codecov/patch

crypt/aes.go#L26

Added line #L26 was not covered by tests
}

if json == nil {
return nil, ErrJsonParserNotSet

Check warning on line 30 in crypt/aes.go

View check run for this annotation

Codecov / codecov/patch

crypt/aes.go#L30

Added line #L30 was not covered by tests
}
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved

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

Check warning on line 37 in crypt/aes.go

View check run for this annotation

Codecov / codecov/patch

crypt/aes.go#L37

Added line #L37 was not covered by tests
}

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)

Check warning on line 45 in crypt/aes.go

View check run for this annotation

Codecov / codecov/patch

crypt/aes.go#L43-L45

Added lines #L43 - L45 were not covered by tests
}

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")
)
2 changes: 1 addition & 1 deletion crypt/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

func (crypt *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
return NewAES(app.MakeConfig(), app.GetJson()), nil
return NewAES(app.MakeConfig(), app.GetJson())

Check warning on line 14 in crypt/service_provider.go

View check run for this annotation

Codecov / codecov/patch

crypt/service_provider.go#L14

Added line #L14 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should judge nil here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should do that here it is better to return error here it will be judged when calling MakeAES anyways.

})
}

Expand Down
6 changes: 4 additions & 2 deletions database/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
return nil, fmt.Errorf("init gorm dialector error: %v", err)
}

if err := r.init(writeDialectors[0]); err != nil {
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
if len(writeDialectors) > 0 {
if err := r.init(writeDialectors[0]); err != nil {
return nil, err

Check warning on line 57 in database/gorm/gorm.go

View check run for this annotation

Codecov / codecov/patch

database/gorm/gorm.go#L57

Added line #L57 was not covered by tests
}
}

if err := r.configurePool(); err != nil {
Expand Down
35 changes: 18 additions & 17 deletions database/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@
}

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(),
})

Check warning on line 63 in database/service_provider.go

View check run for this annotation

Codecov / codecov/patch

database/service_provider.go#L47-L63

Added lines #L47 - L63 were not covered by tests
}
}
10 changes: 6 additions & 4 deletions event/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
}

func (receiver *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]console.Command{
&eventConsole.EventMakeCommand{},
&eventConsole.ListenerMakeCommand{},
})
if artisanFacade := app.MakeArtisan(); artisanFacade != nil {
artisanFacade.Register([]console.Command{
&eventConsole.EventMakeCommand{},
&eventConsole.ListenerMakeCommand{},
})

Check warning on line 29 in event/service_provider.go

View check run for this annotation

Codecov / codecov/patch

event/service_provider.go#L25-L29

Added lines #L25 - L29 were not covered by tests
}
}
7 changes: 6 additions & 1 deletion facades/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ import (
)

func App() foundationcontract.Application {
return foundation.App
app := foundation.App
if app == nil {
panic(ErrApplicationNotSet)
}

return app
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading