Skip to content

Fix sketch new default overwriting behavior #1993

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

Merged
merged 3 commits into from
Nov 29, 2022
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
10 changes: 8 additions & 2 deletions cli/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ import (
)

func initNewCommand() *cobra.Command {
var overwrite bool

newCommand := &cobra.Command{
Use: "new",
Short: tr("Create a new Sketch"),
Long: tr("Create a new Sketch"),
Example: " " + os.Args[0] + " sketch new MultiBlinker",
Args: cobra.ExactArgs(1),
Run: runNewCommand,
Run: func(cmd *cobra.Command, args []string) { runNewCommand(args, overwrite) },
}

newCommand.Flags().BoolVarP(&overwrite, "overwrite", "f", false, tr("Overwrites an existing .ino sketch."))

return newCommand
}

func runNewCommand(cmd *cobra.Command, args []string) {
func runNewCommand(args []string, overwrite bool) {
logrus.Info("Executing `arduino-cli sketch new`")
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
sketchName := args[0]
Expand All @@ -56,6 +61,7 @@ func runNewCommand(cmd *cobra.Command, args []string) {
Instance: nil,
SketchName: sketchDirPath.Base(),
SketchDir: sketchDirPath.Parent().String(),
Overwrite: overwrite,
})
if err != nil {
feedback.Errorf(tr("Error creating sketch: %v"), err)
Expand Down
6 changes: 6 additions & 0 deletions commands/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package sketch

import (
"context"
"errors"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/globals"
Expand Down Expand Up @@ -47,6 +48,11 @@ func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchRe
}
sketchName := sketchDirPath.Base()
sketchMainFilePath := sketchDirPath.Join(sketchName + globals.MainFileValidExtension)
if !req.Overwrite {
if sketchMainFilePath.Exist() {
return nil, &arduino.CantCreateSketchError{Cause: errors.New(tr(".ino file already exists"))}
}
}
if err := sketchMainFilePath.WriteFile(emptySketch); err != nil {
return nil, &arduino.CantCreateSketchError{Cause: err}
}
Expand Down
26 changes: 26 additions & 0 deletions internal/integrationtest/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,29 @@ func TestSketchArchiveCaseMismatchFails(t *testing.T) {
require.Error(t, err)
require.Contains(t, string(stderr), "Error archiving: Can't open sketch:")
}

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

sketchNew := "SketchNewDotArgOverwrite"
sketchPath := cli.SketchbookDir().Join(sketchNew)
require.NoError(t, sketchPath.MkdirAll())

cli.SetWorkingDir(sketchPath)
require.NoFileExists(t, sketchPath.Join(sketchNew+".ino").String())
// Create a new sketch
_, _, err := cli.Run("sketch", "new", ".")
require.NoError(t, err)

require.FileExists(t, sketchPath.Join(sketchNew+".ino").String())
// Tries to overwrite the existing sketch with a new one, but it should fail
_, stderr, err := cli.Run("sketch", "new", ".")
require.Error(t, err)
require.Contains(t, string(stderr), ".ino file already exists")

// Create a new sketch, overwriting the existing one
_, _, err = cli.Run("sketch", "new", ".", "--overwrite")
require.NoError(t, err)
require.FileExists(t, sketchPath.Join(sketchNew+".ino").String())
}
616 changes: 314 additions & 302 deletions rpc/cc/arduino/cli/commands/v1/commands.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions rpc/cc/arduino/cli/commands/v1/commands.proto
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ message NewSketchRequest {
// Default Sketchbook directory "directories.User" is used if sketch_dir is
// empty.
string sketch_dir = 3;
// Specificies if an existing .ino sketch should be overwritten
bool overwrite = 4;
}

message NewSketchResponse {
Expand Down
1 change: 1 addition & 0 deletions rpc/cc/arduino/cli/commands/v1/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rpc/cc/arduino/cli/commands/v1/lib.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion rpc/cc/arduino/cli/debug/v1/debug.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.