Unverified Commit 76ddec81 authored by MatteoPologruto's avatar MatteoPologruto Committed by GitHub

Enable `sketch archive` overrides using `overwrite` flag (#1994)

* Make includeBuildDir private

* Enable sketch archive override using the --overwrite flag

* Add test to verify the changes
parent ab73719d
......@@ -30,10 +30,10 @@ import (
"github.com/spf13/cobra"
)
var includeBuildDir bool
// initArchiveCommand creates a new `archive` command
func initArchiveCommand() *cobra.Command {
var includeBuildDir, overwrite bool
archiveCommand := &cobra.Command{
Use: fmt.Sprintf("archive <%s> <%s>", tr("sketchPath"), tr("archivePath")),
Short: tr("Creates a zip file containing all sketch files."),
......@@ -45,15 +45,16 @@ 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: runArchiveCommand,
Run: func(cmd *cobra.Command, args []string) { runArchiveCommand(args, includeBuildDir, overwrite) },
}
archiveCommand.Flags().BoolVar(&includeBuildDir, "include-build-dir", false, tr("Includes %s directory in the archive.", "build"))
archiveCommand.Flags().BoolVarP(&overwrite, "overwrite", "f", false, tr("Overwrites an already existing archive"))
return archiveCommand
}
func runArchiveCommand(cmd *cobra.Command, args []string) {
func runArchiveCommand(args []string, includeBuildDir bool, overwrite bool) {
logrus.Info("Executing `arduino-cli sketch archive`")
sketchPath := paths.New(".")
......@@ -73,6 +74,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
SketchPath: sketchPath.String(),
ArchivePath: archivePath,
IncludeBuildDir: includeBuildDir,
Overwrite: overwrite,
})
if err != nil {
......
......@@ -66,8 +66,10 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
archivePath = paths.New(archivePath.String() + ".zip")
}
if archivePath.Exist() {
return nil, &arduino.InvalidArgumentError{Message: tr("Archive already exists")}
if !req.Overwrite {
if archivePath.Exist() {
return nil, &arduino.InvalidArgumentError{Message: tr("Archive already exists")}
}
}
filesToZip, err := sketchPath.ReadDirRecursive()
......
......@@ -388,3 +388,26 @@ func TestSketchNewDotArgOverwrite(t *testing.T) {
require.NoError(t, err)
require.FileExists(t, sketchPath.Join(sketchNew+".ino").String())
}
func TestSketchArchiveOverwrite(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
sketchName := "ArchiveSketchOverwrite"
sketchPath := cli.SketchbookDir().Join(sketchName)
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
_, _, err = cli.Run("sketch", "archive", sketchPath.String())
require.NoError(t, err)
// It is not possibile to override an archive by default
_, stderr, err := cli.Run("sketch", "archive", sketchPath.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Archive already exists")
// Override is enabled by the "overwrite" flag
_, _, err = cli.Run("sketch", "archive", sketchPath.String(), "--overwrite")
require.NoError(t, err)
}
......@@ -293,6 +293,8 @@ message ArchiveSketchRequest {
string archive_path = 2;
// Specifies if build directory should be included in the archive
bool include_build_dir = 3;
// Allows to override an already existing archive
bool overwrite = 4;
}
message ArchiveSketchResponse {}
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