Skip to content

Commit 11c80e2

Browse files
committed
refactor and reorganize core commands:
move `DetectSkipPostInstallValue` in arguments package create `PostInstallFlags` struct in arguments package remove `listFlags` struct
1 parent e20ddab commit 11c80e2

File tree

6 files changed

+96
-83
lines changed

6 files changed

+96
-83
lines changed

cli/arguments/port.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arduino/arduino-cli/cli/errorcodes"
2727
"github.com/arduino/arduino-cli/cli/feedback"
2828
"github.com/arduino/arduino-cli/commands"
29+
"github.com/arduino/arduino-cli/configuration"
2930
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3031
"github.com/pkg/errors"
3132
"github.com/sirupsen/logrus"
@@ -55,6 +56,12 @@ type Programmer struct {
5556
programmer string
5657
}
5758

59+
// PostInstallFlags contains flags used by the
60+
type PostInstallFlags struct {
61+
runPostInstall bool // force the execution of installation scripts
62+
skipPostInstall bool //skip the execution of installation scripts
63+
}
64+
5865
// AddToCommand adds the flags used to set port and protocol to the specified Command
5966
func (p *Port) AddToCommand(cmd *cobra.Command) {
6067
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
@@ -84,6 +91,23 @@ func (p *Programmer) AddToCommand(cmd *cobra.Command) {
8491
})
8592
}
8693

94+
// AddToCommand adds flags that can be used to force running or skipping
95+
// of post installation scripts
96+
func (p *PostInstallFlags) AddToCommand(cmd *cobra.Command) {
97+
cmd.Flags().BoolVar(&p.runPostInstall, "run-post-install", false, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
98+
cmd.Flags().BoolVar(&p.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
99+
}
100+
101+
// GetRunPostInstall returns the runPostInstall flag
102+
func (p *PostInstallFlags) GetRunPostInstall() bool {
103+
return p.runPostInstall
104+
}
105+
106+
// GetSkipPostInstall returns the skipPostInstall
107+
func (p *PostInstallFlags) GetSkipPostInstall() bool {
108+
return p.skipPostInstall
109+
}
110+
87111
// GetFQBN returns the fqbn
88112
func (f *Fqbn) GetFQBN() string {
89113
return f.fqbn
@@ -201,3 +225,22 @@ func CheckFlagsConflicts(command *cobra.Command, firstFlagName, secondFlagName s
201225
os.Exit(errorcodes.ErrBadArgument)
202226
}
203227
}
228+
229+
// DetectSkipPostInstallValue returns true if a post install script must be run
230+
func (p *PostInstallFlags) DetectSkipPostInstallValue() bool {
231+
if p.GetRunPostInstall() {
232+
logrus.Info("Will run post-install by user request")
233+
return false
234+
}
235+
if p.GetSkipPostInstall() {
236+
logrus.Info("Will skip post-install by user request")
237+
return true
238+
}
239+
240+
if !configuration.IsInteractive {
241+
logrus.Info("Not running from console, will skip post-install by default")
242+
return true
243+
}
244+
logrus.Info("Running from console, will run post-install by default")
245+
return false
246+
}

cli/core/install.go

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ import (
2626
"github.com/arduino/arduino-cli/cli/instance"
2727
"github.com/arduino/arduino-cli/cli/output"
2828
"github.com/arduino/arduino-cli/commands/core"
29-
"github.com/arduino/arduino-cli/configuration"
3029
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3130
"github.com/sirupsen/logrus"
3231
"github.com/spf13/cobra"
3332
)
3433

34+
var (
35+
postInstallFlags arguments.PostInstallFlags
36+
)
37+
3538
func initInstallCommand() *cobra.Command {
3639
installCommand := &cobra.Command{
3740
Use: fmt.Sprintf("install %s:%s[@%s]...", tr("PACKAGER"), tr("ARCH"), tr("VERSION")),
@@ -42,50 +45,18 @@ func initInstallCommand() *cobra.Command {
4245
" # " + tr("download a specific version (in this case 1.6.9).") + "\n" +
4346
" " + os.Args[0] + " core install arduino:samd@1.6.9",
4447
Args: cobra.MinimumNArgs(1),
45-
Run: runInstallCommand,
48+
PreRun: func(cmd *cobra.Command, args []string) {
49+
arguments.CheckFlagsConflicts(cmd, "run-post-install", "skip-post-install")
50+
},
51+
Run: runInstallCommand,
4652
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4753
return arguments.GetInstallableCores(), cobra.ShellCompDirectiveDefault
4854
},
4955
}
50-
AddPostInstallFlagsToCommand(installCommand)
56+
postInstallFlags.AddToCommand(installCommand)
5157
return installCommand
5258
}
5359

54-
var postInstallFlags struct {
55-
runPostInstall bool
56-
skipPostInstall bool
57-
}
58-
59-
// AddPostInstallFlagsToCommand adds flags that can be used to force running or skipping
60-
// of post installation scripts
61-
func AddPostInstallFlagsToCommand(cmd *cobra.Command) {
62-
cmd.Flags().BoolVar(&postInstallFlags.runPostInstall, "run-post-install", false, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
63-
cmd.Flags().BoolVar(&postInstallFlags.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
64-
}
65-
66-
// DetectSkipPostInstallValue returns true if a post install script must be run
67-
func DetectSkipPostInstallValue() bool {
68-
if postInstallFlags.runPostInstall && postInstallFlags.skipPostInstall {
69-
feedback.Errorf(tr("The flags --run-post-install and --skip-post-install can't be both set at the same time."))
70-
os.Exit(errorcodes.ErrBadArgument)
71-
}
72-
if postInstallFlags.runPostInstall {
73-
logrus.Info("Will run post-install by user request")
74-
return false
75-
}
76-
if postInstallFlags.skipPostInstall {
77-
logrus.Info("Will skip post-install by user request")
78-
return true
79-
}
80-
81-
if !configuration.IsInteractive {
82-
logrus.Info("Not running from console, will skip post-install by default")
83-
return true
84-
}
85-
logrus.Info("Running from console, will run post-install by default")
86-
return false
87-
}
88-
8960
func runInstallCommand(cmd *cobra.Command, args []string) {
9061
inst := instance.CreateAndInit()
9162
logrus.Info("Executing `arduino core install`")
@@ -102,7 +73,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
10273
PlatformPackage: platformRef.PackageName,
10374
Architecture: platformRef.Architecture,
10475
Version: platformRef.Version,
105-
SkipPostInstall: DetectSkipPostInstallValue(),
76+
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
10677
}
10778
_, err := core.PlatformInstall(context.Background(), platformInstallRequest, output.ProgressBar(), output.TaskProgress())
10879
if err != nil {

cli/core/list.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import (
2929
"github.com/spf13/cobra"
3030
)
3131

32+
var (
33+
updatableOnly bool
34+
all bool
35+
)
36+
3237
func initListCommand() *cobra.Command {
3338
listCommand := &cobra.Command{
3439
Use: "list",
@@ -38,24 +43,19 @@ func initListCommand() *cobra.Command {
3843
Args: cobra.NoArgs,
3944
Run: runListCommand,
4045
}
41-
listCommand.Flags().BoolVar(&listFlags.updatableOnly, "updatable", false, tr("List updatable platforms."))
42-
listCommand.Flags().BoolVar(&listFlags.all, "all", false, tr("If set return all installable and installed cores, including manually installed."))
46+
listCommand.Flags().BoolVar(&updatableOnly, "updatable", false, tr("List updatable platforms."))
47+
listCommand.Flags().BoolVar(&all, "all", false, tr("If set return all installable and installed cores, including manually installed."))
4348
return listCommand
4449
}
4550

46-
var listFlags struct {
47-
updatableOnly bool
48-
all bool
49-
}
50-
5151
func runListCommand(cmd *cobra.Command, args []string) {
5252
inst := instance.CreateAndInit()
5353
logrus.Info("Executing `arduino core list`")
5454

5555
platforms, err := core.GetPlatforms(&rpc.PlatformListRequest{
5656
Instance: inst,
57-
UpdatableOnly: listFlags.updatableOnly,
58-
All: listFlags.all,
57+
UpdatableOnly: updatableOnly,
58+
All: all,
5959
})
6060
if err != nil {
6161
feedback.Errorf(tr("Error listing platforms: %v"), err)

cli/core/upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func initUpgradeCommand() *cobra.Command {
4545
" " + os.Args[0] + " core upgrade arduino:samd",
4646
Run: runUpgradeCommand,
4747
}
48-
AddPostInstallFlagsToCommand(upgradeCommand)
48+
postInstallFlags.AddToCommand(upgradeCommand)
4949
return upgradeCommand
5050
}
5151

@@ -93,7 +93,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9393
Instance: inst,
9494
PlatformPackage: platformRef.PackageName,
9595
Architecture: platformRef.Architecture,
96-
SkipPostInstall: DetectSkipPostInstallValue(),
96+
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
9797
}
9898

9999
if _, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress()); err != nil {

cli/upgrade/upgrade.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"context"
2020
"os"
2121

22-
"github.com/arduino/arduino-cli/cli/core"
22+
"github.com/arduino/arduino-cli/cli/arguments"
2323
"github.com/arduino/arduino-cli/cli/feedback"
2424
"github.com/arduino/arduino-cli/cli/instance"
2525
"github.com/arduino/arduino-cli/cli/output"
@@ -30,7 +30,10 @@ import (
3030
"github.com/spf13/cobra"
3131
)
3232

33-
var tr = i18n.Tr
33+
var (
34+
tr = i18n.Tr
35+
postInstallFlags arguments.PostInstallFlags
36+
)
3437

3538
// NewCommand creates a new `upgrade` command
3639
func NewCommand() *cobra.Command {
@@ -43,7 +46,7 @@ func NewCommand() *cobra.Command {
4346
Run: runUpgradeCommand,
4447
}
4548

46-
core.AddPostInstallFlagsToCommand(upgradeCommand)
49+
postInstallFlags.AddToCommand(upgradeCommand)
4750
return upgradeCommand
4851
}
4952

@@ -53,7 +56,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
5356

5457
err := commands.Upgrade(context.Background(), &rpc.UpgradeRequest{
5558
Instance: inst,
56-
SkipPostInstall: core.DetectSkipPostInstallValue(),
59+
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
5760
}, output.NewDownloadProgressBarCB(), output.TaskProgress())
5861

5962
if err != nil {

0 commit comments

Comments
 (0)