Skip to content

Make lib install exit with 0 if library is already present #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions arduino/libraries/librariesindex/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
package librariesindex

import (
"fmt"
"strings"

semver "go.bug.st/relaxed-semver"
)

Expand All @@ -36,23 +33,3 @@ func (r *Reference) String() string {
}
return r.Name + "@" + r.Version.String()
}

// ParseArgs parses a sequence of "item@version" tokens and returns a Name-Version slice.
//
// If version is not present it is assumed as "latest" version.
func ParseArgs(args []string) ([]*Reference, error) {
res := []*Reference{}
for _, item := range args {
tokens := strings.SplitN(item, "@", 2)
if len(tokens) == 2 {
version, err := semver.Parse(tokens[1])
if err != nil {
return nil, fmt.Errorf("invalid version %s: %s", version, err)
}
res = append(res, &Reference{Name: tokens[0], Version: version})
} else {
res = append(res, &Reference{Name: tokens[0]})
}
}
return res, nil
}
15 changes: 12 additions & 3 deletions arduino/libraries/librariesmanager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package librariesmanager

import (
"errors"
"fmt"

"github.com/arduino/arduino-cli/arduino/libraries"
Expand All @@ -26,18 +27,26 @@ import (
paths "github.com/arduino/go-paths-helper"
)

var (
// ErrAlreadyInstalled is returned when a library is already installed and task
// cannot proceed.
ErrAlreadyInstalled = errors.New("library already installed")
)

// InstallPrerequisiteCheck performs prequisite checks to install a library. It returns the
// install path, where the library should be installed and the possible library that is already
// installed on the same folder and it's going to be replaced by the new one.
func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesindex.Release) (*paths.Path, *libraries.Library, error) {
saneName := utils.SanitizeName(indexLibrary.Library.Name)

var replaced *libraries.Library
if installedLibs, have := lm.Libraries[indexLibrary.Library.Name]; have {
if installedLibs, have := lm.Libraries[saneName]; have {
for _, installedLib := range installedLibs.Alternatives {
if installedLib.Location != libraries.Sketchbook {
continue
}
if installedLib.Version.Equal(indexLibrary.Version) {
return installedLib.InstallDir, nil, fmt.Errorf("%s is already installed", indexLibrary.String())
return installedLib.InstallDir, nil, ErrAlreadyInstalled
}
replaced = installedLib
}
Expand All @@ -48,7 +57,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
return nil, nil, fmt.Errorf("sketchbook directory not set")
}

libPath := libsDir.Join(utils.SanitizeName(indexLibrary.Library.Name))
libPath := libsDir.Join(saneName)
if replaced != nil && replaced.InstallDir.EquivalentTo(libPath) {

} else if libPath.IsDir() {
Expand Down
74 changes: 0 additions & 74 deletions cli/core/args.go

This file was deleted.

49 changes: 0 additions & 49 deletions cli/core/args_test.go

This file was deleted.

9 changes: 7 additions & 2 deletions cli/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance()
logrus.Info("Executing `arduino core download`")

platformsRefs := parsePlatformReferenceArgs(args)
platformsRefs, err := globals.ParseReferenceArgs(args, true)
if err != nil {
formatter.PrintError(err, "Invalid argument passed")
os.Exit(errorcodes.ErrBadArgument)
}

for i, platformRef := range platformsRefs {
platformDownloadreq := &rpc.PlatformDownloadReq{
Instance: instance,
PlatformPackage: platformRef.Package,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
Version: platformRef.Version,
}
Expand Down
8 changes: 6 additions & 2 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstance()
logrus.Info("Executing `arduino core install`")

platformsRefs := parsePlatformReferenceArgs(args)
platformsRefs, err := globals.ParseReferenceArgs(args, true)
if err != nil {
formatter.PrintError(err, "Invalid argument passed")
os.Exit(errorcodes.ErrBadArgument)
}

for _, platformRef := range platformsRefs {
plattformInstallReq := &rpc.PlatformInstallReq{
Instance: instance,
PlatformPackage: platformRef.Package,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
Version: platformRef.Version,
}
Expand Down
9 changes: 7 additions & 2 deletions cli/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"

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

platformsRefs := parsePlatformReferenceArgs(args)
platformsRefs, err := globals.ParseReferenceArgs(args, true)
if err != nil {
formatter.PrintError(err, "Invalid argument passed")
os.Exit(errorcodes.ErrBadArgument)
}

for _, platformRef := range platformsRefs {
if platformRef.Version != "" {
Expand All @@ -57,7 +62,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
for _, platformRef := range platformsRefs {
_, err := core.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
Instance: instance,
PlatformPackage: platformRef.Package,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
}, output.NewTaskProgressCB())
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {

// proceed upgrading, if anything is upgradable
exitErr := false
platformsRefs := parsePlatformReferenceArgs(args)
platformsRefs, err := globals.ParseReferenceArgs(args, true)
if err != nil {
formatter.PrintError(err, "Invalid argument passed")
os.Exit(errorcodes.ErrBadArgument)
}

for i, platformRef := range platformsRefs {
if platformRef.Version != "" {
formatter.PrintErrorMessage(("Invalid item " + args[i]))
Expand All @@ -82,7 +87,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {

r := &rpc.PlatformUpgradeReq{
Instance: instance,
PlatformPackage: platformRef.Package,
PlatformPackage: platformRef.PackageName,
Architecture: platformRef.Architecture,
}

Expand Down
74 changes: 74 additions & 0 deletions cli/globals/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// This file is part of arduino-cli.
//
// Copyright 2019 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 globals

import (
"fmt"
"strings"
)

// ReferenceArg represents a reference item (core or library) passed to the CLI
// interface
type ReferenceArg struct {
PackageName string
Architecture string
Version string
}

func (r *ReferenceArg) String() string {
if r.Version != "" {
return r.PackageName + ":" + r.Architecture + "@" + r.Version
}
return r.PackageName + ":" + r.Architecture
}

// ParseReferenceArgs is a convenient wrapper that operates on a slice of strings and
// calls ParseReferenceArg for each of them. It returns at the first invalid argument.
func ParseReferenceArgs(args []string, parseArch bool) ([]*ReferenceArg, error) {
ret := []*ReferenceArg{}
for _, arg := range args {
reference, err := ParseReferenceArg(arg, parseArch)
if err != nil {
return nil, err
}
ret = append(ret, reference)
}
return ret, nil
}

// ParseReferenceArg parses a string and return a ReferenceArg object. If `parseArch` is passed,
// the method also tries to parse the architecture bit, i.e. string must be in the form
// "packager:arch@version", useful to represent a platform (or core) name.
func ParseReferenceArg(arg string, parseArch bool) (*ReferenceArg, error) {
ret := &ReferenceArg{}

toks := strings.SplitN(arg, "@", 2)
ret.PackageName = toks[0]
if len(toks) > 1 {
ret.Version = toks[1]
}

if parseArch {
toks = strings.Split(ret.PackageName, ":")
if len(toks) != 2 {
return nil, fmt.Errorf("invalid item %s", arg)
}
ret.PackageName = toks[0]
ret.Architecture = toks[1]
}

return ret, nil
}
Loading