Skip to content

Commit d880e28

Browse files
committed
Added helper function to extract parsed version from RPC requests
1 parent d53d3c2 commit d880e28

File tree

14 files changed

+91
-82
lines changed

14 files changed

+91
-82
lines changed

cli/core/args.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ import (
2222
"os"
2323
"strings"
2424

25-
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2625
"github.com/arduino/arduino-cli/cli"
2726
"github.com/arduino/arduino-cli/common/formatter"
28-
semver "go.bug.st/relaxed-semver"
2927
)
3028

31-
// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and returns a platformReference slice.
32-
func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReference {
33-
ret := []*packagemanager.PlatformReference{}
29+
type platformReferenceArg struct {
30+
Package string
31+
Architecture string
32+
Version string
33+
}
34+
35+
// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and
36+
// returns a platformReferenceArg slice.
37+
func parsePlatformReferenceArgs(args []string) []*platformReferenceArg {
38+
ret := []*platformReferenceArg{}
3439
for _, arg := range args {
3540
reference, err := parsePlatformReferenceArg(arg)
3641
if err != nil {
@@ -42,24 +47,21 @@ func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReferen
4247
return ret
4348
}
4449

45-
func parsePlatformReferenceArg(arg string) (*packagemanager.PlatformReference, error) {
50+
func parsePlatformReferenceArg(arg string) (*platformReferenceArg, error) {
4651
split := strings.SplitN(arg, "@", 2)
4752
arg = split[0]
48-
var version *semver.Version
53+
version := ""
4954
if len(split) > 1 {
50-
if ver, err := semver.Parse(split[1]); err == nil {
51-
version = ver
52-
} else {
53-
return nil, fmt.Errorf("invalid version: %s", err)
54-
}
55+
version = split[1]
5556
}
57+
5658
split = strings.Split(arg, ":")
5759
if len(split) != 2 {
5860
return nil, fmt.Errorf("invalid item %s", arg)
5961
}
60-
return &packagemanager.PlatformReference{
61-
Package: split[0],
62-
PlatformArchitecture: split[1],
63-
PlatformVersion: version,
62+
return &platformReferenceArg{
63+
Package: split[0],
64+
Architecture: split[1],
65+
Version: version,
6466
}, nil
6567
}

cli/core/args_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestParsePlatformReferenceArgs(t *testing.T) {
3131
ref, err := parsePlatformReferenceArg(arg)
3232
require.NoError(t, err)
3333
require.Equal(t, pack, ref.Package)
34-
require.Equal(t, arch, ref.PlatformArchitecture)
35-
require.Equal(t, version, ref.PlatformVersion)
34+
require.Equal(t, arch, ref.Architecture)
35+
require.Equal(t, version.String(), ref.Version)
3636
}
3737
invalid := func(arg string) {
3838
_, err := parsePlatformReferenceArg(arg)

cli/core/download.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
4848
logrus.Info("Executing `arduino core download`")
4949

5050
platformsRefs := parsePlatformReferenceArgs(args)
51-
for _, platformRef := range platformsRefs {
51+
for i, platformRef := range platformsRefs {
5252
_, err := core.PlatformDownload(context.Background(), &rpc.PlatformDownloadReq{
5353
Instance: instance,
5454
PlatformPackage: platformRef.Package,
55-
Architecture: platformRef.PlatformArchitecture,
56-
Version: platformRef.PlatformVersion.String(),
55+
Architecture: platformRef.Architecture,
56+
Version: platformRef.Version,
5757
}, cli.OutputProgressBar())
5858
if err != nil {
59-
formatter.PrintError(err, "Error downloading "+platformRef.String())
59+
formatter.PrintError(err, "Error downloading "+args[i])
6060
os.Exit(cli.ErrNetwork)
6161
}
6262
}

cli/core/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
5454
_, err := core.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{
5555
Instance: instance,
5656
PlatformPackage: platformRef.Package,
57-
Architecture: platformRef.PlatformArchitecture,
58-
Version: platformRef.PlatformVersion.String(),
57+
Architecture: platformRef.Architecture,
58+
Version: platformRef.Version,
5959
}, cli.OutputProgressBar(), cli.OutputTaskProgress())
6060
if err != nil {
6161
formatter.PrintError(err, "Error during install")

cli/core/uninstall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
5151
_, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
5252
Instance: instance,
5353
PlatformPackage: platformRef.Package,
54-
Architecture: platformRef.PlatformArchitecture,
55-
Version: platformRef.PlatformVersion.String(),
54+
Architecture: platformRef.Architecture,
55+
Version: platformRef.Version,
5656
}, output.NewTaskProgressCB())
5757
if err != nil {
5858
formatter.PrintError(err, "Error during uninstall")

cli/core/upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
5353
_, err := core.PlatformUpgrade(context.Background(), &rpc.PlatformUpgradeReq{
5454
Instance: instance,
5555
PlatformPackage: platformRef.Package,
56-
Architecture: platformRef.PlatformArchitecture,
57-
Version: platformRef.PlatformVersion.String(),
56+
Architecture: platformRef.Architecture,
57+
Version: platformRef.Version,
5858
}, cli.OutputProgressBar(), cli.OutputTaskProgress())
5959
if err != nil {
6060
formatter.PrintError(err, "Error during upgrade")

commands/core/download.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2727
"github.com/arduino/arduino-cli/commands"
2828
"github.com/arduino/arduino-cli/rpc"
29-
semver "go.bug.st/relaxed-semver"
3029
)
3130

3231
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
3534
return nil, errors.New("invalid instance")
3635
}
3736

38-
var version *semver.Version
39-
if req.GetVersion() != "" {
40-
if v, err := semver.Parse(req.GetVersion()); err == nil {
41-
version = v
42-
} else {
43-
return nil, fmt.Errorf("invalid version: %s", err)
44-
}
37+
version, err := commands.ParseVersion(req)
38+
if err != nil {
39+
return nil, fmt.Errorf("invalid version: %s", err)
4540
}
4641

4742
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{

commands/core/install.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2727
"github.com/arduino/arduino-cli/commands"
2828
"github.com/arduino/arduino-cli/rpc"
29-
semver "go.bug.st/relaxed-semver"
3029
)
3130

3231
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
@@ -37,13 +36,9 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
3736
return nil, errors.New("invalid instance")
3837
}
3938

40-
var version *semver.Version
41-
if req.GetVersion() != "" {
42-
if v, err := semver.Parse(req.GetVersion()); err == nil {
43-
version = v
44-
} else {
45-
return nil, fmt.Errorf("invalid version: %s", err)
46-
}
39+
version, err := commands.ParseVersion(req)
40+
if err != nil {
41+
return nil, fmt.Errorf("invalid version: %s", err)
4742
}
4843

4944
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{

commands/core/uninstall.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2727
"github.com/arduino/arduino-cli/commands"
2828
"github.com/arduino/arduino-cli/rpc"
29-
semver "go.bug.st/relaxed-semver"
3029
)
3130

3231
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
3635
}
3736

3837
// If no version is specified consider the installed
39-
version, err := semver.Parse(req.Version)
38+
version, err := commands.ParseVersion(req)
4039
if err != nil {
4140
return nil, fmt.Errorf("invalid version: %s", err)
4241
}

commands/core/upgrade.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2626
"github.com/arduino/arduino-cli/commands"
2727
"github.com/arduino/arduino-cli/rpc"
28-
semver "go.bug.st/relaxed-semver"
2928
)
3029

3130
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq,
@@ -37,26 +36,20 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq,
3736
}
3837

3938
// Extract all PlatformReference to platforms that have updates
40-
var version *semver.Version
41-
if req.Version != "" {
42-
if v, err := semver.Parse(req.Version); err == nil {
43-
version = v
44-
} else {
45-
return nil, fmt.Errorf("invalid version: %s", err)
46-
}
39+
version, err := commands.ParseVersion(req)
40+
if err != nil {
41+
return nil, fmt.Errorf("invalid version: %s", err)
4742
}
4843

4944
ref := &packagemanager.PlatformReference{
5045
Package: req.PlatformPackage,
5146
PlatformArchitecture: req.Architecture,
5247
PlatformVersion: version}
53-
err := upgradePlatform(pm, ref, downloadCB, taskCB)
54-
if err != nil {
48+
if err := upgradePlatform(pm, ref, downloadCB, taskCB); err != nil {
5549
return nil, err
5650
}
5751

58-
_, err = commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance})
59-
if err != nil {
52+
if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
6053
return nil, err
6154
}
6255

commands/lib/download.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/arduino/arduino-cli/commands"
2727
"github.com/arduino/arduino-cli/rpc"
2828
"github.com/sirupsen/logrus"
29-
semver "go.bug.st/relaxed-semver"
3029
)
3130

3231
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
3635

3736
logrus.Info("Preparing download")
3837

39-
var version *semver.Version
40-
if req.GetVersion() != "" {
41-
if v, err := semver.Parse(req.GetVersion()); err == nil {
42-
version = v
43-
} else {
44-
return nil, fmt.Errorf("invalid version: %s", err)
45-
}
38+
version, err := commands.ParseVersion(req)
39+
if err != nil {
40+
return nil, fmt.Errorf("invalid version: %s", err)
4641
}
4742

4843
ref := &librariesindex.Reference{Name: req.GetName(), Version: version}

commands/lib/install.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,18 @@ import (
2525
"github.com/arduino/arduino-cli/commands"
2626
"github.com/arduino/arduino-cli/rpc"
2727
"github.com/sirupsen/logrus"
28-
semver "go.bug.st/relaxed-semver"
2928
)
3029

3130
func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq,
3231
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
3332

3433
lm := commands.GetLibraryManager(req)
35-
var version *semver.Version
36-
if req.GetVersion() != "" {
37-
if v, err := semver.Parse(req.GetVersion()); err == nil {
38-
version = v
39-
} else {
40-
return fmt.Errorf("invalid version: %s", err)
41-
}
34+
35+
version, err := commands.ParseVersion(req)
36+
if err != nil {
37+
return fmt.Errorf("invalid version: %s", err)
4238
}
39+
4340
ref := &librariesindex.Reference{Name: req.GetName(), Version: version}
4441
libRelease := lm.Index.FindRelease(ref)
4542
if libRelease == nil {

commands/lib/uninstall.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@ import (
2424
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2525
"github.com/arduino/arduino-cli/commands"
2626
"github.com/arduino/arduino-cli/rpc"
27-
semver "go.bug.st/relaxed-semver"
2827
)
2928

3029
func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallReq, taskCB commands.TaskProgressCB) error {
3130
lm := commands.GetLibraryManager(req)
32-
var version *semver.Version
33-
if req.GetVersion() != "" {
34-
if v, err := semver.Parse(req.GetVersion()); err == nil {
35-
version = v
36-
} else {
37-
return fmt.Errorf("invalid version: %s", err)
38-
}
31+
32+
version, err := commands.ParseVersion(req)
33+
if err != nil {
34+
return fmt.Errorf("invalid version: %s", err)
3935
}
36+
4037
ref := &librariesindex.Reference{Name: req.GetName(), Version: version}
4138
lib := lm.FindByReference(ref)
4239
if lib == nil {

commands/version.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// This file is part of arduino-cli.
3+
//
4+
// Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
//
6+
// This software is released under the GNU General Public License version 3,
7+
// which covers the main part of arduino-cli.
8+
// The terms of this license can be found at:
9+
// https://www.gnu.org/licenses/gpl-3.0.en.html
10+
//
11+
// You can be released from the requirements of the above licenses by purchasing
12+
// a commercial license. Buying such a license is mandatory if you want to modify or
13+
// otherwise use the software for commercial activities involving the Arduino
14+
// software without disclosing the source code of your own applications. To purchase
15+
// a commercial license, send an email to license@arduino.cc.
16+
//
17+
18+
package commands
19+
20+
import (
21+
semver "go.bug.st/relaxed-semver"
22+
)
23+
24+
// Versioned is an object that provides a GetVersion() method
25+
type Versioned interface {
26+
GetVersion() string
27+
}
28+
29+
// ParseVersion returns the version parsed from an interface that provides
30+
// the GetVersion() method (interface Versioned)
31+
func ParseVersion(req Versioned) (*semver.Version, error) {
32+
if req.GetVersion() != "" {
33+
return semver.Parse(req.GetVersion())
34+
}
35+
return nil, nil
36+
}

0 commit comments

Comments
 (0)