Skip to content

Commit

Permalink
Moved core module under cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocketct committed Apr 1, 2019
1 parent 79aa5a5 commit 63969a4
Show file tree
Hide file tree
Showing 11 changed files with 924 additions and 1 deletion.
65 changes: 65 additions & 0 deletions cli/core/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 (
"fmt"
"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{}
for _, arg := range args {
reference, err := parsePlatformReferenceArg(arg)
if err != nil {
formatter.PrintError(err, "Invalid item "+arg)
os.Exit(cli.ErrBadArgument)
}
ret = append(ret, reference)
}
return ret
}

func parsePlatformReferenceArg(arg string) (*packagemanager.PlatformReference, error) {
split := strings.SplitN(arg, "@", 2)
arg = split[0]
var version *semver.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)
}
}
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,
}, nil
}
49 changes: 49 additions & 0 deletions cli/core/args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 (
"testing"

"github.com/stretchr/testify/require"
semver "go.bug.st/relaxed-semver"
)

func TestParsePlatformReferenceArgs(t *testing.T) {
valid := func(arg, pack, arch, ver string) {
version, _ := semver.Parse(ver) // use nil in case of error

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)
}
invalid := func(arg string) {
_, err := parsePlatformReferenceArg(arg)
require.Error(t, err)
}
valid("arduino:avr", "arduino", "avr", "-")
valid("arduino:[email protected]", "arduino", "avr", "1.6.20")
valid("arduino:avr@", "arduino", "avr", "")
invalid("avr")
invalid("arduino:avr:avr")
invalid("[email protected]:avr")
invalid("[email protected]")
invalid("arduino:avr:[email protected]")
}
41 changes: 41 additions & 0 deletions cli/core/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 (
"github.com/arduino/arduino-cli/cli"
"github.com/spf13/cobra"
)

// InitCommand prepares the command.
func InitCommand() *cobra.Command {
coreCommand := &cobra.Command{
Use: "core",
Short: "Arduino Core operations.",
Long: "Arduino Core operations.",
Example: " " + cli.AppName + " core update-index",
}
coreCommand.AddCommand(initDownloadCommand())
coreCommand.AddCommand(initInstallCommand())
coreCommand.AddCommand(initListCommand())
coreCommand.AddCommand(initUpdateIndexCommand())
coreCommand.AddCommand(initUpgradeCommand())
coreCommand.AddCommand(initUninstallCommand())
coreCommand.AddCommand(initSearchCommand())
return coreCommand
}
106 changes: 106 additions & 0 deletions cli/core/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 (
"os"

"go.bug.st/downloader"

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func initDownloadCommand() *cobra.Command {
downloadCommand := &cobra.Command{
Use: "download [PACKAGER:ARCH[=VERSION]](S)",
Short: "Downloads one or more cores and corresponding tool dependencies.",
Long: "Downloads one or more cores and corresponding tool dependencies.",
Example: "" +
" " + cli.AppName + " core download arduino:samd # to download the latest version of arduino SAMD core.\n" +
" " + cli.AppName + " core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
Args: cobra.MinimumNArgs(1),
Run: runDownloadCommand,
}
return downloadCommand
}

func runDownloadCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino core download`")

platformsRefs := parsePlatformReferenceArgs(args)
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
for _, platformRef := range platformsRefs {
downloadPlatformByRef(pm, platformRef)
}
}

func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *packagemanager.PlatformReference) {
platform, tools, err := pm.FindPlatformReleaseDependencies(platformsRef)
if err != nil {
formatter.PrintError(err, "Could not determine platform dependencies")
os.Exit(cli.ErrBadCall)
}
downloadPlatform(pm, platform)
for _, tool := range tools {
downloadTool(pm, tool)
}
}

func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
// Download platform
resp, err := pm.DownloadPlatformRelease(platformRelease)
download(resp, err, platformRelease.String())
}

func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease) {
// Check if tool has a flavor available for the current OS
if tool.GetCompatibleFlavour() == nil {
formatter.PrintErrorMessage("The tool " + tool.String() + " is not available for the current OS")
os.Exit(cli.ErrGeneric)
}

DownloadToolRelease(pm, tool)
}

// DownloadToolRelease downloads a ToolRelease
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
resp, err := pm.DownloadToolRelease(toolRelease)
download(resp, err, toolRelease.String())
}

func download(d *downloader.Downloader, err error, label string) {
if err != nil {
formatter.PrintError(err, "Error downloading "+label)
os.Exit(cli.ErrNetwork)
}
if d == nil {
formatter.Print(label + " already downloaded")
return
}
formatter.Print("Downloading " + label + "...")
formatter.DownloadProgressBar(d, label)
if d.Error() != nil {
formatter.PrintError(d.Error(), "Error downloading "+label)
os.Exit(cli.ErrNetwork)
}
}
Loading

0 comments on commit 63969a4

Please sign in to comment.