2021-11-22 19:53:42 +08:00
|
|
|
package invoker
|
2020-12-06 16:49:49 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-11-22 19:53:42 +08:00
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
2021-11-23 21:22:23 +08:00
|
|
|
"strings"
|
2020-12-06 16:49:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DBScript interface {
|
|
|
|
Run(ctx context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
var availableScripts = make(map[string]DBScript)
|
|
|
|
|
|
|
|
func RunDBScript(name string, ctx context.Context) error {
|
|
|
|
if script, ok := availableScripts[name]; ok {
|
2022-11-23 18:31:43 +08:00
|
|
|
util.Log().Info("Start executing database script %q.", name)
|
2020-12-06 16:49:49 +08:00
|
|
|
script.Run(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 18:31:43 +08:00
|
|
|
return fmt.Errorf("Database script %q not exist.", name)
|
2020-12-06 16:49:49 +08:00
|
|
|
}
|
|
|
|
|
2021-11-22 19:53:42 +08:00
|
|
|
func Register(name string, script DBScript) {
|
2020-12-06 16:49:49 +08:00
|
|
|
availableScripts[name] = script
|
|
|
|
}
|
2021-11-22 19:53:42 +08:00
|
|
|
|
|
|
|
func ListPrefix(prefix string) []string {
|
|
|
|
var scripts []string
|
|
|
|
for name := range availableScripts {
|
2021-11-23 21:22:23 +08:00
|
|
|
if strings.HasPrefix(name, prefix) {
|
2021-11-22 19:53:42 +08:00
|
|
|
scripts = append(scripts, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scripts
|
|
|
|
}
|