Plugin API improvements (#4603)

* Accept plain map for runPluginTask
* Support running plugin task without task name
* Add interface to run plugin operations
* Update RunPluginTask client mutation
This commit is contained in:
WithoutPants
2024-02-22 11:20:21 +11:00
committed by GitHub
parent a8c909e0c9
commit 0c2a2190e5
13 changed files with 229 additions and 105 deletions

View File

@@ -2,6 +2,7 @@ package api
import (
"context"
"strconv"
"github.com/stashapp/stash/internal/manager"
"github.com/stashapp/stash/internal/manager/config"
@@ -9,10 +10,72 @@ import (
"github.com/stashapp/stash/pkg/sliceutil"
)
func (r *mutationResolver) RunPluginTask(ctx context.Context, pluginID string, taskName string, args []*plugin.PluginArgInput) (string, error) {
func toPluginArgs(args []*plugin.PluginArgInput) plugin.OperationInput {
ret := make(plugin.OperationInput)
for _, a := range args {
ret[a.Key] = toPluginArgValue(a.Value)
}
return ret
}
func toPluginArgValue(arg *plugin.PluginValueInput) interface{} {
if arg == nil {
return nil
}
switch {
case arg.Str != nil:
return *arg.Str
case arg.I != nil:
return *arg.I
case arg.B != nil:
return *arg.B
case arg.F != nil:
return *arg.F
case arg.O != nil:
return toPluginArgs(arg.O)
case arg.A != nil:
var ret []interface{}
for _, v := range arg.A {
ret = append(ret, toPluginArgValue(v))
}
return ret
}
return nil
}
func (r *mutationResolver) RunPluginTask(
ctx context.Context,
pluginID string,
taskName *string,
description *string,
args []*plugin.PluginArgInput,
argsMap map[string]interface{},
) (string, error) {
if argsMap == nil {
// convert args to map
// otherwise ignore args in favour of args map
argsMap = toPluginArgs(args)
}
m := manager.GetInstance()
m.RunPluginTask(ctx, pluginID, taskName, args)
return "todo", nil
jobID := m.RunPluginTask(ctx, pluginID, taskName, description, argsMap)
return strconv.Itoa(jobID), nil
}
func (r *mutationResolver) RunPluginOperation(
ctx context.Context,
pluginID string,
args map[string]interface{},
) (interface{}, error) {
if args == nil {
args = make(map[string]interface{})
}
m := manager.GetInstance()
return m.PluginCache.RunPlugin(ctx, pluginID, args)
}
func (r *mutationResolver) ReloadPlugins(ctx context.Context) (bool, error) {