Commit ea0334cd authored by Umberto Baldi's avatar Umberto Baldi

refactor and reorganize core commands:

move `DetectSkipPostInstallValue` in arguments package
create `PostInstallFlags` struct in arguments package
remove `listFlags` struct
parent 47821aef
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package arguments
import (
"github.com/arduino/arduino-cli/configuration"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// PostInstallFlags contains flags data used by the core install and the upgrade command
// This is useful so all flags used by commands that need
// this information are consistent with each other.
type PostInstallFlags struct {
runPostInstall bool // force the execution of installation scripts
skipPostInstall bool //skip the execution of installation scripts
}
// AddToCommand adds flags that can be used to force running or skipping
// of post installation scripts
func (p *PostInstallFlags) AddToCommand(cmd *cobra.Command) {
cmd.Flags().BoolVar(&p.runPostInstall, "run-post-install", false, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
cmd.Flags().BoolVar(&p.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
}
// GetRunPostInstall returns the run-post-install flag value
func (p *PostInstallFlags) GetRunPostInstall() bool {
return p.runPostInstall
}
// GetSkipPostInstall returns the skip-post-install flag value
func (p *PostInstallFlags) GetSkipPostInstall() bool {
return p.skipPostInstall
}
// DetectSkipPostInstallValue returns true if a post install script must be run
func (p *PostInstallFlags) DetectSkipPostInstallValue() bool {
if p.GetRunPostInstall() {
logrus.Info("Will run post-install by user request")
return false
}
if p.GetSkipPostInstall() {
logrus.Info("Will skip post-install by user request")
return true
}
if !configuration.IsInteractive {
logrus.Info("Not running from console, will skip post-install by default")
return true
}
logrus.Info("Running from console, will run post-install by default")
return false
}
......@@ -26,12 +26,15 @@ import (
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
postInstallFlags arguments.PostInstallFlags
)
func initInstallCommand() *cobra.Command {
installCommand := &cobra.Command{
Use: fmt.Sprintf("install %s:%s[@%s]...", tr("PACKAGER"), tr("ARCH"), tr("VERSION")),
......@@ -42,50 +45,18 @@ func initInstallCommand() *cobra.Command {
" # " + tr("download a specific version (in this case 1.6.9).") + "\n" +
" " + os.Args[0] + " core install arduino:samd@1.6.9",
Args: cobra.MinimumNArgs(1),
Run: runInstallCommand,
PreRun: func(cmd *cobra.Command, args []string) {
arguments.CheckFlagsConflicts(cmd, "run-post-install", "skip-post-install")
},
Run: runInstallCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstallableCores(), cobra.ShellCompDirectiveDefault
},
}
AddPostInstallFlagsToCommand(installCommand)
postInstallFlags.AddToCommand(installCommand)
return installCommand
}
var postInstallFlags struct {
runPostInstall bool
skipPostInstall bool
}
// 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, tr("Force run of post-install scripts (if the CLI is not running interactively)."))
cmd.Flags().BoolVar(&postInstallFlags.skipPostInstall, "skip-post-install", false, tr("Force skip of post-install scripts (if the CLI is running interactively)."))
}
// DetectSkipPostInstallValue returns true if a post install script must be run
func DetectSkipPostInstallValue() bool {
if postInstallFlags.runPostInstall && postInstallFlags.skipPostInstall {
feedback.Errorf(tr("The flags --run-post-install and --skip-post-install can't be both set at the same time."))
os.Exit(errorcodes.ErrBadArgument)
}
if postInstallFlags.runPostInstall {
logrus.Info("Will run post-install by user request")
return false
}
if postInstallFlags.skipPostInstall {
logrus.Info("Will skip post-install by user request")
return true
}
if !configuration.IsInteractive {
logrus.Info("Not running from console, will skip post-install by default")
return true
}
logrus.Info("Running from console, will run post-install by default")
return false
}
func runInstallCommand(cmd *cobra.Command, args []string) {
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino core install`")
......@@ -102,7 +73,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
Version: platformRef.Version,
SkipPostInstall: DetectSkipPostInstallValue(),
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
}
_, err := core.PlatformInstall(context.Background(), platformInstallRequest, output.ProgressBar(), output.TaskProgress())
if err != nil {
......
......@@ -29,6 +29,11 @@ import (
"github.com/spf13/cobra"
)
var (
updatableOnly bool
all bool
)
func initListCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: "list",
......@@ -38,24 +43,19 @@ func initListCommand() *cobra.Command {
Args: cobra.NoArgs,
Run: runListCommand,
}
listCommand.Flags().BoolVar(&listFlags.updatableOnly, "updatable", false, tr("List updatable platforms."))
listCommand.Flags().BoolVar(&listFlags.all, "all", false, tr("If set return all installable and installed cores, including manually installed."))
listCommand.Flags().BoolVar(&updatableOnly, "updatable", false, tr("List updatable platforms."))
listCommand.Flags().BoolVar(&all, "all", false, tr("If set return all installable and installed cores, including manually installed."))
return listCommand
}
var listFlags struct {
updatableOnly bool
all bool
}
func runListCommand(cmd *cobra.Command, args []string) {
inst := instance.CreateAndInit()
logrus.Info("Executing `arduino core list`")
platforms, err := core.GetPlatforms(&rpc.PlatformListRequest{
Instance: inst,
UpdatableOnly: listFlags.updatableOnly,
All: listFlags.all,
UpdatableOnly: updatableOnly,
All: all,
})
if err != nil {
feedback.Errorf(tr("Error listing platforms: %v"), err)
......
......@@ -45,7 +45,7 @@ func initUpgradeCommand() *cobra.Command {
" " + os.Args[0] + " core upgrade arduino:samd",
Run: runUpgradeCommand,
}
AddPostInstallFlagsToCommand(upgradeCommand)
postInstallFlags.AddToCommand(upgradeCommand)
return upgradeCommand
}
......@@ -93,7 +93,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
Instance: inst,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
SkipPostInstall: DetectSkipPostInstallValue(),
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
}
if _, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress()); err != nil {
......
......@@ -19,7 +19,7 @@ import (
"context"
"os"
"github.com/arduino/arduino-cli/cli/core"
"github.com/arduino/arduino-cli/cli/arguments"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
......@@ -30,7 +30,10 @@ import (
"github.com/spf13/cobra"
)
var tr = i18n.Tr
var (
tr = i18n.Tr
postInstallFlags arguments.PostInstallFlags
)
// NewCommand creates a new `upgrade` command
func NewCommand() *cobra.Command {
......@@ -43,7 +46,7 @@ func NewCommand() *cobra.Command {
Run: runUpgradeCommand,
}
core.AddPostInstallFlagsToCommand(upgradeCommand)
postInstallFlags.AddToCommand(upgradeCommand)
return upgradeCommand
}
......@@ -53,7 +56,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
err := commands.Upgrade(context.Background(), &rpc.UpgradeRequest{
Instance: inst,
SkipPostInstall: core.DetectSkipPostInstallValue(),
SkipPostInstall: postInstallFlags.DetectSkipPostInstallValue(),
}, output.NewDownloadProgressBarCB(), output.TaskProgress())
if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment