Skip to content

Commit 1d3db71

Browse files
committed
Add outdated command to list cores and libs that needs upgrade
1 parent 3775f51 commit 1d3db71

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

cli/cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/arduino/arduino-cli/cli/generatedocs"
3636
"github.com/arduino/arduino-cli/cli/globals"
3737
"github.com/arduino/arduino-cli/cli/lib"
38+
"github.com/arduino/arduino-cli/cli/outdated"
3839
"github.com/arduino/arduino-cli/cli/output"
3940
"github.com/arduino/arduino-cli/cli/sketch"
4041
"github.com/arduino/arduino-cli/cli/upload"
@@ -85,6 +86,7 @@ func createCliCommandTree(cmd *cobra.Command) {
8586
cmd.AddCommand(daemon.NewCommand())
8687
cmd.AddCommand(generatedocs.NewCommand())
8788
cmd.AddCommand(lib.NewCommand())
89+
cmd.AddCommand(outdated.NewCommand())
8890
cmd.AddCommand(sketch.NewCommand())
8991
cmd.AddCommand(upload.NewCommand())
9092
cmd.AddCommand(debug.NewCommand())

cli/outdated/outdated.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 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
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package outdated
17+
18+
import (
19+
"context"
20+
"os"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/cli/instance"
25+
"github.com/arduino/arduino-cli/commands/core"
26+
"github.com/arduino/arduino-cli/commands/lib"
27+
rpc "github.com/arduino/arduino-cli/rpc/commands"
28+
"github.com/arduino/arduino-cli/table"
29+
"github.com/sirupsen/logrus"
30+
"github.com/spf13/cobra"
31+
)
32+
33+
// NewCommand creates a new `outdated` command
34+
func NewCommand() *cobra.Command {
35+
outdatedCommand := &cobra.Command{
36+
Use: "outdated",
37+
Short: "Lists cores and libraries that can be upgraded\n",
38+
Long: "This commands shows a list of installed cores and/or libraries\n" +
39+
"that can be upgraded. If nothing needs to be updated the output is empty.",
40+
Example: " " + os.Args[0] + " outdated\n",
41+
Args: cobra.NoArgs,
42+
Run: runOutdatedCommand,
43+
}
44+
45+
return outdatedCommand
46+
}
47+
48+
func runOutdatedCommand(cmd *cobra.Command, args []string) {
49+
inst, err := instance.CreateInstance()
50+
if err != nil {
51+
feedback.Errorf("Error upgrading: %v", err)
52+
os.Exit(errorcodes.ErrGeneric)
53+
}
54+
55+
logrus.Info("Executing `arduino outdated`")
56+
57+
// Gets outdated cores
58+
targets, err := core.GetPlatforms(inst.Id, true)
59+
if err != nil {
60+
feedback.Errorf("Error retrieving core list: %v", err)
61+
os.Exit(errorcodes.ErrGeneric)
62+
}
63+
64+
// Gets outdated libraries
65+
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
66+
Instance: inst,
67+
All: true,
68+
Updatable: true,
69+
})
70+
if err != nil {
71+
feedback.Errorf("Error retrieving library list: %v", err)
72+
os.Exit(errorcodes.ErrGeneric)
73+
}
74+
75+
tab := table.New()
76+
tab.SetHeader("Name", "Installed version", "New version")
77+
78+
// Prints outdated cores
79+
if len(targets) > 0 {
80+
for _, t := range targets {
81+
plat := t.Platform
82+
tab.AddRow(plat.Name, t.Version, plat.GetLatestRelease().Version)
83+
}
84+
}
85+
86+
// Prints outdated libraries
87+
libs := res.GetInstalledLibrary()
88+
if len(libs) > 0 {
89+
for _, l := range libs {
90+
tab.AddRow(l.Library.Name, l.Library.Version, l.Release.Version)
91+
}
92+
}
93+
if len(targets) > 0 || len(libs) > 0 {
94+
feedback.Print(tab.Render())
95+
}
96+
97+
logrus.Info("Done")
98+
}

test/test_outdated.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file is part of arduino-cli.
2+
#
3+
# Copyright 2020 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+
17+
def test_outdated(run_command):
18+
# Updates index for cores and libraries
19+
assert run_command("core update-index")
20+
assert run_command("lib update-index")
21+
22+
# Installs an outdated core and library
23+
assert run_command("core install arduino:[email protected]")
24+
assert run_command("lib install [email protected]")
25+
26+
# Installs latest version of a core and a library
27+
assert run_command("core install arduino:samd")
28+
assert run_command("lib install ArduinoJson")
29+
30+
# Verifies only outdate core and library are returned
31+
result = run_command("outdated")
32+
assert result.ok
33+
lines = result.stdout.splitlines()
34+
assert lines[1].startswith("Arduino AVR Boards")
35+
assert lines[2].startswith("USBHost")

0 commit comments

Comments
 (0)