@@ -21,12 +21,15 @@ import (
2121 "os"
2222 "sort"
2323 "strings"
24+ "time"
2425
2526 "github.com/arduino/arduino-cli/commands"
2627 "github.com/arduino/arduino-cli/commands/lib"
28+ "github.com/arduino/arduino-cli/configuration"
2729 "github.com/arduino/arduino-cli/internal/cli/feedback"
2830 "github.com/arduino/arduino-cli/internal/cli/instance"
2931 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
32+ "github.com/arduino/go-paths-helper"
3033 "github.com/sirupsen/logrus"
3134 "github.com/spf13/cobra"
3235 semver "go.bug.st/relaxed-semver"
@@ -48,6 +51,9 @@ func initSearchCommand() *cobra.Command {
4851 return searchCommand
4952}
5053
54+ // indexUpdateInterval specifies the time threshold over which indexes are updated
55+ const indexUpdateInterval = 60 * time .Minute
56+
5157func runSearchCommand (args []string , namesOnly bool ) {
5258 inst , status := instance .Create ()
5359 logrus .Info ("Executing `arduino-cli lib search`" )
@@ -56,12 +62,14 @@ func runSearchCommand(args []string, namesOnly bool) {
5662 feedback .Fatal (tr ("Error creating instance: %v" , status ), feedback .ErrGeneric )
5763 }
5864
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+ }
6573 }
6674
6775 instance .Init (inst )
@@ -180,3 +188,20 @@ func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version
180188 sort .Sort (semver .List (res ))
181189 return res
182190}
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