Skip to content
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

Enable sketch archive overrides using overwrite flag #1994

Merged
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: 6 additions & 4 deletions cli/sketch/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
Expand All @@ -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(".")
Expand All @@ -73,6 +74,7 @@ func runArchiveCommand(cmd *cobra.Command, args []string) {
SketchPath: sketchPath.String(),
ArchivePath: archivePath,
IncludeBuildDir: includeBuildDir,
Overwrite: overwrite,
})

if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions commands/sketch/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions internal/integrationtest/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading