From b76847fc713183ae19479e5ea93935fd961b8c10 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 18 Mar 2024 13:37:09 +0100 Subject: [PATCH] Inlining methods in ArduinoCoreServiceImpl (part 13: NewSketch, LoadSketch, SetSketchDefaults, ArchiveSketch) --- commands/service.go | 21 --------------------- commands/service_set_sketch_defaults.go | 2 +- commands/service_sketch_archive.go | 8 ++++---- commands/service_sketch_load.go | 4 ++-- commands/service_sketch_load_test.go | 7 ++++--- commands/service_sketch_new.go | 2 +- commands/service_sketch_new_test.go | 21 +++++++++++++-------- internal/cli/arguments/profiles.go | 11 ++++++++--- internal/cli/arguments/sketch.go | 7 +++---- internal/cli/board/attach.go | 4 ++-- internal/cli/cli.go | 2 +- internal/cli/compile/compile.go | 5 +++-- internal/cli/debug/debug.go | 5 +++-- internal/cli/monitor/monitor.go | 5 +++-- internal/cli/sketch/archive.go | 16 +++++++++------- internal/cli/sketch/new.go | 12 +++++++----- internal/cli/sketch/sketch.go | 7 ++++--- internal/cli/upload/upload.go | 5 +++-- 18 files changed, 71 insertions(+), 73 deletions(-) diff --git a/commands/service.go b/commands/service.go index 9faa1c38138..21baf0b12fc 100644 --- a/commands/service.go +++ b/commands/service.go @@ -40,22 +40,6 @@ func (s *arduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionReq return &rpc.VersionResponse{Version: s.versionString}, nil } -// NewSketch FIXMEDOC -func (s *arduinoCoreServerImpl) NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) { - return NewSketch(ctx, req) -} - -// LoadSketch FIXMEDOC -func (s *arduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) { - resp, err := LoadSketch(ctx, req) - return &rpc.LoadSketchResponse{Sketch: resp}, err -} - -// SetSketchDefaults FIXMEDOC -func (s *arduinoCoreServerImpl) SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (*rpc.SetSketchDefaultsResponse, error) { - return SetSketchDefaults(ctx, req) -} - // Upload FIXMEDOC func (s *arduinoCoreServerImpl) Upload(req *rpc.UploadRequest, stream rpc.ArduinoCoreService_UploadServer) error { syncSend := NewSynchronizedSend(stream.Send) @@ -144,11 +128,6 @@ func (s *arduinoCoreServerImpl) ListProgrammersAvailableForUpload(ctx context.Co return ListProgrammersAvailableForUpload(ctx, req) } -// ArchiveSketch FIXMEDOC -func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) { - return ArchiveSketch(ctx, req) -} - // EnumerateMonitorPortSettings FIXMEDOC func (s *arduinoCoreServerImpl) EnumerateMonitorPortSettings(ctx context.Context, req *rpc.EnumerateMonitorPortSettingsRequest) (*rpc.EnumerateMonitorPortSettingsResponse, error) { return EnumerateMonitorPortSettings(ctx, req) diff --git a/commands/service_set_sketch_defaults.go b/commands/service_set_sketch_defaults.go index e90b1ef58c4..6b3ba044899 100644 --- a/commands/service_set_sketch_defaults.go +++ b/commands/service_set_sketch_defaults.go @@ -26,7 +26,7 @@ import ( // SetSketchDefaults updates the sketch project file (sketch.yaml) with the given defaults // for the values `default_fqbn`, `default_port`, and `default_protocol`. -func SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (*rpc.SetSketchDefaultsResponse, error) { +func (s *arduinoCoreServerImpl) SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (*rpc.SetSketchDefaultsResponse, error) { sk, err := sketch.New(paths.New(req.GetSketchPath())) if err != nil { return nil, &cmderrors.CantOpenSketchError{Cause: err} diff --git a/commands/service_sketch_archive.go b/commands/service_sketch_archive.go index b2b28ae4bdd..67515406382 100644 --- a/commands/service_sketch_archive.go +++ b/commands/service_sketch_archive.go @@ -29,7 +29,7 @@ import ( ) // ArchiveSketch FIXMEDOC -func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) { +func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) { // sketchName is the name of the sketch without extension, for example "MySketch" var sketchName string @@ -38,13 +38,13 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc sketchPath = paths.New(".") } - s, err := sketch.New(sketchPath) + sk, err := sketch.New(sketchPath) if err != nil { return nil, &cmderrors.CantOpenSketchError{Cause: err} } - sketchPath = s.FullPath - sketchName = s.Name + sketchPath = sk.FullPath + sketchName = sk.Name archivePath := paths.New(req.GetArchivePath()) if archivePath == nil { diff --git a/commands/service_sketch_load.go b/commands/service_sketch_load.go index ffca3016715..8360ce7adfd 100644 --- a/commands/service_sketch_load.go +++ b/commands/service_sketch_load.go @@ -25,10 +25,10 @@ import ( ) // LoadSketch collects and returns all information about a sketch -func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.Sketch, error) { +func (s *arduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) { sk, err := sketch.New(paths.New(req.GetSketchPath())) if err != nil { return nil, &cmderrors.CantOpenSketchError{Cause: err} } - return sk.ToRpc(), nil + return &rpc.LoadSketchResponse{Sketch: sk.ToRpc()}, nil } diff --git a/commands/service_sketch_load_test.go b/commands/service_sketch_load_test.go index 9d058e7035f..36bb58ea351 100644 --- a/commands/service_sketch_load_test.go +++ b/commands/service_sketch_load_test.go @@ -24,10 +24,11 @@ import ( ) func TestLoadSketchProfiles(t *testing.T) { - loadResp, err := LoadSketch(context.Background(), &commands.LoadSketchRequest{ + srv := NewArduinoCoreServer("") + loadResp, err := srv.LoadSketch(context.Background(), &commands.LoadSketchRequest{ SketchPath: "./testdata/sketch_with_profile", }) require.NoError(t, err) - require.Len(t, loadResp.GetProfiles(), 2) - require.Equal(t, loadResp.GetDefaultProfile().GetName(), "nanorp") + require.Len(t, loadResp.GetSketch().GetProfiles(), 2) + require.Equal(t, loadResp.GetSketch().GetDefaultProfile().GetName(), "nanorp") } diff --git a/commands/service_sketch_new.go b/commands/service_sketch_new.go index 1a39615c9eb..a8eb518a815 100644 --- a/commands/service_sketch_new.go +++ b/commands/service_sketch_new.go @@ -43,7 +43,7 @@ var invalidNames = []string{"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"} // NewSketch creates a new sketch via gRPC -func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) { +func (s *arduinoCoreServerImpl) NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) { var sketchesDir string if len(req.GetSketchDir()) > 0 { sketchesDir = req.GetSketchDir() diff --git a/commands/service_sketch_new_test.go b/commands/service_sketch_new_test.go index 0ba8b56767c..0d0b3e05aa6 100644 --- a/commands/service_sketch_new_test.go +++ b/commands/service_sketch_new_test.go @@ -20,7 +20,7 @@ import ( "fmt" "testing" - "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/stretchr/testify/require" ) @@ -34,8 +34,10 @@ func Test_SketchNameWrongPattern(t *testing.T) { "||||||||||||||", ",`hack[}attempt{];", } + + srv := NewArduinoCoreServer("") for _, name := range invalidNames { - _, err := NewSketch(context.Background(), &commands.NewSketchRequest{ + _, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{ SketchName: name, SketchDir: t.TempDir(), }) @@ -46,9 +48,9 @@ func Test_SketchNameWrongPattern(t *testing.T) { } func Test_SketchNameEmpty(t *testing.T) { - emptyName := "" - _, err := NewSketch(context.Background(), &commands.NewSketchRequest{ - SketchName: emptyName, + srv := NewArduinoCoreServer("") + _, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{ + SketchName: "", SketchDir: t.TempDir(), }) @@ -60,7 +62,8 @@ func Test_SketchNameTooLong(t *testing.T) { for i := range tooLongName { tooLongName[i] = 'a' } - _, err := NewSketch(context.Background(), &commands.NewSketchRequest{ + srv := NewArduinoCoreServer("") + _, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{ SketchName: string(tooLongName), SketchDir: t.TempDir(), }) @@ -83,8 +86,9 @@ func Test_SketchNameOk(t *testing.T) { "_hello_world", string(lengthLimitName), } + srv := NewArduinoCoreServer("") for _, name := range validNames { - _, err := NewSketch(context.Background(), &commands.NewSketchRequest{ + _, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{ SketchName: name, SketchDir: t.TempDir(), }) @@ -95,8 +99,9 @@ func Test_SketchNameOk(t *testing.T) { func Test_SketchNameReserved(t *testing.T) { invalidNames := []string{"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"} + srv := NewArduinoCoreServer("") for _, name := range invalidNames { - _, err := NewSketch(context.Background(), &commands.NewSketchRequest{ + _, err := srv.NewSketch(context.Background(), &rpc.NewSketchRequest{ SketchName: name, SketchDir: t.TempDir(), }) diff --git a/internal/cli/arguments/profiles.go b/internal/cli/arguments/profiles.go index 6516cbdec08..1631fe35d9f 100644 --- a/internal/cli/arguments/profiles.go +++ b/internal/cli/arguments/profiles.go @@ -15,7 +15,12 @@ package arguments -import "github.com/spf13/cobra" +import ( + "context" + + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" + "github.com/spf13/cobra" +) // Profile contains the profile flag data. // This is useful so all flags used by commands that need @@ -25,14 +30,14 @@ type Profile struct { } // AddToCommand adds the flags used to set fqbn to the specified Command -func (f *Profile) AddToCommand(cmd *cobra.Command) { +func (f *Profile) AddToCommand(cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) { cmd.Flags().StringVarP(&f.profile, "profile", "m", "", tr("Sketch profile to use")) 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 + return GetSketchProfiles(context.Background(), srv, sketchProfile), cobra.ShellCompDirectiveDefault }) } diff --git a/internal/cli/arguments/sketch.go b/internal/cli/arguments/sketch.go index 57a3463bcc3..e946c4fd6a2 100644 --- a/internal/cli/arguments/sketch.go +++ b/internal/cli/arguments/sketch.go @@ -18,7 +18,6 @@ package arguments import ( "context" - "github.com/arduino/arduino-cli/commands" f "github.com/arduino/arduino-cli/internal/algorithms" "github.com/arduino/arduino-cli/internal/cli/feedback" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -44,7 +43,7 @@ func InitSketchPath(path string) (sketchPath *paths.Path) { // GetSketchProfiles is an helper function useful to autocomplete. // It returns the profile names set in the sketch.yaml -func GetSketchProfiles(sketchPath string) []string { +func GetSketchProfiles(ctx context.Context, srv rpc.ArduinoCoreServiceServer, sketchPath string) []string { if sketchPath == "" { if wd, _ := paths.Getwd(); wd != nil && wd.String() != "" { sketchPath = wd.String() @@ -52,10 +51,10 @@ func GetSketchProfiles(sketchPath string) []string { return nil } } - sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath}) + resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath}) if err != nil { return nil } - profiles := sk.GetProfiles() + profiles := resp.GetSketch().GetProfiles() return f.Map(profiles, (*rpc.SketchProfile).GetName) } diff --git a/internal/cli/board/attach.go b/internal/cli/board/attach.go index 627f014760c..9c5a8dc268e 100644 --- a/internal/cli/board/attach.go +++ b/internal/cli/board/attach.go @@ -20,7 +20,6 @@ import ( "fmt" "os" - "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/internal/cli/arguments" "github.com/arduino/arduino-cli/internal/cli/feedback" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -56,10 +55,11 @@ func initAttachCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { } func runAttachCommand(srv rpc.ArduinoCoreServiceServer, path string, port *arguments.Port, fqbn string, programmer *arguments.Programmer) { + ctx := context.Background() sketchPath := arguments.InitSketchPath(path) portAddress, portProtocol, _ := port.GetPortAddressAndProtocol(nil, srv, "", "") - newDefaults, err := commands.SetSketchDefaults(context.Background(), &rpc.SetSketchDefaultsRequest{ + newDefaults, err := srv.SetSketchDefaults(ctx, &rpc.SetSketchDefaultsRequest{ SketchPath: sketchPath.String(), DefaultFqbn: fqbn, DefaultProgrammer: programmer.GetProgrammer(), diff --git a/internal/cli/cli.go b/internal/cli/cli.go index f096d079430..8779a87ded0 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -88,7 +88,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { cmd.AddCommand(lib.NewCommand(srv)) cmd.AddCommand(monitor.NewCommand(srv)) cmd.AddCommand(outdated.NewCommand(srv)) - cmd.AddCommand(sketch.NewCommand()) + cmd.AddCommand(sketch.NewCommand(srv)) cmd.AddCommand(update.NewCommand(srv)) cmd.AddCommand(upgrade.NewCommand(srv)) cmd.AddCommand(upload.NewCommand(srv)) diff --git a/internal/cli/compile/compile.go b/internal/cli/compile/compile.go index 80fdc27164d..6a2ea645bf0 100644 --- a/internal/cli/compile/compile.go +++ b/internal/cli/compile/compile.go @@ -93,7 +93,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { } fqbnArg.AddToCommand(compileCommand, srv) - profileArg.AddToCommand(compileCommand) + profileArg.AddToCommand(compileCommand, srv) compileCommand.Flags().BoolVar(&dumpProfile, "dump-profile", false, tr("Create and print a profile configuration from the build.")) showPropertiesArg.AddToCommand(compileCommand) compileCommand.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling.")) @@ -161,10 +161,11 @@ func runCompileCommand(cmd *cobra.Command, args []string, srv rpc.ArduinoCoreSer } sketchPath := arguments.InitSketchPath(path) - sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) + resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) if err != nil { feedback.FatalError(err, feedback.ErrGeneric) } + sk := resp.GetSketch() feedback.WarnAboutDeprecatedFiles(sk) var inst *rpc.Instance diff --git a/internal/cli/debug/debug.go b/internal/cli/debug/debug.go index 90d03cd459c..cb0f37fce58 100644 --- a/internal/cli/debug/debug.go +++ b/internal/cli/debug/debug.go @@ -64,7 +64,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { fqbnArg.AddToCommand(debugCommand, srv) portArgs.AddToCommand(debugCommand, srv) programmer.AddToCommand(debugCommand, srv) - profileArg.AddToCommand(debugCommand) + profileArg.AddToCommand(debugCommand, srv) debugCommand.Flags().StringVar(&interpreter, "interpreter", "console", tr("Debug interpreter e.g.: %s", "console, mi, mi1, mi2, mi3")) debugCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries for debug.")) debugCommand.Flags().BoolVarP(&printInfo, "info", "I", false, tr("Show metadata about the debug session instead of starting the debugger.")) @@ -83,10 +83,11 @@ func runDebugCommand(srv rpc.ArduinoCoreServiceServer, args []string, portArgs * } sketchPath := arguments.InitSketchPath(path) - sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) + resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) if err != nil { feedback.FatalError(err, feedback.ErrGeneric) } + sk := resp.GetSketch() feedback.WarnAboutDeprecatedFiles(sk) var inst *rpc.Instance diff --git a/internal/cli/monitor/monitor.go b/internal/cli/monitor/monitor.go index 94c495068c5..975f68ba1dd 100644 --- a/internal/cli/monitor/monitor.go +++ b/internal/cli/monitor/monitor.go @@ -70,7 +70,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { }, } portArgs.AddToCommand(monitorCommand, srv) - profileArg.AddToCommand(monitorCommand) + profileArg.AddToCommand(monitorCommand, srv) monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered).")) monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port.")) monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is =[,=]...")) @@ -105,13 +105,14 @@ func runMonitorCmd( // If only --port is set we read the fqbn in the following order: default_fqbn -> discovery // If only --fqbn is set we read the port in the following order: default_port sketchPath := arguments.InitSketchPath(sketchPathArg) - sketch, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) + resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) if err != nil && !portArgs.IsPortFlagSet() { feedback.Fatal( tr("Error getting default port from `sketch.yaml`. Check if you're in the correct sketch folder or provide the --port flag: %s", err), feedback.ErrGeneric, ) } + sketch := resp.GetSketch() if sketch != nil { defaultPort, defaultProtocol = sketch.GetDefaultPort(), sketch.GetDefaultProtocol() } diff --git a/internal/cli/sketch/archive.go b/internal/cli/sketch/archive.go index 9b8836daba3..2de1cd0ddaf 100644 --- a/internal/cli/sketch/archive.go +++ b/internal/cli/sketch/archive.go @@ -20,7 +20,6 @@ import ( "fmt" "os" - "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/internal/cli/arguments" "github.com/arduino/arduino-cli/internal/cli/feedback" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -29,7 +28,7 @@ import ( ) // initArchiveCommand creates a new `archive` command -func initArchiveCommand() *cobra.Command { +func initArchiveCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { var includeBuildDir, overwrite bool archiveCommand := &cobra.Command{ @@ -43,7 +42,9 @@ func initArchiveCommand() *cobra.Command { " " + os.Args[0] + " archive /home/user/Arduino/MySketch\n" + " " + os.Args[0] + " archive /home/user/Arduino/MySketch /home/user/MySketchArchive.zip", Args: cobra.MaximumNArgs(2), - Run: func(cmd *cobra.Command, args []string) { runArchiveCommand(args, includeBuildDir, overwrite) }, + Run: func(cmd *cobra.Command, args []string) { + runArchiveCommand(srv, args, includeBuildDir, overwrite) + }, } archiveCommand.Flags().BoolVar(&includeBuildDir, "include-build-dir", false, tr("Includes %s directory in the archive.", "build")) @@ -52,9 +53,9 @@ func initArchiveCommand() *cobra.Command { return archiveCommand } -func runArchiveCommand(args []string, includeBuildDir bool, overwrite bool) { +func runArchiveCommand(srv rpc.ArduinoCoreServiceServer, args []string, includeBuildDir bool, overwrite bool) { logrus.Info("Executing `arduino-cli sketch archive`") - + ctx := context.Background() sketchPathArg := "" if len(args) > 0 { sketchPathArg = args[0] @@ -66,13 +67,14 @@ func runArchiveCommand(args []string, includeBuildDir bool, overwrite bool) { } sketchPath := arguments.InitSketchPath(sketchPathArg) - sk, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) + resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) if err != nil { feedback.FatalError(err, feedback.ErrGeneric) } + sk := resp.GetSketch() feedback.WarnAboutDeprecatedFiles(sk) - if _, err := commands.ArchiveSketch(context.Background(), + if _, err := srv.ArchiveSketch(ctx, &rpc.ArchiveSketchRequest{ SketchPath: sketchPath.String(), ArchivePath: archivePathArg, diff --git a/internal/cli/sketch/new.go b/internal/cli/sketch/new.go index 1bfdf36c764..5be99e4308b 100644 --- a/internal/cli/sketch/new.go +++ b/internal/cli/sketch/new.go @@ -20,7 +20,6 @@ import ( "os" "strings" - "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/internal/arduino/globals" "github.com/arduino/arduino-cli/internal/cli/feedback" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -29,7 +28,7 @@ import ( "github.com/spf13/cobra" ) -func initNewCommand() *cobra.Command { +func initNewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { var overwrite bool newCommand := &cobra.Command{ @@ -38,7 +37,9 @@ func initNewCommand() *cobra.Command { Long: tr("Create a new Sketch"), Example: " " + os.Args[0] + " sketch new MultiBlinker", Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { runNewCommand(args, overwrite) }, + Run: func(cmd *cobra.Command, args []string) { + runNewCommand(srv, args, overwrite) + }, } newCommand.Flags().BoolVarP(&overwrite, "overwrite", "f", false, tr("Overwrites an existing .ino sketch.")) @@ -46,7 +47,8 @@ func initNewCommand() *cobra.Command { return newCommand } -func runNewCommand(args []string, overwrite bool) { +func runNewCommand(srv rpc.ArduinoCoreServiceServer, args []string, overwrite bool) { + ctx := context.Background() logrus.Info("Executing `arduino-cli sketch new`") // Trim to avoid issues if user creates a sketch adding the .ino extesion to the name inputSketchName := args[0] @@ -72,7 +74,7 @@ func runNewCommand(args []string, overwrite bool) { sketchName = sketchDirPath.Base() } - _, err = commands.NewSketch(context.Background(), &rpc.NewSketchRequest{ + _, err = srv.NewSketch(ctx, &rpc.NewSketchRequest{ SketchName: sketchName, SketchDir: sketchDir, Overwrite: overwrite, diff --git a/internal/cli/sketch/sketch.go b/internal/cli/sketch/sketch.go index 5d8da390eed..2530c72247d 100644 --- a/internal/cli/sketch/sketch.go +++ b/internal/cli/sketch/sketch.go @@ -19,13 +19,14 @@ import ( "os" "github.com/arduino/arduino-cli/internal/i18n" + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/spf13/cobra" ) var tr = i18n.Tr // NewCommand created a new `sketch` command -func NewCommand() *cobra.Command { +func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { sketchCommand := &cobra.Command{ Use: "sketch", Short: tr("Arduino CLI sketch commands."), @@ -33,8 +34,8 @@ func NewCommand() *cobra.Command { Example: " " + os.Args[0] + " sketch new MySketch", } - sketchCommand.AddCommand(initNewCommand()) - sketchCommand.AddCommand(initArchiveCommand()) + sketchCommand.AddCommand(initNewCommand(srv)) + sketchCommand.AddCommand(initArchiveCommand(srv)) return sketchCommand } diff --git a/internal/cli/upload/upload.go b/internal/cli/upload/upload.go index ed49145b98c..751083aebd5 100644 --- a/internal/cli/upload/upload.go +++ b/internal/cli/upload/upload.go @@ -69,7 +69,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { fqbnArg.AddToCommand(uploadCommand, srv) portArgs.AddToCommand(uploadCommand, srv) - profileArg.AddToCommand(uploadCommand) + profileArg.AddToCommand(uploadCommand, srv) uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries to upload.")) uploadCommand.Flags().StringVarP(&importFile, "input-file", "i", "", tr("Binary file to upload.")) uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, tr("Verify uploaded binary after the upload.")) @@ -91,7 +91,8 @@ func runUploadCommand(srv rpc.ArduinoCoreServiceServer, args []string, uploadFie path = args[0] } sketchPath := arguments.InitSketchPath(path) - sketch, err := commands.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) + resp, err := srv.LoadSketch(ctx, &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) + sketch := resp.GetSketch() if importDir == "" && importFile == "" { if err != nil { feedback.Fatal(tr("Error during Upload: %v", err), feedback.ErrGeneric)