github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/models/scripts/invoker/invoker.go (about) 1 package invoker 2 3 import ( 4 "context" 5 "fmt" 6 "github.com/cloudreve/Cloudreve/v3/pkg/util" 7 "strings" 8 ) 9 10 type DBScript interface { 11 Run(ctx context.Context) 12 } 13 14 var availableScripts = make(map[string]DBScript) 15 16 func RunDBScript(name string, ctx context.Context) error { 17 if script, ok := availableScripts[name]; ok { 18 util.Log().Info("Start executing database script %q.", name) 19 script.Run(ctx) 20 return nil 21 } 22 23 return fmt.Errorf("Database script %q not exist.", name) 24 } 25 26 func Register(name string, script DBScript) { 27 availableScripts[name] = script 28 } 29 30 func ListPrefix(prefix string) []string { 31 var scripts []string 32 for name := range availableScripts { 33 if strings.HasPrefix(name, prefix) { 34 scripts = append(scripts, name) 35 } 36 } 37 return scripts 38 }