Skip to content

Commit

Permalink
Cleaned up all 'core' commands/cli modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Apr 16, 2019
1 parent c11c8bd commit f6b75c2
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 41 deletions.
15 changes: 7 additions & 8 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package core

import (
"context"
"os"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/output"
"github.com/arduino/arduino-cli/rpc"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -50,18 +52,15 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
platformsRefs := parsePlatformReferenceArgs(args)

for _, platformRef := range platformsRefs {
core.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{
_, err := core.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{
Instance: instance,
PlatformPackage: platformRef.Package,
Architecture: platformRef.PlatformArchitecture,
Version: platformRef.PlatformVersion.String(),
}, output.DownloadProgressBar(), output.NewTaskProgressCB())
// if err != nil {
// formatter.PrintError(err, "Error during build")
// os.Exit(cli.ErrGeneric)
// }
// }
if err != nil {
formatter.PrintError(err, "Error during install")
os.Exit(cli.ErrGeneric)
}
}

// TODO: Cleanup unused tools
}
5 changes: 4 additions & 1 deletion cli/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/output"
"github.com/arduino/arduino-cli/rpc"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -50,12 +51,14 @@ var listFlags struct {

func runListCommand(cmd *cobra.Command, args []string) {
instance := cli.CreateInstance()
logrus.Info("Executing `arduino core list`")

resp, err := core.PlatformList(context.Background(), &rpc.PlatformListReq{
Instance: instance,
UpdatableOnly: listFlags.updatableOnly,
})
if err != nil {
formatter.PrintError(err, "Error saerching for platforms")
formatter.PrintError(err, "Error listing platforms")
os.Exit(cli.ErrGeneric)
}
installed := resp.GetInstalledPlatform()
Expand Down
6 changes: 3 additions & 3 deletions cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"os"
"sort"

"strings"

"github.com/arduino/arduino-cli/cli"
Expand All @@ -47,11 +46,12 @@ func initSearchCommand() *cobra.Command {
}

func runSearchCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino core search`")
instance := cli.CreateInstance()
arguments := strings.ToLower(strings.Join(args, " "))
logrus.Info("Executing `arduino core search`")

arguments := strings.ToLower(strings.Join(args, " "))
formatter.Print("Searching for platforms matching '" + arguments + "'")

resp, err := core.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
Instance: instance,
SearchArgs: arguments,
Expand Down
2 changes: 1 addition & 1 deletion cli/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func initUninstallCommand() *cobra.Command {

func runUninstallCommand(cmd *cobra.Command, args []string) {
instance := cli.CreateInstance()
logrus.Info("Executing `arduino core download`")
logrus.Info("Executing `arduino core uninstall`")

platformsRefs := parsePlatformReferenceArgs(args)

Expand Down
12 changes: 10 additions & 2 deletions cli/core/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ package core

import (
"context"
"os"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/output"
"github.com/arduino/arduino-cli/rpc"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -41,8 +44,13 @@ func initUpdateIndexCommand() *cobra.Command {

func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
instance := cli.CreateInstance()
commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
logrus.Info("Executing `arduino core update-index`")

_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{
Instance: instance,
}, output.DownloadProgressBar())

if err != nil {
formatter.PrintError(err, "Error during install")
os.Exit(cli.ErrGeneric)
}
}
8 changes: 7 additions & 1 deletion cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package core

import (
"context"
"os"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/output"
"github.com/arduino/arduino-cli/rpc"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -49,11 +51,15 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {

platformsRefs := parsePlatformReferenceArgs(args)
for _, platformRef := range platformsRefs {
core.PlatformUpgrade(context.Background(), &rpc.PlatformUpgradeReq{
_, err := core.PlatformUpgrade(context.Background(), &rpc.PlatformUpgradeReq{
Instance: instance,
PlatformPackage: platformRef.Package,
Architecture: platformRef.PlatformArchitecture,
Version: platformRef.PlatformVersion.String(),
}, output.DownloadProgressBar(), output.NewTaskProgressCB())
if err != nil {
formatter.PrintError(err, "Error during upgrade")
os.Exit(cli.ErrGeneric)
}
}
}
10 changes: 5 additions & 5 deletions commands/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ import (
)

func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResp, error) {
pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

var version *semver.Version
if v, err := semver.Parse(req.Version); err == nil {
version = v
} else {
return nil, fmt.Errorf("invalid version: %s", err)
}

pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
Package: req.PlatformPackage,
PlatformArchitecture: req.Architecture,
Expand Down
33 changes: 24 additions & 9 deletions commands/core/install.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* 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 core

import (
Expand All @@ -8,13 +25,18 @@ import (
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/rpc"
semver "go.bug.st/relaxed-semver"
)

func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResp, error) {

pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

var version *semver.Version
if req.Version != "" {
if v, err := semver.Parse(req.Version); err == nil {
Expand All @@ -24,11 +46,6 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
}
}

pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
Package: req.PlatformPackage,
PlatformArchitecture: req.Architecture,
Expand Down Expand Up @@ -119,7 +136,6 @@ func installPlatform(pm *packagemanager.PackageManager,
if err := pm.UninstallPlatform(platformRelease); err != nil {
log.WithError(err).Error("Error rolling-back changes.")
taskCB(&rpc.TaskProgress{Message: "Error rolling-back changes: " + err.Error()})
//return fmt.Errorf("rolling-back changes: %s", err)
}

return fmt.Errorf("updating platform: %s", errUn)
Expand All @@ -146,8 +162,7 @@ func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.To
err := pm.InstallTool(toolRelease)
if err != nil {
log.WithError(err).Warn("Cannot install tool")
formatter.PrintError(err, "Cannot install tool: "+toolRelease.String())
return fmt.Errorf("Cannot install tool: "+toolRelease.String(), err)
return fmt.Errorf("installing tool %s: %s", toolRelease, err)
}
log.Info("Tool installed")
taskCB(&rpc.TaskProgress{Message: toolRelease.String() + " installed", Completed: true})
Expand Down
5 changes: 5 additions & 0 deletions commands/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ package core

import (
"context"
"errors"

"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/rpc"
)

func PlatformList(ctx context.Context, req *rpc.PlatformListReq) (*rpc.PlatformListResp, error) {
pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

installed := []*rpc.InstalledPlatform{}
for _, targetPackage := range pm.GetPackages().Packages {
for _, platform := range targetPackage.Platforms {
Expand Down
7 changes: 5 additions & 2 deletions commands/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ package core

import (
"context"
"errors"
"regexp"
"strings"

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/rpc"

"github.com/arduino/arduino-cli/arduino/cores"
)

func PlatformSearch(ctx context.Context, req *rpc.PlatformSearchReq) (*rpc.PlatformSearchResp, error) {
pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

search := req.SearchArgs

Expand Down
10 changes: 6 additions & 4 deletions commands/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ import (
)

func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallReq, taskCB commands.TaskProgressCB) (*rpc.PlatformUninstallResp, error) {
pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

// If no version is specified consider the installed
version, err := semver.Parse(req.Version)
if err != nil {
return nil, fmt.Errorf("invalid version: %s", err)
}
pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

ref := &packagemanager.PlatformReference{
Package: req.PlatformPackage,
PlatformArchitecture: req.Architecture,
Expand Down
10 changes: 6 additions & 4 deletions commands/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import (

func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq,
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformUpgradeResp, error) {

pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

// Extract all PlatformReference to platforms that have updates
var version *semver.Version
if req.Version != "" {
Expand All @@ -39,10 +45,6 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq,
return nil, fmt.Errorf("invalid version: %s", err)
}
}
pm := commands.GetPackageManager(req)
if pm == nil {
return nil, errors.New("invalid instance")
}

ref := &packagemanager.PlatformReference{
Package: req.PlatformPackage,
Expand Down
2 changes: 1 addition & 1 deletion commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ func UpdateLibrariesIndex(ctx context.Context, lm *librariesmanager.LibrariesMan
func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB DownloadProgressCB) (*rpc.UpdateIndexResp, error) {
id := req.Instance.Id
coreInstance, ok := instances[id]

if !ok {
return nil, fmt.Errorf("invalid handle")
}

indexpath := coreInstance.config.IndexesDir()
for _, URL := range coreInstance.config.BoardManagerAdditionalUrls {
logrus.WithField("url", URL).Print("Updating index")
Expand Down

0 comments on commit f6b75c2

Please sign in to comment.