Skip to content

Commit

Permalink
Added helper function to extract parsed version from RPC requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Apr 24, 2019
1 parent d53d3c2 commit d880e28
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 82 deletions.
34 changes: 18 additions & 16 deletions cli/core/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions cli/core/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions cli/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions cli/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 3 additions & 8 deletions commands/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand Down
11 changes: 3 additions & 8 deletions commands/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions commands/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
17 changes: 5 additions & 12 deletions commands/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
11 changes: 3 additions & 8 deletions commands/lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}
Expand Down
13 changes: 5 additions & 8 deletions commands/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 5 additions & 8 deletions commands/lib/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions commands/version.go
Original file line number Diff line number Diff line change
@@ -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 [email protected].
//

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
}

0 comments on commit d880e28

Please sign in to comment.