Skip to content

Commit ab73719

Browse files
Fix sketch new default overwriting behavior (#1993)
* Check .ino sketch file exists in path before creating a new one Running "arduino-cli sketch new ." from a sketch directory overwrites an existing .ino sketch file, without asking for confirmation. This fix introduces a check to verify if the specified path already contains a .ino sketch file. If it does, an error is returned, otherwise a new sketch is created as usual. * Add test to check if sketch new does not overwrite an already existing .ino sketch file * Add --overwrite flag to sketch new command Using the "--overwrite" flag, allows to create a new sketch overwriting an existing one.
1 parent c570916 commit ab73719

File tree

8 files changed

+359
-306
lines changed

8 files changed

+359
-306
lines changed

cli/sketch/new.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,23 @@ import (
3131
)
3232

3333
func initNewCommand() *cobra.Command {
34+
var overwrite bool
35+
3436
newCommand := &cobra.Command{
3537
Use: "new",
3638
Short: tr("Create a new Sketch"),
3739
Long: tr("Create a new Sketch"),
3840
Example: " " + os.Args[0] + " sketch new MultiBlinker",
3941
Args: cobra.ExactArgs(1),
40-
Run: runNewCommand,
42+
Run: func(cmd *cobra.Command, args []string) { runNewCommand(args, overwrite) },
4143
}
44+
45+
newCommand.Flags().BoolVarP(&overwrite, "overwrite", "f", false, tr("Overwrites an existing .ino sketch."))
46+
4247
return newCommand
4348
}
4449

45-
func runNewCommand(cmd *cobra.Command, args []string) {
50+
func runNewCommand(args []string, overwrite bool) {
4651
logrus.Info("Executing `arduino-cli sketch new`")
4752
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
4853
sketchName := args[0]
@@ -56,6 +61,7 @@ func runNewCommand(cmd *cobra.Command, args []string) {
5661
Instance: nil,
5762
SketchName: sketchDirPath.Base(),
5863
SketchDir: sketchDirPath.Parent().String(),
64+
Overwrite: overwrite,
5965
})
6066
if err != nil {
6167
feedback.Errorf(tr("Error creating sketch: %v"), err)

commands/sketch/new.go

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package sketch
1717

1818
import (
1919
"context"
20+
"errors"
2021

2122
"github.com/arduino/arduino-cli/arduino"
2223
"github.com/arduino/arduino-cli/arduino/globals"
@@ -47,6 +48,11 @@ func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchRe
4748
}
4849
sketchName := sketchDirPath.Base()
4950
sketchMainFilePath := sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
51+
if !req.Overwrite {
52+
if sketchMainFilePath.Exist() {
53+
return nil, &arduino.CantCreateSketchError{Cause: errors.New(tr(".ino file already exists"))}
54+
}
55+
}
5056
if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
5157
return nil, &arduino.CantCreateSketchError{Cause: err}
5258
}

internal/integrationtest/sketch/sketch_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,29 @@ func TestSketchArchiveCaseMismatchFails(t *testing.T) {
362362
require.Error(t, err)
363363
require.Contains(t, string(stderr), "Error archiving: Can't open sketch:")
364364
}
365+
366+
func TestSketchNewDotArgOverwrite(t *testing.T) {
367+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
368+
defer env.CleanUp()
369+
370+
sketchNew := "SketchNewDotArgOverwrite"
371+
sketchPath := cli.SketchbookDir().Join(sketchNew)
372+
require.NoError(t, sketchPath.MkdirAll())
373+
374+
cli.SetWorkingDir(sketchPath)
375+
require.NoFileExists(t, sketchPath.Join(sketchNew+".ino").String())
376+
// Create a new sketch
377+
_, _, err := cli.Run("sketch", "new", ".")
378+
require.NoError(t, err)
379+
380+
require.FileExists(t, sketchPath.Join(sketchNew+".ino").String())
381+
// Tries to overwrite the existing sketch with a new one, but it should fail
382+
_, stderr, err := cli.Run("sketch", "new", ".")
383+
require.Error(t, err)
384+
require.Contains(t, string(stderr), ".ino file already exists")
385+
386+
// Create a new sketch, overwriting the existing one
387+
_, _, err = cli.Run("sketch", "new", ".", "--overwrite")
388+
require.NoError(t, err)
389+
require.FileExists(t, sketchPath.Join(sketchNew+".ino").String())
390+
}

rpc/cc/arduino/cli/commands/v1/commands.pb.go

+314-302
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/commands.proto

+2
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ message NewSketchRequest {
255255
// Default Sketchbook directory "directories.User" is used if sketch_dir is
256256
// empty.
257257
string sketch_dir = 3;
258+
// Specificies if an existing .ino sketch should be overwritten
259+
bool overwrite = 4;
258260
}
259261

260262
message NewSketchResponse {

rpc/cc/arduino/cli/commands/v1/common.pb.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/lib.pb.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/debug/v1/debug.pb.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)