Skip to content

Use slices and maps more where appropriate #2306

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

Merged
merged 4 commits into from
Apr 1, 2025

Conversation

kolyshkin
Copy link
Contributor

Please see individual commits for details.

Copy link
Member

@rhatdan rhatdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
LGTM

Copy link
Contributor

openshift-ci bot commented Mar 30, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: kolyshkin, rhatdan

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -84,63 +85,61 @@ func main() {
cmd := args[0]

for _, command := range commands {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: This works, and it is an improvement already, but processing commands in a loop is not quite what we mean.

Consider

i := slices.IndexFunc(commands, func(c) { slices.Contains(c.names, cmd) })
if i == -1 { /* fail */ }
command := commands[i]
// straight-line top-level code processing the command we found

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this looks weird.

Rewrote (I left for range as it seems more readable than slices.IndexFunc in this particular case).

...by using maps.Clone if possible.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Simplify the code that finds the command.

While at it, rename cmd to name to avoid using cmd and command together.

Best reviewed with --ignore-all-space.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Comment on lines +92 to +94
fmt.Printf("%s: unrecognized command.\n", name)
os.Exit(1)
return nil // To satisfy linters.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Absolutely non-blocking: Aesthetically, I’d prefer return nil here, and having os.Exit at the top level, but this works perfectly fine.)

@rhatdan
Copy link
Member

rhatdan commented Apr 1, 2025

/lgtm

@openshift-ci openshift-ci bot added the lgtm label Apr 1, 2025
@openshift-merge-bot openshift-merge-bot bot merged commit f1c4bdf into containers:main Apr 1, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants