Closed danstis closed 8 months ago
PR Description updated to latest commit (https://github.com/danstis/rmstale/commit/7b61a6d4e888d5cc0bc1d16c99e1a3b928f70824)
⏱️ Estimated effort to review [1-5] | 2, because the changes are straightforward and localized to specific functionalities. The removal of an external dependency and its replacement with native code is clear, and the modifications in the go.mod and go.sum files are directly related to this change. |
🧪 Relevant tests | No |
🔍 Possible issues | User Input Validation: The new custom confirmation logic does not validate user input beyond checking for 'y' or 'Y'. Consider handling unexpected input more gracefully. |
Error Handling: The error from `fmt.Scanln` is logged but does not prevent further execution. This might not be an issue in the current context but could lead to unintended behavior in more complex scenarios. | |
🔒 Security concerns | No |
Category | Suggestions |
Enhancement |
Improve user input handling by allowing "yes" in addition to "y".___ **Consider handling the case where the user inputs "yes" or "Yes" in addition to "y" and "Y"for a more user-friendly experience. This can be done by converting the response to lowercase and then checking if it equals "y".** [rmstale.go [84]](https://github.com/danstis/rmstale/pull/197/files#diff-8d2045f56d565d537deafa629d12ea2b52f0701a7366f155b4b1038745e58e4eR84-R84) ```diff -if response != "y" && response != "Y" { +if strings.ToLower(response) != "y" { ``` |
Ensure consistent flag usage messaging by using
___
**Replace | |
Add a brief sleep before reading user input to improve visibility of the warning message.___ **Consider adding a brief sleep after printing the warning message and before reading theuser input. This can help ensure that the message is fully visible to the user before the program waits for input, improving user experience on some terminals.** [rmstale.go [77-79]](https://github.com/danstis/rmstale/pull/197/files#diff-8d2045f56d565d537deafa629d12ea2b52f0701a7366f155b4b1038745e58e4eR77-R79) ```diff fmt.Printf("WARNING: Will remove files and folders recursively below '%v'%s older than %v days. Continue? (y/n) ", filepath.FromSlash(folder), extMsg, age) +time.Sleep(2 * time.Second) var response string _, err := fmt.Scanln(&response) ``` | |
Best practice |
Exit with a non-zero status code after logging an input read error.___ **After logging an error withlogger.Errorf when failing to read user input, consider exiting with a non-zero status code to indicate an error condition to the calling environment.** [rmstale.go [81-82]](https://github.com/danstis/rmstale/pull/197/files#diff-8d2045f56d565d537deafa629d12ea2b52f0701a7366f155b4b1038745e58e4eR81-R82) ```diff logger.Errorf("Failed to read user input: %v", err) -return +os.Exit(1) ``` |
Maintainability |
Extract user confirmation logic into a separate function for better readability.___ **To improve code readability and maintainability, consider extracting the user confirmationlogic into a separate function. This would make the main function cleaner and the logic reusable.** [rmstale.go [77-87]](https://github.com/danstis/rmstale/pull/197/files#diff-8d2045f56d565d537deafa629d12ea2b52f0701a7366f155b4b1038745e58e4eR77-R87) ```diff -fmt.Printf("WARNING: Will remove files and folders recursively below '%v'%s older than %v days. Continue? (y/n) ", filepath.FromSlash(folder), extMsg, age) -var response string -_, err := fmt.Scanln(&response) -if err != nil { - logger.Errorf("Failed to read user input: %v", err) - return -} -if response != "y" && response != "Y" { +if !getUserConfirmation("WARNING: Will remove files and folders recursively below '%v'%s older than %v days. Continue? (y/n) ", filepath.FromSlash(folder), extMsg, age) { logger.Warning("Operation not confirmed, exiting.") return } ``` |
Failed conditions
55.6% Coverage on New Code (required ≥ 60%)
User description
Fixes #196
Type
enhancement, bug_fix
Description
github.com/segmentio/go-prompt
dependency with custom user confirmation logic usingfmt.Scanln
to improve control and reduce external dependencies.flag.PrintDefaults()
toflag.Usage()
.github.com/segmentio/go-prompt
and its indirect dependencies fromgo.mod
andgo.sum
.Changes walkthrough
rmstale.go
Replace go-prompt with custom confirmation logic
rmstale.go
prompt.Confirm
with custom confirmation logic usingfmt.Scanln
.flag.PrintDefaults()
toflag.Usage()
for displaying commandusage.
go.mod
Remove go-prompt dependency from go.mod
go.mod - Removed `github.com/segmentio/go-prompt` dependency.
go.sum
Update go.sum after removing go-prompt
go.sum
go.sum
to reflect removal ofgithub.com/segmentio/go-prompt
and its dependencies.