Skip to content

Commit 88d89c5

Browse files
committed
add new error to handle multiple platform found, return res if the string the user is trying to operate matches perfectly one of the available platforms, optimize the code
1 parent 12f7fb9 commit 88d89c5

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

arduino/errors.go

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package arduino
1717

1818
import (
1919
"fmt"
20+
"strings"
2021

2122
"github.com/arduino/arduino-cli/arduino/discovery"
2223
"github.com/arduino/arduino-cli/i18n"
@@ -715,3 +716,24 @@ func (e *SignatureVerificationFailedError) Unwrap() error {
715716
func (e *SignatureVerificationFailedError) ToRPCStatus() *status.Status {
716717
return status.New(codes.Unavailable, e.Error())
717718
}
719+
720+
// MultiplePlatformsError is returned when trying to detect
721+
// the Platform the user is trying to interact with and
722+
// and multiple results are found.
723+
type MultiplePlatformsError struct {
724+
Platforms []string
725+
UserPlatform string
726+
}
727+
728+
func (e *MultiplePlatformsError) Error() string {
729+
return tr("Found %d platform for reference %s: %s",
730+
len(e.Platforms),
731+
e.UserPlatform,
732+
strings.Join(e.Platforms, "\n"),
733+
)
734+
}
735+
736+
// ToRPCStatus converts the error into a *status.Status
737+
func (e *MultiplePlatformsError) ToRPCStatus() *status.Status {
738+
return status.New(codes.InvalidArgument, e.Error())
739+
}

cli/arguments/reference.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"strings"
2121

22+
"github.com/arduino/arduino-cli/arduino"
2223
"github.com/arduino/arduino-cli/cli/instance"
2324
"github.com/arduino/arduino-cli/commands/core"
2425
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -94,20 +95,28 @@ func ParseReference(arg string) (*Reference, error) {
9495
UpdatableOnly: false,
9596
All: true, // this is true because we want also the installable platforms
9697
})
97-
var found = 0
98+
foundPlatforms := []string{}
9899
for _, platform := range platforms {
99-
if strings.EqualFold(platform.GetId(), ret.PackageName+":"+ret.Architecture) {
100-
logrus.Infof("Found possible match for reference %s -> %s", arg, platform.GetId())
101-
toks = strings.Split(platform.GetId(), ":")
102-
found++
100+
platformID := platform.GetId()
101+
platformUser := ret.PackageName + ":" + ret.Architecture
102+
// At first we check if the platform the user is searching for matches an available one,
103+
// this way we do not need to adapt the casing and we can return it directly
104+
if platformUser == platformID {
105+
return ret, nil
106+
}
107+
if strings.EqualFold(platformUser, platformID) {
108+
logrus.Infof("Found possible match for reference %s -> %s", platformUser, platformID)
109+
toks = strings.Split(platformID, ":")
110+
foundPlatforms = append(foundPlatforms, platformID)
103111
}
104112
}
105-
// replace the returned Reference only if only one occurrence is found, otherwise leave it as is
106-
if found == 1 {
113+
// replace the returned Reference only if only one occurrence is found,
114+
// otherwise return an error to the user because we don't know on which platform operate
115+
if len(foundPlatforms) == 1 {
107116
ret.PackageName = toks[0]
108117
ret.Architecture = toks[1]
109118
} else {
110-
logrus.Warnf("Found %d platform for reference: %s", found, arg)
119+
return nil, &arduino.MultiplePlatformsError{Platforms: foundPlatforms, UserPlatform: arg}
111120
}
112121
return ret, nil
113122
}

0 commit comments

Comments
 (0)