Skip to content

Commit

Permalink
Add completion support to --profile
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Nov 14, 2023
1 parent 6c86fa2 commit 0d8ca10
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/cli/arguments/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ type Profile struct {
// AddToCommand adds the flags used to set fqbn to the specified Command
func (f *Profile) AddToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&f.profile, "profile", "m", "", tr("Sketch profile to use"))
// TODO: register autocompletion
cmd.RegisterFlagCompletionFunc("profile", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var sketchProfile string
if len(args) > 0 {
sketchProfile = args[0]
}
return GetSketchProfiles(sketchProfile), cobra.ShellCompDirectiveDefault
})
}

// Get returns the profile name
Expand Down
25 changes: 25 additions & 0 deletions internal/cli/arguments/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
package arguments

import (
"context"

"github.com/arduino/arduino-cli/commands/sketch"
sk "github.com/arduino/arduino-cli/commands/sketch"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
)
Expand All @@ -43,3 +47,24 @@ func InitSketchPath(path string, printWarnings bool) (sketchPath *paths.Path) {
}
return sketchPath
}

// GetSketchProfiles is an helper function useful to autocomplete.
// It returns the profile names set in the sketch.yaml
func GetSketchProfiles(sketchPath string) []string {
if sketchPath == "" {
if wd, _ := paths.Getwd(); wd != nil && wd.String() != "" {
sketchPath = wd.String()
} else {
return nil
}
}
list, _ := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{
SketchPath: sketchPath,
})
profiles := list.GetProfiles()
res := make([]string, len(profiles))
for i, p := range list.GetProfiles() {
res[i] = p.GetName()
}
return res
}
28 changes: 28 additions & 0 deletions internal/integrationtest/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -229,3 +230,30 @@ func TestCoreCompletion(t *testing.T) {
stdout, _, _ = cli.Run("__complete", "upload", "-P", "")
require.Contains(t, string(stdout), "atmel_ice")
}

func TestProfileCompletion(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Create test sketches
sketchWithProfilesPath, err := paths.New("testdata", "SketchWithProfiles").Abs()
require.NoError(t, err)
require.True(t, sketchWithProfilesPath.IsDir())

stdout, _, _ := cli.Run("__complete", "compile", sketchWithProfilesPath.String(), "--profile", "")
require.Contains(t, string(stdout), "profile1")
stdout, _, _ = cli.Run("__complete", "monitor", sketchWithProfilesPath.String(), "--profile", "")
require.Contains(t, string(stdout), "profile1")
stdout, _, _ = cli.Run("__complete", "upload", sketchWithProfilesPath.String(), "--profile", "")
require.Contains(t, string(stdout), "profile1")

// The cli is running in the sketch folder, so need the explictly specify the path in the cli
cli.SetWorkingDir(sketchWithProfilesPath)
stdout, _, _ = cli.Run("__complete", "compile", "--profile", "")
require.Contains(t, string(stdout), "profile1")
stdout, _, _ = cli.Run("__complete", "monitor", "--profile", "")
require.Contains(t, string(stdout), "profile1")
stdout, _, _ = cli.Run("__complete", "upload", "--profile", "")
require.Contains(t, string(stdout), "profile1")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

void setup() {}
void loop() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default_port: /dev/ttyDEF
default_fqbn: arduino:avr:yun
profiles:
profile1:
fqbn: "broken_fqbn"
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ default_port: /dev/ttyDEF
default_fqbn: arduino:avr:yun
profiles:
profile1:
fqbn: "broken_fqbn"
fqbn: "arduino:avr:uno"
profile2:
fqbn: "arduino:avr:yun"
profile3:
fqbn: "arduino:avr:unowifi"

0 comments on commit 0d8ca10

Please sign in to comment.