Unverified Commit ab73719d authored by MatteoPologruto's avatar MatteoPologruto Committed by GitHub

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.
parent c5709162
......@@ -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]
......@@ -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)
......
......@@ -17,6 +17,7 @@ package sketch
import (
"context"
"errors"
"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/globals"
......@@ -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}
}
......
......@@ -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())
}
......@@ -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 {
......
......@@ -89,6 +89,7 @@ type DownloadProgress struct {
unknownFields protoimpl.UnknownFields
// Types that are assignable to Message:
//
// *DownloadProgress_Start
// *DownloadProgress_Update
// *DownloadProgress_End
......
......@@ -1770,7 +1770,8 @@ type Library struct {
// Map of FQBNs that specifies if library is compatible with this library
CompatibleWith map[string]bool `protobuf:"bytes,28,rep,name=compatible_with,json=compatibleWith,proto3" json:"compatible_with,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"`
// This value is set to true if the library is in development and should not
// be treated as read-only.
// be treated as read-only. This status is determined by the presence of a
// `.development` file in the library root directory.
InDevelopment bool `protobuf:"varint,29,opt,name=in_development,json=inDevelopment,proto3" json:"in_development,omitempty"`
}
......
......@@ -216,7 +216,6 @@ func (x *DebugConfigRequest) GetProgrammer() string {
return ""
}
//
type DebugResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment