From d880e280e01f201bd3a5600a932b9c73fa839421 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 24 Apr 2019 18:08:50 +0200 Subject: [PATCH] Added helper function to extract parsed version from RPC requests --- cli/core/args.go | 34 ++++++++++++++++++---------------- cli/core/args_test.go | 4 ++-- cli/core/download.go | 8 ++++---- cli/core/install.go | 4 ++-- cli/core/uninstall.go | 4 ++-- cli/core/upgrade.go | 4 ++-- commands/core/download.go | 11 +++-------- commands/core/install.go | 11 +++-------- commands/core/uninstall.go | 3 +-- commands/core/upgrade.go | 17 +++++------------ commands/lib/download.go | 11 +++-------- commands/lib/install.go | 13 +++++-------- commands/lib/uninstall.go | 13 +++++-------- commands/version.go | 36 ++++++++++++++++++++++++++++++++++++ 14 files changed, 91 insertions(+), 82 deletions(-) create mode 100644 commands/version.go diff --git a/cli/core/args.go b/cli/core/args.go index 399ddd588fa..fdbf044a20e 100644 --- a/cli/core/args.go +++ b/cli/core/args.go @@ -22,15 +22,20 @@ import ( "os" "strings" - "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/cli" "github.com/arduino/arduino-cli/common/formatter" - semver "go.bug.st/relaxed-semver" ) -// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and returns a platformReference slice. -func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReference { - ret := []*packagemanager.PlatformReference{} +type platformReferenceArg struct { + Package string + Architecture string + Version string +} + +// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and +// returns a platformReferenceArg slice. +func parsePlatformReferenceArgs(args []string) []*platformReferenceArg { + ret := []*platformReferenceArg{} for _, arg := range args { reference, err := parsePlatformReferenceArg(arg) if err != nil { @@ -42,24 +47,21 @@ func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReferen return ret } -func parsePlatformReferenceArg(arg string) (*packagemanager.PlatformReference, error) { +func parsePlatformReferenceArg(arg string) (*platformReferenceArg, error) { split := strings.SplitN(arg, "@", 2) arg = split[0] - var version *semver.Version + version := "" if len(split) > 1 { - if ver, err := semver.Parse(split[1]); err == nil { - version = ver - } else { - return nil, fmt.Errorf("invalid version: %s", err) - } + version = split[1] } + split = strings.Split(arg, ":") if len(split) != 2 { return nil, fmt.Errorf("invalid item %s", arg) } - return &packagemanager.PlatformReference{ - Package: split[0], - PlatformArchitecture: split[1], - PlatformVersion: version, + return &platformReferenceArg{ + Package: split[0], + Architecture: split[1], + Version: version, }, nil } diff --git a/cli/core/args_test.go b/cli/core/args_test.go index 7c0fb361636..4f6f34959de 100644 --- a/cli/core/args_test.go +++ b/cli/core/args_test.go @@ -31,8 +31,8 @@ func TestParsePlatformReferenceArgs(t *testing.T) { ref, err := parsePlatformReferenceArg(arg) require.NoError(t, err) require.Equal(t, pack, ref.Package) - require.Equal(t, arch, ref.PlatformArchitecture) - require.Equal(t, version, ref.PlatformVersion) + require.Equal(t, arch, ref.Architecture) + require.Equal(t, version.String(), ref.Version) } invalid := func(arg string) { _, err := parsePlatformReferenceArg(arg) diff --git a/cli/core/download.go b/cli/core/download.go index 9b8ead4ffbb..2d8ad13fe4c 100644 --- a/cli/core/download.go +++ b/cli/core/download.go @@ -48,15 +48,15 @@ func runDownloadCommand(cmd *cobra.Command, args []string) { logrus.Info("Executing `arduino core download`") platformsRefs := parsePlatformReferenceArgs(args) - for _, platformRef := range platformsRefs { + for i, platformRef := range platformsRefs { _, err := core.PlatformDownload(context.Background(), &rpc.PlatformDownloadReq{ Instance: instance, PlatformPackage: platformRef.Package, - Architecture: platformRef.PlatformArchitecture, - Version: platformRef.PlatformVersion.String(), + Architecture: platformRef.Architecture, + Version: platformRef.Version, }, cli.OutputProgressBar()) if err != nil { - formatter.PrintError(err, "Error downloading "+platformRef.String()) + formatter.PrintError(err, "Error downloading "+args[i]) os.Exit(cli.ErrNetwork) } } diff --git a/cli/core/install.go b/cli/core/install.go index 656c6fed558..03aec847281 100644 --- a/cli/core/install.go +++ b/cli/core/install.go @@ -54,8 +54,8 @@ func runInstallCommand(cmd *cobra.Command, args []string) { _, err := core.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{ Instance: instance, PlatformPackage: platformRef.Package, - Architecture: platformRef.PlatformArchitecture, - Version: platformRef.PlatformVersion.String(), + Architecture: platformRef.Architecture, + Version: platformRef.Version, }, cli.OutputProgressBar(), cli.OutputTaskProgress()) if err != nil { formatter.PrintError(err, "Error during install") diff --git a/cli/core/uninstall.go b/cli/core/uninstall.go index ec3998886a5..581770ffbea 100644 --- a/cli/core/uninstall.go +++ b/cli/core/uninstall.go @@ -51,8 +51,8 @@ func runUninstallCommand(cmd *cobra.Command, args []string) { _, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{ Instance: instance, PlatformPackage: platformRef.Package, - Architecture: platformRef.PlatformArchitecture, - Version: platformRef.PlatformVersion.String(), + Architecture: platformRef.Architecture, + Version: platformRef.Version, }, output.NewTaskProgressCB()) if err != nil { formatter.PrintError(err, "Error during uninstall") diff --git a/cli/core/upgrade.go b/cli/core/upgrade.go index 15bfdcd2bd5..5b5fbbf4c5a 100644 --- a/cli/core/upgrade.go +++ b/cli/core/upgrade.go @@ -53,8 +53,8 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) { _, err := core.PlatformUpgrade(context.Background(), &rpc.PlatformUpgradeReq{ Instance: instance, PlatformPackage: platformRef.Package, - Architecture: platformRef.PlatformArchitecture, - Version: platformRef.PlatformVersion.String(), + Architecture: platformRef.Architecture, + Version: platformRef.Version, }, cli.OutputProgressBar(), cli.OutputTaskProgress()) if err != nil { formatter.PrintError(err, "Error during upgrade") diff --git a/commands/core/download.go b/commands/core/download.go index 88b44846a25..dacaa616c7c 100644 --- a/commands/core/download.go +++ b/commands/core/download.go @@ -26,7 +26,6 @@ import ( "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" - semver "go.bug.st/relaxed-semver" ) func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResp, error) { @@ -35,13 +34,9 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa return nil, errors.New("invalid instance") } - var version *semver.Version - if req.GetVersion() != "" { - if v, err := semver.Parse(req.GetVersion()); err == nil { - version = v - } else { - return nil, fmt.Errorf("invalid version: %s", err) - } + version, err := commands.ParseVersion(req) + if err != nil { + return nil, fmt.Errorf("invalid version: %s", err) } platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{ diff --git a/commands/core/install.go b/commands/core/install.go index d4d75b57051..d16987e5bd6 100644 --- a/commands/core/install.go +++ b/commands/core/install.go @@ -26,7 +26,6 @@ import ( "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" - semver "go.bug.st/relaxed-semver" ) func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, @@ -37,13 +36,9 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, return nil, errors.New("invalid instance") } - var version *semver.Version - if req.GetVersion() != "" { - if v, err := semver.Parse(req.GetVersion()); err == nil { - version = v - } else { - return nil, fmt.Errorf("invalid version: %s", err) - } + version, err := commands.ParseVersion(req) + if err != nil { + return nil, fmt.Errorf("invalid version: %s", err) } platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{ diff --git a/commands/core/uninstall.go b/commands/core/uninstall.go index 8693ce64bdf..e15b628d036 100644 --- a/commands/core/uninstall.go +++ b/commands/core/uninstall.go @@ -26,7 +26,6 @@ import ( "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" - semver "go.bug.st/relaxed-semver" ) func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallReq, taskCB commands.TaskProgressCB) (*rpc.PlatformUninstallResp, error) { @@ -36,7 +35,7 @@ func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallReq, taskC } // If no version is specified consider the installed - version, err := semver.Parse(req.Version) + version, err := commands.ParseVersion(req) if err != nil { return nil, fmt.Errorf("invalid version: %s", err) } diff --git a/commands/core/upgrade.go b/commands/core/upgrade.go index 73bf1e11646..7d1b9354088 100644 --- a/commands/core/upgrade.go +++ b/commands/core/upgrade.go @@ -25,7 +25,6 @@ import ( "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" - semver "go.bug.st/relaxed-semver" ) func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, @@ -37,26 +36,20 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, } // Extract all PlatformReference to platforms that have updates - var version *semver.Version - if req.Version != "" { - if v, err := semver.Parse(req.Version); err == nil { - version = v - } else { - return nil, fmt.Errorf("invalid version: %s", err) - } + version, err := commands.ParseVersion(req) + if err != nil { + return nil, fmt.Errorf("invalid version: %s", err) } ref := &packagemanager.PlatformReference{ Package: req.PlatformPackage, PlatformArchitecture: req.Architecture, PlatformVersion: version} - err := upgradePlatform(pm, ref, downloadCB, taskCB) - if err != nil { + if err := upgradePlatform(pm, ref, downloadCB, taskCB); err != nil { return nil, err } - _, err = commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}) - if err != nil { + if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil { return nil, err } diff --git a/commands/lib/download.go b/commands/lib/download.go index 03ec464a68e..53215409b8d 100644 --- a/commands/lib/download.go +++ b/commands/lib/download.go @@ -26,7 +26,6 @@ import ( "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" "github.com/sirupsen/logrus" - semver "go.bug.st/relaxed-semver" ) func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadCB commands.DownloadProgressCB) (*rpc.LibraryDownloadResp, error) { @@ -36,13 +35,9 @@ func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadC logrus.Info("Preparing download") - var version *semver.Version - if req.GetVersion() != "" { - if v, err := semver.Parse(req.GetVersion()); err == nil { - version = v - } else { - return nil, fmt.Errorf("invalid version: %s", err) - } + version, err := commands.ParseVersion(req) + if err != nil { + return nil, fmt.Errorf("invalid version: %s", err) } ref := &librariesindex.Reference{Name: req.GetName(), Version: version} diff --git a/commands/lib/install.go b/commands/lib/install.go index 8761a5fd30f..9efab3c4999 100644 --- a/commands/lib/install.go +++ b/commands/lib/install.go @@ -25,21 +25,18 @@ import ( "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" "github.com/sirupsen/logrus" - semver "go.bug.st/relaxed-semver" ) func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error { lm := commands.GetLibraryManager(req) - var version *semver.Version - if req.GetVersion() != "" { - if v, err := semver.Parse(req.GetVersion()); err == nil { - version = v - } else { - return fmt.Errorf("invalid version: %s", err) - } + + version, err := commands.ParseVersion(req) + if err != nil { + return fmt.Errorf("invalid version: %s", err) } + ref := &librariesindex.Reference{Name: req.GetName(), Version: version} libRelease := lm.Index.FindRelease(ref) if libRelease == nil { diff --git a/commands/lib/uninstall.go b/commands/lib/uninstall.go index 7358fb5b24b..fd773c3454f 100644 --- a/commands/lib/uninstall.go +++ b/commands/lib/uninstall.go @@ -24,19 +24,16 @@ import ( "github.com/arduino/arduino-cli/arduino/libraries/librariesindex" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" - semver "go.bug.st/relaxed-semver" ) func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallReq, taskCB commands.TaskProgressCB) error { lm := commands.GetLibraryManager(req) - var version *semver.Version - if req.GetVersion() != "" { - if v, err := semver.Parse(req.GetVersion()); err == nil { - version = v - } else { - return fmt.Errorf("invalid version: %s", err) - } + + version, err := commands.ParseVersion(req) + if err != nil { + return fmt.Errorf("invalid version: %s", err) } + ref := &librariesindex.Reference{Name: req.GetName(), Version: version} lib := lm.FindByReference(ref) if lib == nil { diff --git a/commands/version.go b/commands/version.go new file mode 100644 index 00000000000..98eb71736fe --- /dev/null +++ b/commands/version.go @@ -0,0 +1,36 @@ +// +// This file is part of arduino-cli. +// +// Copyright 2018 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 commands + +import ( + semver "go.bug.st/relaxed-semver" +) + +// Versioned is an object that provides a GetVersion() method +type Versioned interface { + GetVersion() string +} + +// ParseVersion returns the version parsed from an interface that provides +// the GetVersion() method (interface Versioned) +func ParseVersion(req Versioned) (*semver.Version, error) { + if req.GetVersion() != "" { + return semver.Parse(req.GetVersion()) + } + return nil, nil +}