(db ScriptStore)
| 39 | } |
| 40 | |
| 41 | func LoadBuiltinScripts(db ScriptStore) { |
| 42 | scripts := fetchRemoteScripts() |
| 43 | source := "remote" |
| 44 | if scripts == nil { |
| 45 | log.Printf("[builtin] using %d local builtin scripts", len(builtinScripts)) |
| 46 | scripts = builtinScripts |
| 47 | source = "local" |
| 48 | } else { |
| 49 | log.Printf("Fetched %d builtin scripts from remote", len(scripts)) |
| 50 | } |
| 51 | |
| 52 | var added, updated, skipped int |
| 53 | for _, s := range scripts { |
| 54 | if !strings.HasPrefix(s.ID, "builtin-") { |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | existing, err := db.GetScript(s.ID) |
| 59 | if err != nil || existing == nil { |
| 60 | s.CreatedAt = time.Now() |
| 61 | s.UpdatedAt = time.Now() |
| 62 | if err := db.SaveScript(&s); err != nil { |
| 63 | log.Printf("Warning: failed to load builtin script %q: %v", s.Name, err) |
| 64 | } else { |
| 65 | added++ |
| 66 | } |
| 67 | continue |
| 68 | } |
| 69 | |
| 70 | if scriptContentEqual(existing, &s) { |
| 71 | skipped++ |
| 72 | continue |
| 73 | } |
| 74 | |
| 75 | s.CreatedAt = existing.CreatedAt |
| 76 | s.UpdatedAt = time.Now() |
| 77 | if err := db.SaveScript(&s); err != nil { |
| 78 | log.Printf("Warning: failed to update builtin script %q: %v", s.Name, err) |
| 79 | } else { |
| 80 | updated++ |
| 81 | log.Printf("↑ Updated builtin script: %s", s.Name) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | log.Printf("Builtin scripts sync complete (%s): %d added, %d updated, %d unchanged", |
| 86 | source, added, updated, skipped) |
| 87 | } |
| 88 | |
| 89 | func scriptContentEqual(a, b *models.Script) bool { |
| 90 | hashA := scriptHash(a) |
nothing calls this directly
no test coverage detected