FetchScripts fetches all available scripts from the repository.
()
| 432 | |
| 433 | // FetchScripts fetches all available scripts from the repository. |
| 434 | func FetchScripts() ([]Script, error) { |
| 435 | c := getPluginCache() |
| 436 | |
| 437 | var cachedScripts []Script |
| 438 | found, err := c.Get(ScriptListCacheKey, &cachedScripts) |
| 439 | if err != nil { |
| 440 | getScriptsLogger().Debug("Cache error for fetched scripts: %v", err) |
| 441 | } else if found && len(cachedScripts) > 0 { |
| 442 | getScriptsLogger().Debug("Using cached fetched scripts (%d items)", len(cachedScripts)) |
| 443 | |
| 444 | return cachedScripts, nil |
| 445 | } |
| 446 | |
| 447 | const perPage = 200 |
| 448 | var ( |
| 449 | page = 1 |
| 450 | scripts []Script |
| 451 | ) |
| 452 | |
| 453 | for { |
| 454 | endpoint := fmt.Sprintf("%s/script_scripts/records?perPage=%d&page=%d&sort=slug&expand=type", MetadataPocketBaseAPI, perPage, page) |
| 455 | |
| 456 | var response pocketBaseResponse |
| 457 | if err := fetchPocketBase(endpoint, &response); err != nil { |
| 458 | return nil, fmt.Errorf("failed to fetch script metadata from PocketBase: %w", err) |
| 459 | } |
| 460 | |
| 461 | for _, item := range response.Items { |
| 462 | script := mapPocketBaseRecord(item) |
| 463 | if script.ScriptPath == "" { |
| 464 | getScriptsLogger().Debug("Skipping script %s with unsupported type %q", script.Name, item.Expand.Type.Type) |
| 465 | continue |
| 466 | } |
| 467 | |
| 468 | scripts = append(scripts, script) |
| 469 | } |
| 470 | |
| 471 | if page >= response.TotalPages || len(response.Items) == 0 { |
| 472 | break |
| 473 | } |
| 474 | |
| 475 | page++ |
| 476 | } |
| 477 | |
| 478 | if len(scripts) == 0 { |
| 479 | return nil, fmt.Errorf("no valid scripts found in PocketBase metadata") |
| 480 | } |
| 481 | |
| 482 | if err := c.Set(ScriptListCacheKey, scripts, ScriptListTTL); err != nil { |
| 483 | getScriptsLogger().Debug("Failed to cache fetched scripts: %v", err) |
| 484 | } |
| 485 | |
| 486 | return scripts, nil |
| 487 | } |
| 488 | |
| 489 | // SearchScripts returns scripts whose name, slug, or description contains query. |
| 490 | func SearchScripts(scripts []Script, query string) []Script { |
no test coverage detected