From 15264b20e314b7b809b17b7dbbbf3ea9f905e862 Mon Sep 17 00:00:00 2001 From: ardnew Date: Fri, 1 Jul 2022 13:46:15 -0500 Subject: [PATCH 1/3] cli: use cached index with lib search (#1624) --- cli/lib/search.go | 57 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/cli/lib/search.go b/cli/lib/search.go index b16e76790ad..b96b2d22e2c 100644 --- a/cli/lib/search.go +++ b/cli/lib/search.go @@ -21,6 +21,7 @@ import ( "os" "sort" "strings" + "time" "github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/feedback" @@ -28,7 +29,9 @@ import ( "github.com/arduino/arduino-cli/cli/output" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands/lib" + "github.com/arduino/arduino-cli/configuration" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" + "github.com/arduino/go-paths-helper" "github.com/sirupsen/logrus" "github.com/spf13/cobra" semver "go.bug.st/relaxed-semver" @@ -51,6 +54,9 @@ func initSearchCommand() *cobra.Command { return searchCommand } +// indexUpdateInterval specifies the time threshold over which indexes are updated +const indexUpdateInterval = "60m" + func runSearchCommand(cmd *cobra.Command, args []string) { inst, status := instance.Create() logrus.Info("Executing `arduino-cli lib search`") @@ -60,13 +66,15 @@ func runSearchCommand(cmd *cobra.Command, args []string) { os.Exit(errorcodes.ErrGeneric) } - if err := commands.UpdateLibrariesIndex( - context.Background(), - &rpc.UpdateLibrariesIndexRequest{Instance: inst}, - output.ProgressBar(), - ); err != nil { - feedback.Errorf(tr("Error updating library index: %v"), err) - os.Exit(errorcodes.ErrGeneric) + if indexNeedsUpdating(indexUpdateInterval) { + if err := commands.UpdateLibrariesIndex( + context.Background(), + &rpc.UpdateLibrariesIndexRequest{Instance: inst}, + output.ProgressBar(), + ); err != nil { + feedback.Errorf(tr("Error updating library index: %v"), err) + os.Exit(errorcodes.ErrGeneric) + } } for _, err := range instance.Init(inst) { @@ -193,3 +201,38 @@ func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version sort.Sort(semver.List(res)) return res } + +// indexNeedsUpdating returns whether library_index.json need updating. +// A positive duration string must be provided to calculate the time threshold +// used to update the index (default: +24 hours). +// Valid duration units are "ns", "us" (or "µs"), "ms", "s", "m", "h". +// Use a duration of 0 to always update the index. +func indexNeedsUpdating(duration string) bool { + // Library index path is constant (relative to the data directory). + // It does not depend on board manager URLs or any other configuration. + dataDir := configuration.Settings.GetString("directories.Data") + indexPath := paths.New(dataDir).Join("library_index.json") + // Verify the index file exists and we can read its fstat attrs. + if indexPath.NotExist() { + return true + } + info, err := indexPath.Stat() + if err != nil { + return true + } + // Sanity check the given threshold duration string. + now := time.Now() + modTimeThreshold, err := time.ParseDuration(duration) + if err != nil { + feedback.Error(tr("Invalid timeout: %s", err)) + os.Exit(errorcodes.ErrBadArgument) + } + // The behavior of now.After(T) is confusing if T < 0 and MTIME in the future, + // and is probably not what the user intended. Disallow negative T and inform + // the user that positive thresholds are expected. + if modTimeThreshold < 0 { + feedback.Error(tr("Timeout must be non-negative: %dns (%s)", modTimeThreshold, duration)) + os.Exit(errorcodes.ErrBadArgument) + } + return modTimeThreshold == 0 || now.After(info.ModTime().Add(modTimeThreshold)) +} From bcbda90d55b72fbf7780ba347e5200ea430da2ec Mon Sep 17 00:00:00 2001 From: ardnew Date: Fri, 1 Jul 2022 14:09:00 -0500 Subject: [PATCH 2/3] remove comment on default cache timeout (maintenance smell) --- cli/lib/search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/lib/search.go b/cli/lib/search.go index b96b2d22e2c..3378795d6fd 100644 --- a/cli/lib/search.go +++ b/cli/lib/search.go @@ -204,7 +204,7 @@ func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version // indexNeedsUpdating returns whether library_index.json need updating. // A positive duration string must be provided to calculate the time threshold -// used to update the index (default: +24 hours). +// used to update the index. // Valid duration units are "ns", "us" (or "µs"), "ms", "s", "m", "h". // Use a duration of 0 to always update the index. func indexNeedsUpdating(duration string) bool { From f556aff7079ac5b02683f305329c90f8755c50f0 Mon Sep 17 00:00:00 2001 From: ardnew Date: Fri, 1 Jul 2022 14:15:30 -0500 Subject: [PATCH 3/3] run gofmt on changes --- cli/lib/search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/lib/search.go b/cli/lib/search.go index 3378795d6fd..eaad0061350 100644 --- a/cli/lib/search.go +++ b/cli/lib/search.go @@ -203,7 +203,7 @@ func versionsFromSearchedLibrary(library *rpc.SearchedLibrary) []*semver.Version } // indexNeedsUpdating returns whether library_index.json need updating. -// A positive duration string must be provided to calculate the time threshold +// A positive duration string must be provided to calculate the time threshold // used to update the index. // Valid duration units are "ns", "us" (or "µs"), "ms", "s", "m", "h". // Use a duration of 0 to always update the index.