Skip to content

Commit 771583d

Browse files
committed
Implement suggestions on fqbn
1 parent fc367b7 commit 771583d

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

arduino/cores/packagemanager/package_manager.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
2727
paths "github.com/arduino/go-paths-helper"
2828
properties "github.com/arduino/go-properties-orderedmap"
29+
"github.com/schollz/closestmatch"
2930
"github.com/sirupsen/logrus"
3031
semver "go.bug.st/relaxed-semver"
3132
)
@@ -129,6 +130,22 @@ func (pm *PackageManager) FindBoardWithFQBN(fqbnIn string) (*cores.Board, error)
129130
return board, err
130131
}
131132

133+
func (pm *PackageManager) findClosestMatchFqbn(fqbn *cores.FQBN) string {
134+
// Create closestmatch DB
135+
wordsToTest := []string{}
136+
name := fqbn.StringWithoutConfig()
137+
for _, board := range pm.InstalledBoards() {
138+
wordsToTest = append(wordsToTest, board.FQBN())
139+
}
140+
// Choose a set of bag sizes, more is more accurate but slower
141+
bagSizes := []int{2}
142+
143+
// Create a closestmatch object and find the best matching name
144+
cm := closestmatch.New(wordsToTest, bagSizes)
145+
closestName := cm.Closest(name)
146+
return closestName
147+
}
148+
132149
// ResolveFQBN returns, in order:
133150
//
134151
// - the Package pointed by the fqbn
@@ -174,8 +191,10 @@ func (pm *PackageManager) ResolveFQBN(fqbn *cores.FQBN) (
174191
// Find board
175192
board := platformRelease.Boards[fqbn.BoardID]
176193
if board == nil {
194+
// Try looking for the closest match; if found, suggest it
195+
suggestion := pm.findClosestMatchFqbn(fqbn)
177196
return targetPackage, platformRelease, nil, nil, nil,
178-
fmt.Errorf("board %s:%s not found", platformRelease, fqbn.BoardID)
197+
fmt.Errorf("board %s not found, did you mean %s ?", fqbn.StringWithoutConfig(), suggestion)
179198
}
180199

181200
buildProperties, err := board.GetBuildProperties(fqbn.Configs)

0 commit comments

Comments
 (0)