@@ -21,12 +21,15 @@ import (
21
21
"os"
22
22
"sort"
23
23
"strings"
24
+ "time"
24
25
25
26
"github.com/arduino/arduino-cli/commands"
26
27
"github.com/arduino/arduino-cli/commands/lib"
28
+ "github.com/arduino/arduino-cli/configuration"
27
29
"github.com/arduino/arduino-cli/internal/cli/feedback"
28
30
"github.com/arduino/arduino-cli/internal/cli/instance"
29
31
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
32
+ "github.com/arduino/go-paths-helper"
30
33
"github.com/sirupsen/logrus"
31
34
"github.com/spf13/cobra"
32
35
semver "go.bug.st/relaxed-semver"
@@ -48,6 +51,9 @@ func initSearchCommand() *cobra.Command {
48
51
return searchCommand
49
52
}
50
53
54
+ // indexUpdateInterval specifies the time threshold over which indexes are updated
55
+ const indexUpdateInterval = 60 * time .Minute
56
+
51
57
func runSearchCommand (args []string , namesOnly bool ) {
52
58
inst , status := instance .Create ()
53
59
logrus .Info ("Executing `arduino-cli lib search`" )
@@ -56,12 +62,14 @@ func runSearchCommand(args []string, namesOnly bool) {
56
62
feedback .Fatal (tr ("Error creating instance: %v" , status ), feedback .ErrGeneric )
57
63
}
58
64
59
- if err := commands .UpdateLibrariesIndex (
60
- context .Background (),
61
- & rpc.UpdateLibrariesIndexRequest {Instance : inst },
62
- feedback .ProgressBar (),
63
- ); err != nil {
64
- feedback .Fatal (tr ("Error updating library index: %v" , err ), feedback .ErrGeneric )
65
+ if indexNeedsUpdating (indexUpdateInterval ) {
66
+ if err := commands .UpdateLibrariesIndex (
67
+ context .Background (),
68
+ & rpc.UpdateLibrariesIndexRequest {Instance : inst },
69
+ feedback .ProgressBar (),
70
+ ); err != nil {
71
+ feedback .Fatal (tr ("Error updating library index: %v" , err ), feedback .ErrGeneric )
72
+ }
65
73
}
66
74
67
75
instance .Init (inst )
@@ -180,3 +188,20 @@ func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version
180
188
sort .Sort (semver .List (res ))
181
189
return res
182
190
}
191
+
192
+ // indexNeedsUpdating returns whether library_index.json needs updating
193
+ func indexNeedsUpdating (timeout time.Duration ) bool {
194
+ // Library index path is constant (relative to the data directory).
195
+ // It does not depend on board manager URLs or any other configuration.
196
+ dataDir := configuration .Settings .GetString ("directories.Data" )
197
+ indexPath := paths .New (dataDir ).Join ("library_index.json" )
198
+ // Verify the index file exists and we can read its fstat attrs.
199
+ if indexPath .NotExist () {
200
+ return true
201
+ }
202
+ info , err := indexPath .Stat ()
203
+ if err != nil {
204
+ return true
205
+ }
206
+ return time .Since (info .ModTime ()) > timeout
207
+ }
0 commit comments