Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completion support to --profile #2426

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: "arduino:avr:uno"
Loading