Skip to content

Commit f962e64

Browse files
Add --overwrite flag to sketch new command
Using the "--overwrite" flag, allows to create a new sketch overwriting an existing one.
1 parent 7ecb034 commit f962e64

File tree

8 files changed

+336
-308
lines changed

8 files changed

+336
-308
lines changed

Diff for: 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)

Diff for: commands/sketch/new.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchRe
4848
}
4949
sketchName := sketchDirPath.Base()
5050
sketchMainFilePath := sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
51-
if sketchMainFilePath.Exist() {
52-
return nil, &arduino.CantCreateSketchError{Cause: errors.New(".ino file already exists")}
51+
if !req.Overwrite {
52+
if sketchMainFilePath.Exist() {
53+
return nil, &arduino.CantCreateSketchError{Cause: errors.New(tr(".ino file already exists"))}
54+
}
5355
}
5456
if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
5557
return nil, &arduino.CantCreateSketchError{Cause: err}

Diff for: internal/integrationtest/sketch/sketch_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,9 @@ func TestSketchNewDotArgOverwrite(t *testing.T) {
382382
_, stderr, err := cli.Run("sketch", "new", ".")
383383
require.Error(t, err)
384384
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())
385390
}

Diff for: 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.

Diff for: 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 {

Diff for: 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.

Diff for: 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.

Diff for: 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)