16
16
package outdated
17
17
18
18
import (
19
+ "fmt"
19
20
"os"
21
+ "sort"
22
+ "strings"
20
23
21
24
"github.com/arduino/arduino-cli/i18n"
22
25
"github.com/arduino/arduino-cli/internal/cli/core"
26
+ "github.com/arduino/arduino-cli/internal/cli/feedback"
23
27
"github.com/arduino/arduino-cli/internal/cli/instance"
24
28
"github.com/arduino/arduino-cli/internal/cli/lib"
25
29
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
30
+ "github.com/arduino/arduino-cli/table"
26
31
"github.com/sirupsen/logrus"
27
32
"github.com/spf13/cobra"
28
33
)
@@ -49,8 +54,97 @@ func runOutdatedCommand(cmd *cobra.Command, args []string) {
49
54
Outdated (inst )
50
55
}
51
56
52
- // Outdated returns a list of outdated platforms and libraries
57
+ // Outdated prints a list of outdated platforms and libraries
53
58
func Outdated (inst * rpc.Instance ) {
54
- core .List (inst , false , true )
55
- lib .List (inst , []string {}, false , true )
59
+ feedback .PrintResult (
60
+ outdatedResult {core .GetList (inst , false , true ), lib .GetList (inst , []string {}, false , true )},
61
+ )
62
+ }
63
+
64
+ // output from this command requires special formatting, let's create a dedicated
65
+ // feedback.Result implementation
66
+ type outdatedResult struct {
67
+ platforms []* rpc.Platform
68
+ installedLibs []* rpc.InstalledLibrary
69
+ }
70
+
71
+ func (ir outdatedResult ) Data () interface {} {
72
+ return []interface {}{ir .platforms , ir .installedLibs }
73
+ }
74
+
75
+ func (ir outdatedResult ) String () string {
76
+ if len (ir .platforms ) == 0 && len (ir .installedLibs ) == 0 {
77
+ return tr ("No outdated platforms or libraries found." )
78
+ }
79
+
80
+ // A table useful both for platforms and libraries, where some of the fields will be blank.
81
+ t := table .New ()
82
+ t .SetHeader (
83
+ tr ("ID" ),
84
+ tr ("Name" ),
85
+ tr ("Installed" ),
86
+ tr ("Latest" ),
87
+ tr ("Location" ),
88
+ tr ("Description" ),
89
+ )
90
+ t .SetColumnWidthMode (2 , table .Average )
91
+ t .SetColumnWidthMode (3 , table .Average )
92
+ t .SetColumnWidthMode (5 , table .Average )
93
+
94
+ // Based on internal/cli/core/list.go
95
+ for _ , p := range ir .platforms {
96
+ name := p .Name
97
+ if p .Deprecated {
98
+ name = fmt .Sprintf ("[%s] %s" , tr ("DEPRECATED" ), name )
99
+ }
100
+ t .AddRow (p .Id , name , p .Installed , p .Latest , "" , "" )
101
+ }
102
+
103
+ // Based on internal/cli/lib/list.go
104
+ sort .Slice (ir .installedLibs , func (i , j int ) bool {
105
+ return strings .ToLower (
106
+ ir .installedLibs [i ].Library .Name ,
107
+ ) < strings .ToLower (
108
+ ir .installedLibs [j ].Library .Name ,
109
+ ) ||
110
+ strings .ToLower (
111
+ ir .installedLibs [i ].Library .ContainerPlatform ,
112
+ ) < strings .ToLower (
113
+ ir .installedLibs [j ].Library .ContainerPlatform ,
114
+ )
115
+ })
116
+ lastName := ""
117
+ for _ , libMeta := range ir .installedLibs {
118
+ lib := libMeta .GetLibrary ()
119
+ name := lib .Name
120
+ if name == lastName {
121
+ name = ` "`
122
+ } else {
123
+ lastName = name
124
+ }
125
+
126
+ location := lib .GetLocation ().String ()
127
+ if lib .ContainerPlatform != "" {
128
+ location = lib .GetContainerPlatform ()
129
+ }
130
+
131
+ available := ""
132
+ sentence := ""
133
+ if libMeta .GetRelease () != nil {
134
+ available = libMeta .GetRelease ().GetVersion ()
135
+ sentence = lib .Sentence
136
+ }
137
+
138
+ if available == "" {
139
+ available = "-"
140
+ }
141
+ if sentence == "" {
142
+ sentence = "-"
143
+ } else if len (sentence ) > 40 {
144
+ sentence = sentence [:37 ] + "..."
145
+ }
146
+ t .AddRow ("" , name , lib .Version , available , location , sentence )
147
+ }
148
+
149
+ return t .Render ()
56
150
}
0 commit comments