Skip to content

Commit 0b14e7d

Browse files
authored
refactor: use slices.Contains to simplify code (#2908)
Signed-off-by: queryfast <queryfast@outlook.com>
1 parent d8e744a commit 0b14e7d

File tree

6 files changed

+13
-44
lines changed

6 files changed

+13
-44
lines changed

pkg/docker/compose.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"path/filepath"
12+
"slices"
1213
"strings"
1314
"text/template"
1415
"time"
@@ -283,12 +284,6 @@ func HasRemoteComposeService(host *models.Host, composeFile string, service stri
283284
if err != nil {
284285
return false, err
285286
}
286-
found := false
287-
for _, s := range services {
288-
if s == service {
289-
found = true
290-
break
291-
}
292-
}
287+
found := slices.Contains(services, service)
293288
return found, nil
294289
}

pkg/docker/image.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path/filepath"
10+
"slices"
1011
"strings"
1112

1213
"github.com/ava-labs/avalanche-cli/pkg/constants"
@@ -28,10 +29,8 @@ func DockerLocalImageExists(host *models.Host, image string) (bool, error) {
2829
if err != nil {
2930
return false, err
3031
}
31-
for _, localImage := range parseDockerImageListOutput(output) {
32-
if localImage == image {
33-
return true, nil
34-
}
32+
if slices.Contains(parseDockerImageListOutput(output), image) {
33+
return true, nil
3534
}
3635
return false, nil
3736
}

pkg/prompts/prompts.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,7 @@ func CheckSubnetAuthKeys(walletKeys []string, subnetAuthKeys []string, controlKe
125125
return fmt.Errorf("number of given auth keys differs from the threshold")
126126
}
127127
for _, subnetAuthKey := range subnetAuthKeys {
128-
found := false
129-
for _, controlKey := range controlKeys {
130-
if subnetAuthKey == controlKey {
131-
found = true
132-
break
133-
}
134-
}
128+
found := slices.Contains(controlKeys, subnetAuthKey)
135129
if !found {
136130
return fmt.Errorf("auth key %s does not belong to control keys", subnetAuthKey)
137131
}

pkg/prompts/prompts_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"path/filepath"
11+
"slices"
1112
"testing"
1213

1314
"github.com/ava-labs/avalanche-cli/pkg/key"
@@ -670,12 +671,7 @@ func TestPromptChain(t *testing.T) {
670671
mockPrompt := &mocks.Prompter{}
671672
mockPrompt.On("CaptureListWithSize", prompt, mock.MatchedBy(func(options []string) bool {
672673
// Should contain "Custom" option
673-
for _, opt := range options {
674-
if opt == Custom {
675-
return true
676-
}
677-
}
678-
return false
674+
return slices.Contains(options, Custom)
679675
}), 11).Return(Custom, nil).Once()
680676
mockPrompt.On("CaptureString", "Blockchain ID/Alias").Return("custom-blockchain-id", nil).Once()
681677
notListed, pChain, xChain, cChain, subnetName, blockchainID, err := PromptChain(
@@ -694,12 +690,7 @@ func TestPromptChain(t *testing.T) {
694690
mockPrompt := &mocks.Prompter{}
695691
mockPrompt.On("CaptureListWithSize", prompt, mock.MatchedBy(func(options []string) bool {
696692
// Should contain "My blockchain isn't listed" option
697-
for _, opt := range options {
698-
if opt == "My blockchain isn't listed" {
699-
return true
700-
}
701-
}
702-
return false
693+
return slices.Contains(options, "My blockchain isn't listed")
703694
}), 11).Return("My blockchain isn't listed", nil).Once()
704695
notListed, pChain, xChain, cChain, subnetName, blockchainID, err := PromptChain(
705696
mockPrompt, prompt, subnetNames, true, true, true, avoidBlockchainName, false)

pkg/utils/common.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os/exec"
1717
"os/user"
1818
"regexp"
19+
"slices"
1920
"strconv"
2021
"strings"
2122
"syscall"
@@ -38,7 +39,6 @@ import (
3839
"github.com/ava-labs/subnet-evm/core"
3940

4041
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
41-
"golang.org/x/exp/slices"
4242
"golang.org/x/mod/semver"
4343
)
4444

@@ -94,12 +94,7 @@ func GetRealFilePath(path string) string {
9494
}
9595

9696
func Any[T any](input []T, f func(T) bool) bool {
97-
for _, e := range input {
98-
if f(e) {
99-
return true
100-
}
101-
}
102-
return false
97+
return slices.ContainsFunc(input, f)
10398
}
10499

105100
func Find[T any](input []T, f func(T) bool) *T {

pkg/vm/precompiles_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package vm
55

66
import (
7+
"slices"
78
"testing"
89

910
"github.com/ava-labs/avalanche-cli/internal/testutils"
@@ -173,13 +174,7 @@ func TestAddAddressToAllowed(t *testing.T) {
173174
// Check if the address was added to enabled list when expected
174175
if tt.shouldAddToEnabled {
175176
expectedAddress := common.HexToAddress(tt.addressToAdd)
176-
found := false
177-
for _, addr := range result.EnabledAddresses {
178-
if addr == expectedAddress {
179-
found = true
180-
break
181-
}
182-
}
177+
found := slices.Contains(result.EnabledAddresses, expectedAddress)
183178
require.True(t, found, "Address should have been added to enabled list")
184179
}
185180

0 commit comments

Comments
 (0)