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 ( ...@@ -30,10 +30,10 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var includeBuildDir bool
// initArchiveCommand creates a new `archive` command // initArchiveCommand creates a new `archive` command
func initArchiveCommand() *cobra.Command { func initArchiveCommand() *cobra.Command {
var includeBuildDir, overwrite bool
archiveCommand := &cobra.Command{ archiveCommand := &cobra.Command{
Use: fmt.Sprintf("archive <%s> <%s>", tr("sketchPath"), tr("archivePath")), Use: fmt.Sprintf("archive <%s> <%s>", tr("sketchPath"), tr("archivePath")),
Short: tr("Creates a zip file containing all sketch files."), Short: tr("Creates a zip file containing all sketch files."),
...@@ -45,15 +45,16 @@ func initArchiveCommand() *cobra.Command { ...@@ -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\n" +
" " + os.Args[0] + " archive /home/user/Arduino/MySketch /home/user/MySketchArchive.zip", " " + os.Args[0] + " archive /home/user/Arduino/MySketch /home/user/MySketchArchive.zip",
Args: cobra.MaximumNArgs(2), 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().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 return archiveCommand
} }
func runArchiveCommand(cmd *cobra.Command, args []string) { func runArchiveCommand(args []string, includeBuildDir bool, overwrite bool) {
logrus.Info("Executing `arduino-cli sketch archive`") logrus.Info("Executing `arduino-cli sketch archive`")
sketchPath := paths.New(".") sketchPath := paths.New(".")
...@@ -73,6 +74,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) { ...@@ -73,6 +74,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
SketchPath: sketchPath.String(), SketchPath: sketchPath.String(),
ArchivePath: archivePath, ArchivePath: archivePath,
IncludeBuildDir: includeBuildDir, IncludeBuildDir: includeBuildDir,
Overwrite: overwrite,
}) })
if err != nil { if err != nil {
......
...@@ -66,8 +66,10 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc ...@@ -66,8 +66,10 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.Arc
archivePath = paths.New(archivePath.String() + ".zip") archivePath = paths.New(archivePath.String() + ".zip")
} }
if archivePath.Exist() { if !req.Overwrite {
return nil, &arduino.InvalidArgumentError{Message: tr("Archive already exists")} if archivePath.Exist() {
return nil, &arduino.InvalidArgumentError{Message: tr("Archive already exists")}
}
} }
filesToZip, err := sketchPath.ReadDirRecursive() filesToZip, err := sketchPath.ReadDirRecursive()
......
...@@ -388,3 +388,26 @@ func TestSketchNewDotArgOverwrite(t *testing.T) { ...@@ -388,3 +388,26 @@ func TestSketchNewDotArgOverwrite(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.FileExists(t, sketchPath.Join(sketchNew+".ino").String()) 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 { ...@@ -293,6 +293,8 @@ message ArchiveSketchRequest {
string archive_path = 2; string archive_path = 2;
// Specifies if build directory should be included in the archive // Specifies if build directory should be included in the archive
bool include_build_dir = 3; bool include_build_dir = 3;
// Allows to override an already existing archive
bool overwrite = 4;
} }
message ArchiveSketchResponse {} 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