@@ -18,17 +18,23 @@ package core
1818import (
1919 "context"
2020 "os"
21+ "path"
2122 "sort"
2223 "strings"
24+ "time"
2325
26+ "github.com/arduino/arduino-cli/arduino/utils"
2427 "github.com/arduino/arduino-cli/cli/errorcodes"
2528 "github.com/arduino/arduino-cli/cli/feedback"
29+ "github.com/arduino/arduino-cli/cli/globals"
2630 "github.com/arduino/arduino-cli/cli/instance"
2731 "github.com/arduino/arduino-cli/cli/output"
2832 "github.com/arduino/arduino-cli/commands"
2933 "github.com/arduino/arduino-cli/commands/core"
34+ "github.com/arduino/arduino-cli/configuration"
3035 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3136 "github.com/arduino/arduino-cli/table"
37+ "github.com/arduino/go-paths-helper"
3238 "github.com/sirupsen/logrus"
3339 "github.com/spf13/cobra"
3440)
@@ -51,19 +57,24 @@ func initSearchCommand() *cobra.Command {
5157 return searchCommand
5258}
5359
60+ // indexUpdateInterval specifies the time threshold over which indexes are updated
61+ const indexUpdateInterval = "24h"
62+
5463func runSearchCommand (cmd * cobra.Command , args []string ) {
5564 inst , err := instance .CreateInstance ()
5665 if err != nil {
5766 feedback .Errorf ("Error searching for platforms: %v" , err )
5867 os .Exit (errorcodes .ErrGeneric )
5968 }
6069
61- _ , err = commands .UpdateIndex (context .Background (), & rpc.UpdateIndexRequest {
62- Instance : inst ,
63- }, output .ProgressBar ())
64- if err != nil {
65- feedback .Errorf ("Error updating index: %v" , err )
66- os .Exit (errorcodes .ErrGeneric )
70+ if indexesNeedUpdating (indexUpdateInterval ) {
71+ _ , err = commands .UpdateIndex (context .Background (), & rpc.UpdateIndexRequest {
72+ Instance : inst ,
73+ }, output .ProgressBar ())
74+ if err != nil {
75+ feedback .Errorf ("Error updating index: %v" , err )
76+ os .Exit (errorcodes .ErrGeneric )
77+ }
6778 }
6879
6980 arguments := strings .ToLower (strings .Join (args , " " ))
@@ -107,3 +118,49 @@ func (sr searchResults) String() string {
107118 }
108119 return "No platforms matching your search."
109120}
121+
122+ // indexesNeedUpdating returns whether one or more index files need updating.
123+ // A duration string must be provided to calculate the time threshold
124+ // used to update the indexes, if the duration is not valid a default
125+ // of 24 hours is used.
126+ // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
127+ func indexesNeedUpdating (duration string ) bool {
128+ indexpath := paths .New (configuration .Settings .GetString ("directories.Data" ))
129+
130+ now := time .Now ()
131+ modTimeThreshold , err := time .ParseDuration (duration )
132+ // Not the most elegant way of handling this error
133+ // but it does its job
134+ if err != nil {
135+ modTimeThreshold , _ = time .ParseDuration ("24h" )
136+ }
137+
138+ urls := []string {globals .DefaultIndexURL }
139+ urls = append (urls , configuration .Settings .GetStringSlice ("board_manager.additional_urls" )... )
140+ for _ , u := range urls {
141+ URL , err := utils .URLParse (u )
142+ if err != nil {
143+ continue
144+ }
145+
146+ if URL .Scheme == "file" {
147+ // No need to update local files
148+ continue
149+ }
150+
151+ coreIndexPath := indexpath .Join (path .Base (URL .Path ))
152+ if coreIndexPath .NotExist () {
153+ return true
154+ }
155+
156+ info , err := coreIndexPath .Stat ()
157+ if err != nil {
158+ return true
159+ }
160+
161+ if now .After (info .ModTime ().Add (modTimeThreshold )) {
162+ return true
163+ }
164+ }
165+ return false
166+ }
0 commit comments