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

add duplicate command warning #3174

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions core/commandline/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
Copy link
Contributor

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's map[string]struct{} for a simple set implementation.

Here's an alternative implementation using the standard library:

nameSet := make(map[string]struct{})

// To add an item to the set:
nameSet[itemName] = struct{}{}

// To check if an item exists in the set:
_, exists := nameSet[itemName]

This approach achieves the same functionality without introducing an external dependency.

"os"
"os/signal"
"os/user"
Expand Down Expand Up @@ -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()

Check warning on line 94 in core/commandline/shell.go

View check run for this annotation

Codecov / codecov/patch

core/commandline/shell.go#L94

Added line #L94 was not covered by tests
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)
}

Check warning on line 101 in core/commandline/shell.go

View check run for this annotation

Codecov / codecov/patch

core/commandline/shell.go#L99-L101

Added lines #L99 - L101 were not covered by tests

nameSet.Insert(command.Name)

Check warning on line 103 in core/commandline/shell.go

View check run for this annotation

Codecov / codecov/patch

core/commandline/shell.go#L103

Added line #L103 was not covered by tests
}

Comment on lines +94 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  1. Only non-shell commands are considered.
  2. Duplicate commands are skipped and a warning is printed.
  3. Non-duplicate commands are added to prunedCommands and their names are inserted into the nameSet.
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.

Suggested change
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)
}
nameSet := sets.NewString()
for _, command := range commands {
if command.Name != shellCommandName {
if nameSet.Has(command.Name) {
fmt.Printf("Command %s already exists, skipping\n", command.Name)
} else {
prunedCommands = append(prunedCommands, command)
nameSet.Insert(command.Name)
}
}
}

return prunedCommands
}

Expand Down
Loading