Skip to content

Commit 633e9ab

Browse files
authored
Merge pull request #298 from arduino/massi/lib-install
Make lib install exit with 0 if library is already present
2 parents 620a4c1 + 7aecdba commit 633e9ab

File tree

16 files changed

+347
-245
lines changed

16 files changed

+347
-245
lines changed

Diff for: arduino/libraries/librariesindex/reference.go

-23
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
package librariesindex
1919

2020
import (
21-
"fmt"
22-
"strings"
23-
2421
semver "go.bug.st/relaxed-semver"
2522
)
2623

@@ -36,23 +33,3 @@ func (r *Reference) String() string {
3633
}
3734
return r.Name + "@" + r.Version.String()
3835
}
39-
40-
// ParseArgs parses a sequence of "item@version" tokens and returns a Name-Version slice.
41-
//
42-
// If version is not present it is assumed as "latest" version.
43-
func ParseArgs(args []string) ([]*Reference, error) {
44-
res := []*Reference{}
45-
for _, item := range args {
46-
tokens := strings.SplitN(item, "@", 2)
47-
if len(tokens) == 2 {
48-
version, err := semver.Parse(tokens[1])
49-
if err != nil {
50-
return nil, fmt.Errorf("invalid version %s: %s", version, err)
51-
}
52-
res = append(res, &Reference{Name: tokens[0], Version: version})
53-
} else {
54-
res = append(res, &Reference{Name: tokens[0]})
55-
}
56-
}
57-
return res, nil
58-
}

Diff for: arduino/libraries/librariesmanager/install.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package librariesmanager
1919

2020
import (
21+
"errors"
2122
"fmt"
2223

2324
"github.com/arduino/arduino-cli/arduino/libraries"
@@ -26,18 +27,26 @@ import (
2627
paths "github.com/arduino/go-paths-helper"
2728
)
2829

30+
var (
31+
// ErrAlreadyInstalled is returned when a library is already installed and task
32+
// cannot proceed.
33+
ErrAlreadyInstalled = errors.New("library already installed")
34+
)
35+
2936
// InstallPrerequisiteCheck performs prequisite checks to install a library. It returns the
3037
// install path, where the library should be installed and the possible library that is already
3138
// installed on the same folder and it's going to be replaced by the new one.
3239
func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesindex.Release) (*paths.Path, *libraries.Library, error) {
40+
saneName := utils.SanitizeName(indexLibrary.Library.Name)
41+
3342
var replaced *libraries.Library
34-
if installedLibs, have := lm.Libraries[indexLibrary.Library.Name]; have {
43+
if installedLibs, have := lm.Libraries[saneName]; have {
3544
for _, installedLib := range installedLibs.Alternatives {
3645
if installedLib.Location != libraries.Sketchbook {
3746
continue
3847
}
3948
if installedLib.Version.Equal(indexLibrary.Version) {
40-
return installedLib.InstallDir, nil, fmt.Errorf("%s is already installed", indexLibrary.String())
49+
return installedLib.InstallDir, nil, ErrAlreadyInstalled
4150
}
4251
replaced = installedLib
4352
}
@@ -48,7 +57,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
4857
return nil, nil, fmt.Errorf("sketchbook directory not set")
4958
}
5059

51-
libPath := libsDir.Join(utils.SanitizeName(indexLibrary.Library.Name))
60+
libPath := libsDir.Join(saneName)
5261
if replaced != nil && replaced.InstallDir.EquivalentTo(libPath) {
5362

5463
} else if libPath.IsDir() {

Diff for: cli/core/args.go

-74
This file was deleted.

Diff for: cli/core/args_test.go

-49
This file was deleted.

Diff for: cli/core/download.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
5050
instance := instance.CreateInstance()
5151
logrus.Info("Executing `arduino core download`")
5252

53-
platformsRefs := parsePlatformReferenceArgs(args)
53+
platformsRefs, err := globals.ParseReferenceArgs(args, true)
54+
if err != nil {
55+
formatter.PrintError(err, "Invalid argument passed")
56+
os.Exit(errorcodes.ErrBadArgument)
57+
}
58+
5459
for i, platformRef := range platformsRefs {
5560
platformDownloadreq := &rpc.PlatformDownloadReq{
5661
Instance: instance,
57-
PlatformPackage: platformRef.Package,
62+
PlatformPackage: platformRef.PackageName,
5863
Architecture: platformRef.Architecture,
5964
Version: platformRef.Version,
6065
}

Diff for: cli/core/install.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
5151
instance := instance.CreateInstance()
5252
logrus.Info("Executing `arduino core install`")
5353

54-
platformsRefs := parsePlatformReferenceArgs(args)
54+
platformsRefs, err := globals.ParseReferenceArgs(args, true)
55+
if err != nil {
56+
formatter.PrintError(err, "Invalid argument passed")
57+
os.Exit(errorcodes.ErrBadArgument)
58+
}
5559

5660
for _, platformRef := range platformsRefs {
5761
plattformInstallReq := &rpc.PlatformInstallReq{
5862
Instance: instance,
59-
PlatformPackage: platformRef.Package,
63+
PlatformPackage: platformRef.PackageName,
6064
Architecture: platformRef.Architecture,
6165
Version: platformRef.Version,
6266
}

Diff for: cli/core/uninstall.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323

2424
"github.com/arduino/arduino-cli/cli/errorcodes"
25+
"github.com/arduino/arduino-cli/cli/globals"
2526
"github.com/arduino/arduino-cli/cli/instance"
2627
"github.com/arduino/arduino-cli/cli/output"
2728
"github.com/arduino/arduino-cli/commands/core"
@@ -46,7 +47,11 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
4647
instance := instance.CreateInstance()
4748
logrus.Info("Executing `arduino core uninstall`")
4849

49-
platformsRefs := parsePlatformReferenceArgs(args)
50+
platformsRefs, err := globals.ParseReferenceArgs(args, true)
51+
if err != nil {
52+
formatter.PrintError(err, "Invalid argument passed")
53+
os.Exit(errorcodes.ErrBadArgument)
54+
}
5055

5156
for _, platformRef := range platformsRefs {
5257
if platformRef.Version != "" {
@@ -57,7 +62,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
5762
for _, platformRef := range platformsRefs {
5863
_, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
5964
Instance: instance,
60-
PlatformPackage: platformRef.Package,
65+
PlatformPackage: platformRef.PackageName,
6166
Architecture: platformRef.Architecture,
6267
}, output.NewTaskProgressCB())
6368
if err != nil {

Diff for: cli/core/upgrade.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
7272

7373
// proceed upgrading, if anything is upgradable
7474
exitErr := false
75-
platformsRefs := parsePlatformReferenceArgs(args)
75+
platformsRefs, err := globals.ParseReferenceArgs(args, true)
76+
if err != nil {
77+
formatter.PrintError(err, "Invalid argument passed")
78+
os.Exit(errorcodes.ErrBadArgument)
79+
}
80+
7681
for i, platformRef := range platformsRefs {
7782
if platformRef.Version != "" {
7883
formatter.PrintErrorMessage(("Invalid item " + args[i]))
@@ -82,7 +87,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
8287

8388
r := &rpc.PlatformUpgradeReq{
8489
Instance: instance,
85-
PlatformPackage: platformRef.Package,
90+
PlatformPackage: platformRef.PackageName,
8691
Architecture: platformRef.Architecture,
8792
}
8893

Diff for: cli/globals/args.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 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 modify or
12+
// otherwise use the software for commercial activities involving the Arduino
13+
// software without disclosing the source code of your own applications. To purchase
14+
// a commercial license, send an email to [email protected].
15+
16+
package globals
17+
18+
import (
19+
"fmt"
20+
"strings"
21+
)
22+
23+
// ReferenceArg represents a reference item (core or library) passed to the CLI
24+
// interface
25+
type ReferenceArg struct {
26+
PackageName string
27+
Architecture string
28+
Version string
29+
}
30+
31+
func (r *ReferenceArg) String() string {
32+
if r.Version != "" {
33+
return r.PackageName + ":" + r.Architecture + "@" + r.Version
34+
}
35+
return r.PackageName + ":" + r.Architecture
36+
}
37+
38+
// ParseReferenceArgs is a convenient wrapper that operates on a slice of strings and
39+
// calls ParseReferenceArg for each of them. It returns at the first invalid argument.
40+
func ParseReferenceArgs(args []string, parseArch bool) ([]*ReferenceArg, error) {
41+
ret := []*ReferenceArg{}
42+
for _, arg := range args {
43+
reference, err := ParseReferenceArg(arg, parseArch)
44+
if err != nil {
45+
return nil, err
46+
}
47+
ret = append(ret, reference)
48+
}
49+
return ret, nil
50+
}
51+
52+
// ParseReferenceArg parses a string and return a ReferenceArg object. If `parseArch` is passed,
53+
// the method also tries to parse the architecture bit, i.e. string must be in the form
54+
// "packager:arch@version", useful to represent a platform (or core) name.
55+
func ParseReferenceArg(arg string, parseArch bool) (*ReferenceArg, error) {
56+
ret := &ReferenceArg{}
57+
58+
toks := strings.SplitN(arg, "@", 2)
59+
ret.PackageName = toks[0]
60+
if len(toks) > 1 {
61+
ret.Version = toks[1]
62+
}
63+
64+
if parseArch {
65+
toks = strings.Split(ret.PackageName, ":")
66+
if len(toks) != 2 {
67+
return nil, fmt.Errorf("invalid item %s", arg)
68+
}
69+
ret.PackageName = toks[0]
70+
ret.Architecture = toks[1]
71+
}
72+
73+
return ret, nil
74+
}

0 commit comments

Comments
 (0)