Skip to content

Commit 0d8ca10

Browse files
Add completion support to --profile
1 parent 6c86fa2 commit 0d8ca10

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

Diff for: internal/cli/arguments/profiles.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ type Profile struct {
2727
// AddToCommand adds the flags used to set fqbn to the specified Command
2828
func (f *Profile) AddToCommand(cmd *cobra.Command) {
2929
cmd.Flags().StringVarP(&f.profile, "profile", "m", "", tr("Sketch profile to use"))
30-
// TODO: register autocompletion
30+
cmd.RegisterFlagCompletionFunc("profile", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
31+
var sketchProfile string
32+
if len(args) > 0 {
33+
sketchProfile = args[0]
34+
}
35+
return GetSketchProfiles(sketchProfile), cobra.ShellCompDirectiveDefault
36+
})
3137
}
3238

3339
// Get returns the profile name

Diff for: internal/cli/arguments/sketch.go

+25
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
package arguments
1717

1818
import (
19+
"context"
20+
21+
"github.com/arduino/arduino-cli/commands/sketch"
1922
sk "github.com/arduino/arduino-cli/commands/sketch"
2023
"github.com/arduino/arduino-cli/internal/cli/feedback"
24+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2125
"github.com/arduino/go-paths-helper"
2226
"github.com/sirupsen/logrus"
2327
)
@@ -43,3 +47,24 @@ func InitSketchPath(path string, printWarnings bool) (sketchPath *paths.Path) {
4347
}
4448
return sketchPath
4549
}
50+
51+
// GetSketchProfiles is an helper function useful to autocomplete.
52+
// It returns the profile names set in the sketch.yaml
53+
func GetSketchProfiles(sketchPath string) []string {
54+
if sketchPath == "" {
55+
if wd, _ := paths.Getwd(); wd != nil && wd.String() != "" {
56+
sketchPath = wd.String()
57+
} else {
58+
return nil
59+
}
60+
}
61+
list, _ := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{
62+
SketchPath: sketchPath,
63+
})
64+
profiles := list.GetProfiles()
65+
res := make([]string, len(profiles))
66+
for i, p := range list.GetProfiles() {
67+
res[i] = p.GetName()
68+
}
69+
return res
70+
}

Diff for: internal/integrationtest/completion/completion_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
"github.com/arduino/arduino-cli/internal/integrationtest"
22+
"github.com/arduino/go-paths-helper"
2223
"github.com/stretchr/testify/require"
2324
)
2425

@@ -229,3 +230,30 @@ func TestCoreCompletion(t *testing.T) {
229230
stdout, _, _ = cli.Run("__complete", "upload", "-P", "")
230231
require.Contains(t, string(stdout), "atmel_ice")
231232
}
233+
234+
func TestProfileCompletion(t *testing.T) {
235+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
236+
defer env.CleanUp()
237+
238+
// Create test sketches
239+
sketchWithProfilesPath, err := paths.New("testdata", "SketchWithProfiles").Abs()
240+
require.NoError(t, err)
241+
require.True(t, sketchWithProfilesPath.IsDir())
242+
243+
stdout, _, _ := cli.Run("__complete", "compile", sketchWithProfilesPath.String(), "--profile", "")
244+
require.Contains(t, string(stdout), "profile1")
245+
stdout, _, _ = cli.Run("__complete", "monitor", sketchWithProfilesPath.String(), "--profile", "")
246+
require.Contains(t, string(stdout), "profile1")
247+
stdout, _, _ = cli.Run("__complete", "upload", sketchWithProfilesPath.String(), "--profile", "")
248+
require.Contains(t, string(stdout), "profile1")
249+
250+
// The cli is running in the sketch folder, so need the explictly specify the path in the cli
251+
cli.SetWorkingDir(sketchWithProfilesPath)
252+
stdout, _, _ = cli.Run("__complete", "compile", "--profile", "")
253+
require.Contains(t, string(stdout), "profile1")
254+
stdout, _, _ = cli.Run("__complete", "monitor", "--profile", "")
255+
require.Contains(t, string(stdout), "profile1")
256+
stdout, _, _ = cli.Run("__complete", "upload", "--profile", "")
257+
require.Contains(t, string(stdout), "profile1")
258+
259+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
void setup() {}
3+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
default_port: /dev/ttyDEF
2+
default_fqbn: arduino:avr:yun
3+
profiles:
4+
profile1:
5+
fqbn: "broken_fqbn"

Diff for: internal/integrationtest/monitor/testdata/SketchWithDefaultPortAndFQBN/sketch.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ default_port: /dev/ttyDEF
22
default_fqbn: arduino:avr:yun
33
profiles:
44
profile1:
5-
fqbn: "broken_fqbn"
5+
fqbn: "arduino:avr:uno"
6+
profile2:
7+
fqbn: "arduino:avr:yun"
8+
profile3:
9+
fqbn: "arduino:avr:unowifi"

0 commit comments

Comments
 (0)