@@ -18,17 +18,23 @@ package core
18
18
import (
19
19
"context"
20
20
"os"
21
+ "path"
21
22
"sort"
22
23
"strings"
24
+ "time"
23
25
26
+ "github.com/arduino/arduino-cli/arduino/utils"
24
27
"github.com/arduino/arduino-cli/cli/errorcodes"
25
28
"github.com/arduino/arduino-cli/cli/feedback"
29
+ "github.com/arduino/arduino-cli/cli/globals"
26
30
"github.com/arduino/arduino-cli/cli/instance"
27
31
"github.com/arduino/arduino-cli/cli/output"
28
32
"github.com/arduino/arduino-cli/commands"
29
33
"github.com/arduino/arduino-cli/commands/core"
34
+ "github.com/arduino/arduino-cli/configuration"
30
35
rpc "github.com/arduino/arduino-cli/rpc/commands"
31
36
"github.com/arduino/arduino-cli/table"
37
+ "github.com/arduino/go-paths-helper"
32
38
"github.com/sirupsen/logrus"
33
39
"github.com/spf13/cobra"
34
40
)
@@ -51,19 +57,24 @@ func initSearchCommand() *cobra.Command {
51
57
return searchCommand
52
58
}
53
59
60
+ // indexUpdateInterval specifies the time threshold over which indexes are updated
61
+ const indexUpdateInterval = "24h"
62
+
54
63
func runSearchCommand (cmd * cobra.Command , args []string ) {
55
64
inst , err := instance .CreateInstance ()
56
65
if err != nil {
57
66
feedback .Errorf ("Error searching for platforms: %v" , err )
58
67
os .Exit (errorcodes .ErrGeneric )
59
68
}
60
69
61
- _ , err = commands .UpdateIndex (context .Background (), & rpc.UpdateIndexReq {
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.UpdateIndexReq {
72
+ Instance : inst ,
73
+ }, output .ProgressBar ())
74
+ if err != nil {
75
+ feedback .Errorf ("Error updating index: %v" , err )
76
+ os .Exit (errorcodes .ErrGeneric )
77
+ }
67
78
}
68
79
69
80
arguments := strings .ToLower (strings .Join (args , " " ))
@@ -107,3 +118,49 @@ func (sr searchResults) String() string {
107
118
}
108
119
return "No platforms matching your search."
109
120
}
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