-
Notifications
You must be signed in to change notification settings - Fork 119
chore: optimize make:command for auto-generated Signature and simplified method receivers #821
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,25 +23,25 @@ func NewKeyGenerateCommand(config config.Config) *KeyGenerateCommand { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Signature The name and signature of the console command. | ||||||||||||||||||||||
| func (receiver *KeyGenerateCommand) Signature() string { | ||||||||||||||||||||||
| func (r *KeyGenerateCommand) Signature() string { | ||||||||||||||||||||||
| return "key:generate" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Description The console command description. | ||||||||||||||||||||||
| func (receiver *KeyGenerateCommand) Description() string { | ||||||||||||||||||||||
| func (r *KeyGenerateCommand) Description() string { | ||||||||||||||||||||||
| return "Set the application key" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Extend The console command extend. | ||||||||||||||||||||||
| func (receiver *KeyGenerateCommand) Extend() command.Extend { | ||||||||||||||||||||||
| func (r *KeyGenerateCommand) Extend() command.Extend { | ||||||||||||||||||||||
| return command.Extend{ | ||||||||||||||||||||||
| Category: "key", | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Handle Execute the console command. | ||||||||||||||||||||||
| func (receiver *KeyGenerateCommand) Handle(ctx console.Context) error { | ||||||||||||||||||||||
| if receiver.config.GetString("app.env") == "production" { | ||||||||||||||||||||||
| func (r *KeyGenerateCommand) Handle(ctx console.Context) error { | ||||||||||||||||||||||
| if r.config.GetString("app.env") == "production" { | ||||||||||||||||||||||
| color.Warningln("**************************************") | ||||||||||||||||||||||
| color.Warningln("* Application In Production! *") | ||||||||||||||||||||||
| color.Warningln("**************************************") | ||||||||||||||||||||||
|
|
@@ -58,8 +58,8 @@ func (receiver *KeyGenerateCommand) Handle(ctx console.Context) error { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| key := receiver.generateRandomKey() | ||||||||||||||||||||||
| if err := receiver.writeNewEnvironmentFileWith(key); err != nil { | ||||||||||||||||||||||
| key := r.generateRandomKey() | ||||||||||||||||||||||
| if err := r.writeNewEnvironmentFileWith(key); err != nil { | ||||||||||||||||||||||
| ctx.Error(err.Error()) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
|
|
@@ -71,18 +71,18 @@ func (receiver *KeyGenerateCommand) Handle(ctx console.Context) error { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // generateRandomKey Generate a random key for the application. | ||||||||||||||||||||||
| func (receiver *KeyGenerateCommand) generateRandomKey() string { | ||||||||||||||||||||||
| func (r *KeyGenerateCommand) generateRandomKey() string { | ||||||||||||||||||||||
| return str.Random(32) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+74
to
76
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider using crypto/rand for application key generation. Similar to the JWT secret generation, the application key should use cryptographically secure random number generation. -func (r *KeyGenerateCommand) generateRandomKey() string {
- return str.Random(32)
+func (r *KeyGenerateCommand) generateRandomKey() string {
+ bytes := make([]byte, 32)
+ if _, err := rand.Read(bytes); err != nil {
+ return str.Random(32) // Fallback to existing implementation
+ }
+ return hex.EncodeToString(bytes)
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // writeNewEnvironmentFileWith Write a new environment file with the given key. | ||||||||||||||||||||||
| func (receiver *KeyGenerateCommand) writeNewEnvironmentFileWith(key string) error { | ||||||||||||||||||||||
| func (r *KeyGenerateCommand) writeNewEnvironmentFileWith(key string) error { | ||||||||||||||||||||||
| content, err := os.ReadFile(support.EnvPath) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| newContent := strings.Replace(string(content), "APP_KEY="+receiver.config.GetString("app.key"), "APP_KEY="+key, 1) | ||||||||||||||||||||||
| newContent := strings.Replace(string(content), "APP_KEY="+r.config.GetString("app.key"), "APP_KEY="+key, 1) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| err = os.WriteFile(support.EnvPath, []byte(newContent), 0644) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "github.com/goravel/framework/contracts/console/command" | ||
| supportconsole "github.com/goravel/framework/support/console" | ||
| "github.com/goravel/framework/support/file" | ||
| "github.com/goravel/framework/support/str" | ||
| ) | ||
|
|
||
| type MakeCommand struct { | ||
|
|
@@ -18,31 +19,31 @@ func NewMakeCommand() *MakeCommand { | |
| } | ||
|
|
||
| // Signature The name and signature of the console command. | ||
| func (receiver *MakeCommand) Signature() string { | ||
| func (r *MakeCommand) Signature() string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Add test coverage for command interface methods The following methods in
While 🔗 Analysis chainVerify test coverage for the modified methods. While the changes look good, static analysis indicates that some of the modified methods lack test coverage. Run this script to analyze test coverage: Also applies to: 27-27, 32-32, 39-39, 46-46, 55-55, 60-60, 63-63 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check test coverage for modified methods
# Search for existing tests
rg -l "TestMakeCommand" --type go
# Check if methods are covered in tests
rg "func.*MakeCommand.*Signature|func.*MakeCommand.*Description|func.*MakeCommand.*Extend" --type go
Length of output: 5171 Script: #!/bin/bash
# Check test coverage in make_command_test.go
echo "=== Content of make_command_test.go ==="
cat console/console/make_command_test.go
echo -e "\n=== Searching for specific test cases ==="
rg "func Test.*MakeCommand.*(Signature|Description|Extend)" console/console/make_command_test.go
Length of output: 2329 Script: #!/bin/bash
# Check other test files for coverage
echo "=== Content of foundation/console/test_make_command_test.go ==="
cat foundation/console/test_make_command_test.go
echo -e "\n=== Checking foundation/application.go for tests ==="
cat foundation/application.go | grep -A 10 "TestMakeCommand"
Length of output: 2491 🧰 Tools🪛 GitHub Check: codecov/patch[warning] 22-22: console/console/make_command.go#L22 |
||
| return "make:command" | ||
| } | ||
|
|
||
| // Description The console command description. | ||
| func (receiver *MakeCommand) Description() string { | ||
| func (r *MakeCommand) Description() string { | ||
| return "Create a new Artisan command" | ||
| } | ||
|
|
||
| // Extend The console command extend. | ||
| func (receiver *MakeCommand) Extend() command.Extend { | ||
| func (r *MakeCommand) Extend() command.Extend { | ||
| return command.Extend{ | ||
| Category: "make", | ||
| } | ||
| } | ||
|
|
||
| // Handle Execute the console command. | ||
| func (receiver *MakeCommand) Handle(ctx console.Context) error { | ||
| func (r *MakeCommand) Handle(ctx console.Context) error { | ||
| m, err := supportconsole.NewMake(ctx, "command", ctx.Argument(0), filepath.Join("app", "console", "commands")) | ||
| if err != nil { | ||
| ctx.Error(err.Error()) | ||
| return nil | ||
| } | ||
|
|
||
| if err := file.Create(m.GetFilePath(), receiver.populateStub(receiver.getStub(), m.GetPackageName(), m.GetStructName())); err != nil { | ||
| if err := file.Create(m.GetFilePath(), r.populateStub(r.getStub(), m.GetPackageName(), m.GetStructName())); err != nil { | ||
| return err | ||
| } | ||
|
|
||
|
|
@@ -51,14 +52,15 @@ func (receiver *MakeCommand) Handle(ctx console.Context) error { | |
| return nil | ||
| } | ||
|
|
||
| func (receiver *MakeCommand) getStub() string { | ||
| func (r *MakeCommand) getStub() string { | ||
| return Stubs{}.Command() | ||
| } | ||
|
|
||
| // populateStub Populate the place-holders in the command stub. | ||
| func (receiver *MakeCommand) populateStub(stub string, packageName, structName string) string { | ||
| func (r *MakeCommand) populateStub(stub string, packageName, structName string) string { | ||
| stub = strings.ReplaceAll(stub, "DummyCommand", structName) | ||
| stub = strings.ReplaceAll(stub, "DummyPackage", packageName) | ||
| stub = strings.ReplaceAll(stub, "DummySignature", str.Of(structName).Kebab().Prepend("app:").String()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that Laravel adds an app: prefix for user-created commands, and I think it’s quite reasonable as it helps distinguish custom commands from framework or package-provided ones, making the command structure more organized. |
||
|
|
||
| return stub | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using crypto/rand for JWT secret generation.
The current implementation uses
str.Randomwhich might not provide cryptographically secure randomness needed for JWT secrets.