Skip to content

Commit ea0334c

Browse files
committed
refactor and reorganize core commands:
move `DetectSkipPostInstallValue` in arguments package create `PostInstallFlags` struct in arguments package remove `listFlags` struct
1 parent 47821ae commit ea0334c

File tree

5 files changed

+94
-54
lines changed

5 files changed

+94
-54
lines changed

cli/arguments/post_install.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package arguments
17+
18+
import (
19+
"github.com/arduino/arduino-cli/configuration"
20+
"github.com/sirupsen/logrus"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// PostInstallFlags contains flags data used by the core install and the upgrade command
25+
// This is useful so all flags used by commands that need
26+
// this information are consistent with each other.
27+
type PostInstallFlags struct {
28+
runPostInstall bool // force the execution of installation scripts
29+
skipPostInstall bool //skip the execution of installation scripts
30+
}
31+
32+
// AddToCommand adds flags that can be used to force running or skipping
33+
// of post installation scripts
34+
func (p *PostInstallFlags) AddToCommand(cmd *cobra.Command) {
35+
cmd.Flags().BoolVar(&p.runPostInstall, "run-post-install", false, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
36+
cmd.Flags().BoolVar(&p.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
37+
}
38+
39+
// GetRunPostInstall returns the run-post-install flag value
40+
func (p *PostInstallFlags) GetRunPostInstall() bool {
41+
return p.runPostInstall
42+
}
43+
44+
// GetSkipPostInstall returns the skip-post-install flag value
45+
func (p *PostInstallFlags) GetSkipPostInstall() bool {
46+
return p.skipPostInstall
47+
}
48+
49+
// DetectSkipPostInstallValue returns true if a post install script must be run
50+
func (p *PostInstallFlags) DetectSkipPostInstallValue() bool {
51+
if p.GetRunPostInstall() {
52+
logrus.Info("Will run post-install by user request")
53+
return false
54+
}
55+
if p.GetSkipPostInstall() {
56+
logrus.Info("Will skip post-install by user request")
57+
return true
58+
}
59+
60+
if !configuration.IsInteractive {
61+
logrus.Info("Not running from console, will skip post-install by default")
62+
return true
63+
}
64+
logrus.Info("Running from console, will run post-install by default")
65+
return false
66+
}

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)