-
Notifications
You must be signed in to change notification settings - Fork 33
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
add duplicate command warning #3174
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 | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||||||||||||||||||
"context" | ||||||||||||||||||||||||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||||||||||||||||||||||||
"k8s.io/apimachinery/pkg/util/sets" | ||||||||||||||||||||||||||||||||||||||||||||||
"os" | ||||||||||||||||||||||||||||||||||||||||||||||
"os/signal" | ||||||||||||||||||||||||||||||||||||||||||||||
"os/user" | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -90,11 +91,18 @@ | |||||||||||||||||||||||||||||||||||||||||||||
// pruneShellCommands gets a list of commands including the shell command. | ||||||||||||||||||||||||||||||||||||||||||||||
func pruneShellCommands(commands []*cli.Command) (prunedCommands []*cli.Command) { | ||||||||||||||||||||||||||||||||||||||||||||||
// initialize shell commands | ||||||||||||||||||||||||||||||||||||||||||||||
nameSet := sets.NewString() | ||||||||||||||||||||||||||||||||||||||||||||||
for _, command := range commands { | ||||||||||||||||||||||||||||||||||||||||||||||
if command.Name != shellCommandName { | ||||||||||||||||||||||||||||||||||||||||||||||
prunedCommands = append(prunedCommands, command) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
if !nameSet.Has(command.Name) { | ||||||||||||||||||||||||||||||||||||||||||||||
fmt.Printf("Command %s already exists, skipping\n", command.Name) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
nameSet.Insert(command.Name) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+94
to
+105
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. Fix logical error in duplicate command handling The current implementation has a logical error that results in skipping all commands except duplicates, which is the opposite of the intended behavior. Additionally, the warning message is printed for non-duplicate commands instead of duplicate ones. To fix this, please apply the following changes: nameSet := sets.NewString()
for _, command := range commands {
if command.Name != shellCommandName {
- prunedCommands = append(prunedCommands, command)
- }
- if !nameSet.Has(command.Name) {
- fmt.Printf("Command %s already exists, skipping\n", command.Name)
- }
-
- nameSet.Insert(command.Name)
+ if nameSet.Has(command.Name) {
+ fmt.Printf("Command %s already exists, skipping\n", command.Name)
+ } else {
+ prunedCommands = append(prunedCommands, command)
+ nameSet.Insert(command.Name)
+ }
+ }
} This change ensures that:
Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
return prunedCommands | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
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.
Consider using the standard library instead of Kubernetes package for set implementation.
While the
k8s.io/apimachinery/pkg/util/sets
package provides a convenient set implementation, it might be overkill for this use case, especially if this is not a Kubernetes-related project. Consider using the standard library'smap[string]struct{}
for a simple set implementation.Here's an alternative implementation using the standard library:
This approach achieves the same functionality without introducing an external dependency.