Skip to content

Commit 5b38d4a

Browse files
committed
Removed some duplicated code
1 parent 68c20cd commit 5b38d4a

File tree

6 files changed

+15
-34
lines changed

6 files changed

+15
-34
lines changed

cli/core/install.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func initInstallCommand() *cobra.Command {
4343
Args: cobra.MinimumNArgs(1),
4444
Run: runInstallCommand,
4545
}
46-
addPostInstallFlagsToCommand(installCommand)
46+
AddPostInstallFlagsToCommand(installCommand)
4747
return installCommand
4848
}
4949

@@ -52,12 +52,15 @@ var postInstallFlags struct {
5252
skipPostInstall bool
5353
}
5454

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

60-
func detectSkipPostInstallValue() bool {
62+
// DetectSkipPostInstallValue returns true if a post install script must be run
63+
func DetectSkipPostInstallValue() bool {
6164
if postInstallFlags.runPostInstall && postInstallFlags.skipPostInstall {
6265
feedback.Errorf("The flags --run-post-install and --skip-post-install can't be both set at the same time.")
6366
os.Exit(errorcodes.ErrBadArgument)
@@ -100,7 +103,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
100103
PlatformPackage: platformRef.PackageName,
101104
Architecture: platformRef.Architecture,
102105
Version: platformRef.Version,
103-
SkipPostInstall: detectSkipPostInstallValue(),
106+
SkipPostInstall: DetectSkipPostInstallValue(),
104107
}
105108
_, err := core.PlatformInstall(context.Background(), platformInstallReq, output.ProgressBar(), output.TaskProgress())
106109
if err != nil {

cli/core/upgrade.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func initUpgradeCommand() *cobra.Command {
4242
" " + os.Args[0] + " core upgrade arduino:samd",
4343
Run: runUpgradeCommand,
4444
}
45-
addPostInstallFlagsToCommand(upgradeCommand)
45+
AddPostInstallFlagsToCommand(upgradeCommand)
4646
return upgradeCommand
4747
}
4848

@@ -92,7 +92,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9292
Instance: inst,
9393
PlatformPackage: platformRef.PackageName,
9494
Architecture: platformRef.Architecture,
95-
SkipPostInstall: detectSkipPostInstallValue(),
95+
SkipPostInstall: DetectSkipPostInstallValue(),
9696
}
9797

9898
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())

cli/upgrade/upgrade.go

+3-26
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
"context"
2020
"os"
2121

22+
"github.com/arduino/arduino-cli/cli/core"
2223
"github.com/arduino/arduino-cli/cli/errorcodes"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/cli/instance"
2526
"github.com/arduino/arduino-cli/cli/output"
2627
"github.com/arduino/arduino-cli/commands"
27-
"github.com/arduino/arduino-cli/configuration"
2828
rpc "github.com/arduino/arduino-cli/rpc/commands"
2929
"github.com/sirupsen/logrus"
3030
"github.com/spf13/cobra"
@@ -35,28 +35,6 @@ var postInstallFlags struct {
3535
skipPostInstall bool
3636
}
3737

38-
func detectSkipPostInstallValue() bool {
39-
if postInstallFlags.runPostInstall && postInstallFlags.skipPostInstall {
40-
feedback.Errorf("The flags --run-post-install and --skip-post-install can't be both set at the same time.")
41-
os.Exit(errorcodes.ErrBadArgument)
42-
}
43-
if postInstallFlags.runPostInstall {
44-
logrus.Info("Will run post-install by user request")
45-
return false
46-
}
47-
if postInstallFlags.skipPostInstall {
48-
logrus.Info("Will skip post-install by user request")
49-
return true
50-
}
51-
52-
if !configuration.IsInteractive {
53-
logrus.Info("Not running from console, will skip post-install by default")
54-
return true
55-
}
56-
logrus.Info("Running from console, will run post-install by default")
57-
return false
58-
}
59-
6038
// NewCommand creates a new `upgrade` command
6139
func NewCommand() *cobra.Command {
6240
upgradeCommand := &cobra.Command{
@@ -68,8 +46,7 @@ func NewCommand() *cobra.Command {
6846
Run: runUpgradeCommand,
6947
}
7048

71-
upgradeCommand.Flags().BoolVar(&postInstallFlags.runPostInstall, "run-post-install", false, "Force run of post-install scripts (if the CLI is not running interactively).")
72-
upgradeCommand.Flags().BoolVar(&postInstallFlags.skipPostInstall, "skip-post-install", false, "Force skip of post-install scripts (if the CLI is running interactively).")
49+
core.AddPostInstallFlagsToCommand(upgradeCommand)
7350
return upgradeCommand
7451
}
7552

@@ -84,7 +61,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
8461

8562
err = commands.Upgrade(context.Background(), &rpc.UpgradeReq{
8663
Instance: inst,
87-
SkipPostInstall: detectSkipPostInstallValue(),
64+
SkipPostInstall: core.DetectSkipPostInstallValue(),
8865
}, output.NewDownloadProgressBarCB(), output.TaskProgress())
8966

9067
if err != nil {

commands/instances.go

+1
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeReq, downloadCB DownloadProgre
568568
if err := pm.UninstallPlatform(latest); err != nil {
569569
logrus.WithError(err).Error("Error rolling-back changes.")
570570
taskCB(&rpc.TaskProgress{Message: "Error rolling-back changes: " + err.Error()})
571+
return err
571572
}
572573
}
573574

rpc/commands/commands.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/commands/commands.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ message UpgradeReq {
222222
// Arduino Core Service instance from the Init response.
223223
Instance instance = 1;
224224

225-
// Set to true to not run (eventual) post install scripts for trusted platforms
225+
// Set to true to not run (eventual) post install scripts
226226
bool skipPostInstall = 2;
227227
}
228228

0 commit comments

Comments
 (0)