Skip to content

Commit c222ad0

Browse files
[skip-changelog] Do not force update index every time lib search is executed (arduino#2072)
* cli: use cached index with lib search (arduino#1624) * remove comment on default cache timeout (maintenance smell) * Simplify code applying minor changes --------- Co-authored-by: ardnew <[email protected]>
1 parent 396718f commit c222ad0

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

internal/cli/lib/search.go

+31-6
Original file line numberDiff line numberDiff line change
@@ -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+
5157
func 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

Comments
 (0)