MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / FetchScripts

Function FetchScripts

internal/plugins/communityscripts/scripts.go:434–487  ·  view source on GitHub ↗

FetchScripts fetches all available scripts from the repository.

()

Source from the content-addressed store, hash-verified

432
433// FetchScripts fetches all available scripts from the repository.
434func 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.
490func SearchScripts(scripts []Script, query string) []Script {

Callers 1

GetScriptsByCategoryFunction · 0.70

Calls 7

getPluginCacheFunction · 0.85
fetchPocketBaseFunction · 0.85
getScriptsLoggerFunction · 0.70
mapPocketBaseRecordFunction · 0.70
GetMethod · 0.65
DebugMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected