Skip to content

Add --run-post-install and --skip-post-install flags to upgrade command #953

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 2 commits into from
Sep 24, 2020
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
11 changes: 7 additions & 4 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func initInstallCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runInstallCommand,
}
addPostInstallFlagsToCommand(installCommand)
AddPostInstallFlagsToCommand(installCommand)
return installCommand
}

Expand All @@ -52,12 +52,15 @@ var postInstallFlags struct {
skipPostInstall bool
}

func addPostInstallFlagsToCommand(cmd *cobra.Command) {
// AddPostInstallFlagsToCommand adds flags that can be used to force running or skipping
// of post installation scripts
func AddPostInstallFlagsToCommand(cmd *cobra.Command) {
cmd.Flags().BoolVar(&postInstallFlags.runPostInstall, "run-post-install", false, "Force run of post-install scripts (if the CLI is not running interactively).")
cmd.Flags().BoolVar(&postInstallFlags.skipPostInstall, "skip-post-install", false, "Force skip of post-install scripts (if the CLI is running interactively).")
}

func detectSkipPostInstallValue() bool {
// DetectSkipPostInstallValue returns true if a post install script must be run
func DetectSkipPostInstallValue() bool {
if postInstallFlags.runPostInstall && postInstallFlags.skipPostInstall {
feedback.Errorf("The flags --run-post-install and --skip-post-install can't be both set at the same time.")
os.Exit(errorcodes.ErrBadArgument)
Expand Down Expand Up @@ -100,7 +103,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
Version: platformRef.Version,
SkipPostInstall: detectSkipPostInstallValue(),
SkipPostInstall: DetectSkipPostInstallValue(),
}
_, err := core.PlatformInstall(context.Background(), platformInstallReq, output.ProgressBar(), output.TaskProgress())
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func initUpgradeCommand() *cobra.Command {
" " + os.Args[0] + " core upgrade arduino:samd",
Run: runUpgradeCommand,
}
addPostInstallFlagsToCommand(upgradeCommand)
AddPostInstallFlagsToCommand(upgradeCommand)
return upgradeCommand
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
SkipPostInstall: detectSkipPostInstallValue(),
SkipPostInstall: DetectSkipPostInstallValue(),
}

_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
Expand Down
5 changes: 4 additions & 1 deletion cli/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"os"

"github.com/arduino/arduino-cli/cli/core"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/instance"
Expand All @@ -40,6 +41,7 @@ func NewCommand() *cobra.Command {
Run: runUpgradeCommand,
}

core.AddPostInstallFlagsToCommand(upgradeCommand)
return upgradeCommand
}

Expand All @@ -53,7 +55,8 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino upgrade`")

err = commands.Upgrade(context.Background(), &rpc.UpgradeReq{
Instance: inst,
Instance: inst,
SkipPostInstall: core.DetectSkipPostInstallValue(),
}, output.NewDownloadProgressBarCB(), output.TaskProgress())

if err != nil {
Expand Down
20 changes: 19 additions & 1 deletion commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
toolsToInstall := []*cores.ToolRelease{}
for _, tool := range tools {
if tool.IsInstalled() {
logrus.WithField("tool", tool).Warn("Tool already installed")
taskCB(&rpc.TaskProgress{Name: "Tool " + tool.String() + " already installed", Completed: true})
} else {
toolsToInstall = append(toolsToInstall, tool)
Expand All @@ -536,7 +537,8 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
return err
}

taskCB(&rpc.TaskProgress{Name: "Installing " + latest.String()})
logrus.Info("Updating platform " + installed.String())
taskCB(&rpc.TaskProgress{Name: "Updating " + latest.String()})

// Installs tools
for _, tool := range toolsToInstall {
Expand All @@ -549,6 +551,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
// Installs platform
err = pm.InstallPlatform(latest)
if err != nil {
logrus.WithError(err).Error("Cannot install platform")
taskCB(&rpc.TaskProgress{Message: "Error installing " + latest.String()})
return err
}
Expand All @@ -558,12 +561,27 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre

// In case uninstall fails tries to rollback
if err != nil {
logrus.WithError(err).Error("Error updating platform.")
taskCB(&rpc.TaskProgress{Message: "Error upgrading platform: " + err.Error()})

// Rollback
if err := pm.UninstallPlatform(latest); err != nil {
logrus.WithError(err).Error("Error rolling-back changes.")
taskCB(&rpc.TaskProgress{Message: "Error rolling-back changes: " + err.Error()})
return err
}
}

// Perform post install
if !req.SkipPostInstall {
logrus.Info("Running post_install script")
taskCB(&rpc.TaskProgress{Message: "Configuring platform"})
if err := pm.RunPostInstallScript(latest); err != nil {
taskCB(&rpc.TaskProgress{Message: fmt.Sprintf("WARNING: cannot run post install: %s", err)})
}
} else {
logrus.Info("Skipping platform configuration (post_install run).")
taskCB(&rpc.TaskProgress{Message: "Skipping platform configuration"})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/commands/board.pb.go

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

Loading