-
Notifications
You must be signed in to change notification settings - Fork 15
🚸 Execute go mod tidy
after basic creation
#123
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
WalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant User
participant createBasic as Creator
participant Shell as ShellCmd
User->>Creator: invoke createBasic()
Creator->>Shell: runCmd(execCommand "go mod init" modName)
alt init error
Shell-->>Creator: error
Creator-->>User: return error
else init ok
Creator->>Shell: runCmd(execCommand "go mod tidy") [in project dir]
alt tidy error
Shell-->>Creator: error
Creator-->>User: return error
else tidy ok
Creator-->>User: success
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
cmd/new.go (1)
81-91
: Consider enhancing command execution robustnessA few suggestions to improve the implementation:
- Add timeouts to prevent hanging on slow network connections
- Provide more descriptive error messages
- Update the success message to mention module installation
Example implementation with timeout:
func createBasic(projectPath, modName string) (err error) { // create main.go if err = createFile(fmt.Sprintf("%s%cmain.go", projectPath, os.PathSeparator), newBasicTemplate); err != nil { return } - if err = runCmd(execCommand("go", "mod", "init", modName)); err != nil{ + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + initCmd := execCommand("go", "mod", "init", modName) + initCmd.WaitDelay = ctx.Done() + if err = runCmd(initCmd); err != nil { + return fmt.Errorf("failed to initialize module: %w", err) } - installModules := execCommand("go", "mod", "tidy") + installModules := execCommand("go", "mod", "tidy") + installModules.WaitDelay = ctx.Done() installModules.Dir = projectPath - if err = runCmd(installModules); err != nil{ + if err = runCmd(installModules); err != nil { + return fmt.Errorf("failed to install dependencies: %w", err) } return }Also consider updating the success message template to mention module installation:
newSuccessTemplate = ` Scaffolding project in %s (module %s) + ✓ Created project structure + ✓ Initialized Go module + ✓ Installed dependencies Done. Now run: cd %s fiber dev ✨ Done in %s. `
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
cmd/new.go
(1 hunks)
🔇 Additional comments (2)
cmd/new.go (2)
81-83
: LGTM: Proper error handling for module initialization
The error handling for go mod init
is well-implemented and follows Go's idiomatic patterns.
81-91
: Verify error handling and cleanup
Let's verify that errors are properly propagated and the project directory is cleaned up on failure.
✅ Verification successful
Let me gather more information about the error handling in the parent function.
Error handling and cleanup are properly implemented
The code has robust error handling and cleanup mechanisms:
- The parent function has a deferred cleanup that removes the project directory on any error
runCmd
properly handles command execution with stderr/stdout pipes and proper cleanup- All critical operations (mkdir, chdir, go mod commands) have error checks
- The project directory is cleaned up if any step in the initialization fails
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify error handling and cleanup implementation
# Check if the defer cleanup is triggered on errors
rg -A 5 "defer func\(\)" cmd/
# Check if the parent function handles errors from module commands
rg "if err = create\w+\(" cmd/
Length of output: 4780
Script:
#!/bin/bash
# Check the parent function implementation and error handling
rg -B 5 -A 10 "createProject\(" cmd/
# Check the runCmd implementation
rg -B 5 -A 10 "func runCmd\(" cmd/
Length of output: 1575
//Execute go mod tidy in the project directory | ||
installModules := execCommand("go", "mod", "tidy") | ||
installModules.Dir = fmt.Sprintf("%s%c", projectPath, os.PathSeparator) | ||
if err = runCmd(installModules); err != nil{ | ||
return | ||
} |
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
Fix directory path construction
The directory path construction adds an extra separator character. The projectPath
already contains the full path to the project.
Simplify the directory path assignment:
- installModules.Dir = fmt.Sprintf("%s%c", projectPath, os.PathSeparator)
+ installModules.Dir = projectPath
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
//Execute go mod tidy in the project directory | |
installModules := execCommand("go", "mod", "tidy") | |
installModules.Dir = fmt.Sprintf("%s%c", projectPath, os.PathSeparator) | |
if err = runCmd(installModules); err != nil{ | |
return | |
} | |
//Execute go mod tidy in the project directory | |
installModules := execCommand("go", "mod", "tidy") | |
installModules.Dir = projectPath | |
if err = runCmd(installModules); err != nil{ | |
return | |
} |
go mod tidy
after basic creationgo mod tidy
after basic creation
Please provide enough information so that others can review your pull request:
Fix to #122
Explain the details for making this change. What existing problem does the pull request solve?
The expected use case for
fiber new <name_project>
is a working and ready to go setup. With this PR i propose to automatically install the required modules usinggo mod tidy
Summary by CodeRabbit
New Features
Bug Fixes