Skip to content

Commit 63969a4

Browse files
committed
Moved core module under cli
1 parent 79aa5a5 commit 63969a4

File tree

11 files changed

+924
-1
lines changed

11 files changed

+924
-1
lines changed

cli/core/args.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 core
19+
20+
import (
21+
"fmt"
22+
"os"
23+
"strings"
24+
25+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
26+
"github.com/arduino/arduino-cli/cli"
27+
"github.com/arduino/arduino-cli/common/formatter"
28+
semver "go.bug.st/relaxed-semver"
29+
)
30+
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{}
34+
for _, arg := range args {
35+
reference, err := parsePlatformReferenceArg(arg)
36+
if err != nil {
37+
formatter.PrintError(err, "Invalid item "+arg)
38+
os.Exit(cli.ErrBadArgument)
39+
}
40+
ret = append(ret, reference)
41+
}
42+
return ret
43+
}
44+
45+
func parsePlatformReferenceArg(arg string) (*packagemanager.PlatformReference, error) {
46+
split := strings.SplitN(arg, "@", 2)
47+
arg = split[0]
48+
var version *semver.Version
49+
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+
}
56+
split = strings.Split(arg, ":")
57+
if len(split) != 2 {
58+
return nil, fmt.Errorf("invalid item %s", arg)
59+
}
60+
return &packagemanager.PlatformReference{
61+
Package: split[0],
62+
PlatformArchitecture: split[1],
63+
PlatformVersion: version,
64+
}, nil
65+
}

cli/core/args_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 core
19+
20+
import (
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
semver "go.bug.st/relaxed-semver"
25+
)
26+
27+
func TestParsePlatformReferenceArgs(t *testing.T) {
28+
valid := func(arg, pack, arch, ver string) {
29+
version, _ := semver.Parse(ver) // use nil in case of error
30+
31+
ref, err := parsePlatformReferenceArg(arg)
32+
require.NoError(t, err)
33+
require.Equal(t, pack, ref.Package)
34+
require.Equal(t, arch, ref.PlatformArchitecture)
35+
require.Equal(t, version, ref.PlatformVersion)
36+
}
37+
invalid := func(arg string) {
38+
_, err := parsePlatformReferenceArg(arg)
39+
require.Error(t, err)
40+
}
41+
valid("arduino:avr", "arduino", "avr", "-")
42+
valid("arduino:avr@1.6.20", "arduino", "avr", "1.6.20")
43+
valid("arduino:avr@", "arduino", "avr", "")
44+
invalid("avr")
45+
invalid("arduino:avr:avr")
46+
invalid("arduino@1.6.20:avr")
47+
invalid("avr@1.6.20")
48+
invalid("arduino:avr:avr@1.6.20")
49+
}

cli/core/core.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 core
19+
20+
import (
21+
"github.com/arduino/arduino-cli/cli"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// InitCommand prepares the command.
26+
func InitCommand() *cobra.Command {
27+
coreCommand := &cobra.Command{
28+
Use: "core",
29+
Short: "Arduino Core operations.",
30+
Long: "Arduino Core operations.",
31+
Example: " " + cli.AppName + " core update-index",
32+
}
33+
coreCommand.AddCommand(initDownloadCommand())
34+
coreCommand.AddCommand(initInstallCommand())
35+
coreCommand.AddCommand(initListCommand())
36+
coreCommand.AddCommand(initUpdateIndexCommand())
37+
coreCommand.AddCommand(initUpgradeCommand())
38+
coreCommand.AddCommand(initUninstallCommand())
39+
coreCommand.AddCommand(initSearchCommand())
40+
return coreCommand
41+
}

cli/core/download.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 core
19+
20+
import (
21+
"os"
22+
23+
"go.bug.st/downloader"
24+
25+
"github.com/arduino/arduino-cli/arduino/cores"
26+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
27+
"github.com/arduino/arduino-cli/cli"
28+
"github.com/arduino/arduino-cli/common/formatter"
29+
"github.com/sirupsen/logrus"
30+
"github.com/spf13/cobra"
31+
)
32+
33+
func initDownloadCommand() *cobra.Command {
34+
downloadCommand := &cobra.Command{
35+
Use: "download [PACKAGER:ARCH[=VERSION]](S)",
36+
Short: "Downloads one or more cores and corresponding tool dependencies.",
37+
Long: "Downloads one or more cores and corresponding tool dependencies.",
38+
Example: "" +
39+
" " + cli.AppName + " core download arduino:samd # to download the latest version of arduino SAMD core.\n" +
40+
" " + cli.AppName + " core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
41+
Args: cobra.MinimumNArgs(1),
42+
Run: runDownloadCommand,
43+
}
44+
return downloadCommand
45+
}
46+
47+
func runDownloadCommand(cmd *cobra.Command, args []string) {
48+
logrus.Info("Executing `arduino core download`")
49+
50+
platformsRefs := parsePlatformReferenceArgs(args)
51+
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
52+
for _, platformRef := range platformsRefs {
53+
downloadPlatformByRef(pm, platformRef)
54+
}
55+
}
56+
57+
func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *packagemanager.PlatformReference) {
58+
platform, tools, err := pm.FindPlatformReleaseDependencies(platformsRef)
59+
if err != nil {
60+
formatter.PrintError(err, "Could not determine platform dependencies")
61+
os.Exit(cli.ErrBadCall)
62+
}
63+
downloadPlatform(pm, platform)
64+
for _, tool := range tools {
65+
downloadTool(pm, tool)
66+
}
67+
}
68+
69+
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
70+
// Download platform
71+
resp, err := pm.DownloadPlatformRelease(platformRelease)
72+
download(resp, err, platformRelease.String())
73+
}
74+
75+
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease) {
76+
// Check if tool has a flavor available for the current OS
77+
if tool.GetCompatibleFlavour() == nil {
78+
formatter.PrintErrorMessage("The tool " + tool.String() + " is not available for the current OS")
79+
os.Exit(cli.ErrGeneric)
80+
}
81+
82+
DownloadToolRelease(pm, tool)
83+
}
84+
85+
// DownloadToolRelease downloads a ToolRelease
86+
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
87+
resp, err := pm.DownloadToolRelease(toolRelease)
88+
download(resp, err, toolRelease.String())
89+
}
90+
91+
func download(d *downloader.Downloader, err error, label string) {
92+
if err != nil {
93+
formatter.PrintError(err, "Error downloading "+label)
94+
os.Exit(cli.ErrNetwork)
95+
}
96+
if d == nil {
97+
formatter.Print(label + " already downloaded")
98+
return
99+
}
100+
formatter.Print("Downloading " + label + "...")
101+
formatter.DownloadProgressBar(d, label)
102+
if d.Error() != nil {
103+
formatter.PrintError(d.Error(), "Error downloading "+label)
104+
os.Exit(cli.ErrNetwork)
105+
}
106+
}

0 commit comments

Comments
 (0)