Skip to content

Commit 8a769b5

Browse files
committed
refactor: use slices.Contains to simplify code
Signed-off-by: queryfast <queryfast@outlook.com>
1 parent 96fda1b commit 8a769b5

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"
@@ -669,12 +670,7 @@ func TestPromptChain(t *testing.T) {
669670
mockPrompt := &mocks.Prompter{}
670671
mockPrompt.On("CaptureListWithSize", prompt, mock.MatchedBy(func(options []string) bool {
671672
// Should contain "Custom" option
672-
for _, opt := range options {
673-
if opt == Custom {
674-
return true
675-
}
676-
}
677-
return false
673+
return slices.Contains(options, Custom)
678674
}), 11).Return(Custom, nil).Once()
679675
mockPrompt.On("CaptureString", "Blockchain ID/Alias").Return("custom-blockchain-id", nil).Once()
680676
notListed, pChain, xChain, cChain, subnetName, blockchainID, err := PromptChain(
@@ -693,12 +689,7 @@ func TestPromptChain(t *testing.T) {
693689
mockPrompt := &mocks.Prompter{}
694690
mockPrompt.On("CaptureListWithSize", prompt, mock.MatchedBy(func(options []string) bool {
695691
// Should contain "My blockchain isn't listed" option
696-
for _, opt := range options {
697-
if opt == "My blockchain isn't listed" {
698-
return true
699-
}
700-
}
701-
return false
692+
return slices.Contains(options, "My blockchain isn't listed")
702693
}), 11).Return("My blockchain isn't listed", nil).Once()
703694
notListed, pChain, xChain, cChain, subnetName, blockchainID, err := PromptChain(
704695
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"
@@ -32,7 +33,6 @@ import (
3233
"github.com/ava-labs/subnet-evm/core"
3334

3435
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
35-
"golang.org/x/exp/slices"
3636
"golang.org/x/mod/semver"
3737
)
3838

@@ -88,12 +88,7 @@ func GetRealFilePath(path string) string {
8888
}
8989

9090
func Any[T any](input []T, f func(T) bool) bool {
91-
for _, e := range input {
92-
if f(e) {
93-
return true
94-
}
95-
}
96-
return false
91+
return slices.ContainsFunc(input, f)
9792
}
9893

9994
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)