2020-08-08 02:05:35 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config describes the configuration for a single plugin.
|
|
|
|
type Config struct {
|
|
|
|
id string
|
|
|
|
|
|
|
|
// path to the configuration file
|
|
|
|
path string
|
|
|
|
|
|
|
|
// The name of the plugin. This will be displayed in the UI.
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
|
|
|
// An optional description of what the plugin does.
|
|
|
|
Description *string `yaml:"description"`
|
|
|
|
|
|
|
|
// An optional URL for the plugin.
|
|
|
|
URL *string `yaml:"url"`
|
|
|
|
|
|
|
|
// An optional version string.
|
|
|
|
Version *string `yaml:"version"`
|
|
|
|
|
|
|
|
// The communication interface used when communicating with the spawned
|
|
|
|
// plugin process. Defaults to 'raw' if not provided.
|
|
|
|
Interface interfaceEnum `yaml:"interface"`
|
|
|
|
|
|
|
|
// The command to execute for the operations in this plugin. The first
|
|
|
|
// element should be the program name, and subsequent elements are passed
|
|
|
|
// as arguments.
|
|
|
|
//
|
|
|
|
// Note: the execution process will search the path for the program,
|
|
|
|
// then will attempt to find the program in the plugins
|
|
|
|
// directory. The exe extension is not necessary on Windows platforms.
|
|
|
|
// The current working directory is set to that of the stash process.
|
|
|
|
Exec []string `yaml:"exec,flow"`
|
|
|
|
|
|
|
|
// The default log level to output the plugin process's stderr stream.
|
|
|
|
// Only used if the plugin does not encode its output using log level
|
|
|
|
// control characters.
|
|
|
|
// See package common/log for valid values.
|
|
|
|
// If left unset, defaults to log.ErrorLevel.
|
2020-08-09 03:48:47 +00:00
|
|
|
PluginErrLogLevel string `yaml:"errLog"`
|
2020-08-08 02:05:35 +00:00
|
|
|
|
|
|
|
// The task configurations for tasks provided by this plugin.
|
|
|
|
Tasks []*OperationConfig `yaml:"tasks"`
|
2021-06-11 07:24:58 +00:00
|
|
|
|
|
|
|
// The hooks configurations for hooks registered by this plugin.
|
|
|
|
Hooks []*HookConfig `yaml:"hooks"`
|
2020-08-08 02:05:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 05:55:05 +00:00
|
|
|
func (c Config) getPluginTasks(includePlugin bool) []*PluginTask {
|
|
|
|
var ret []*PluginTask
|
2020-08-08 02:05:35 +00:00
|
|
|
|
|
|
|
for _, o := range c.Tasks {
|
2022-04-25 05:55:05 +00:00
|
|
|
task := &PluginTask{
|
2020-08-08 02:05:35 +00:00
|
|
|
Name: o.Name,
|
|
|
|
Description: &o.Description,
|
|
|
|
}
|
|
|
|
|
|
|
|
if includePlugin {
|
|
|
|
task.Plugin = c.toPlugin()
|
|
|
|
}
|
|
|
|
ret = append(ret, task)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-04-25 05:55:05 +00:00
|
|
|
func (c Config) getPluginHooks(includePlugin bool) []*PluginHook {
|
|
|
|
var ret []*PluginHook
|
2021-06-11 07:24:58 +00:00
|
|
|
|
|
|
|
for _, o := range c.Hooks {
|
2022-04-25 05:55:05 +00:00
|
|
|
hook := &PluginHook{
|
2021-06-11 07:24:58 +00:00
|
|
|
Name: o.Name,
|
|
|
|
Description: &o.Description,
|
|
|
|
Hooks: convertHooks(o.TriggeredBy),
|
|
|
|
}
|
|
|
|
|
|
|
|
if includePlugin {
|
|
|
|
hook.Plugin = c.toPlugin()
|
|
|
|
}
|
|
|
|
ret = append(ret, hook)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertHooks(hooks []HookTriggerEnum) []string {
|
|
|
|
var ret []string
|
|
|
|
for _, h := range hooks {
|
|
|
|
ret = append(ret, h.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-08-08 02:05:35 +00:00
|
|
|
func (c Config) getName() string {
|
|
|
|
if c.Name != "" {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2022-04-25 05:55:05 +00:00
|
|
|
func (c Config) toPlugin() *Plugin {
|
|
|
|
return &Plugin{
|
2020-08-08 02:05:35 +00:00
|
|
|
ID: c.id,
|
|
|
|
Name: c.getName(),
|
|
|
|
Description: c.Description,
|
|
|
|
URL: c.URL,
|
|
|
|
Version: c.Version,
|
|
|
|
Tasks: c.getPluginTasks(false),
|
2021-06-11 07:24:58 +00:00
|
|
|
Hooks: c.getPluginHooks(false),
|
2020-08-08 02:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) getTask(name string) *OperationConfig {
|
|
|
|
for _, o := range c.Tasks {
|
|
|
|
if o.Name == name {
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
func (c Config) getHooks(hookType HookTriggerEnum) []*HookConfig {
|
|
|
|
var ret []*HookConfig
|
|
|
|
for _, h := range c.Hooks {
|
|
|
|
for _, t := range h.TriggeredBy {
|
|
|
|
if hookType == t {
|
|
|
|
ret = append(ret, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-08-08 02:05:35 +00:00
|
|
|
func (c Config) getConfigPath() string {
|
|
|
|
return filepath.Dir(c.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) getExecCommand(task *OperationConfig) []string {
|
|
|
|
ret := c.Exec
|
|
|
|
|
|
|
|
ret = append(ret, task.ExecArgs...)
|
|
|
|
|
|
|
|
if len(ret) > 0 {
|
|
|
|
_, err := exec.LookPath(ret[0])
|
|
|
|
if err != nil {
|
|
|
|
// change command to use absolute path
|
|
|
|
pluginPath := filepath.Dir(c.path)
|
|
|
|
ret[0] = filepath.Join(pluginPath, ret[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace {pluginDir} in arguments with that of the plugin directory
|
|
|
|
dir := c.getConfigPath()
|
|
|
|
for i, arg := range ret {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
Enable gocritic (#1848)
* Don't capitalize local variables
ValidCodecs -> validCodecs
* Capitalize deprecation markers
A deprecated marker should be capitalized.
* Use re.MustCompile for static regexes
If the regex fails to compile, it's a programmer error, and should be
treated as such. The regex is entirely static.
* Simplify else-if constructions
Rewrite
else { if cond {}}
to
else if cond {}
* Use a switch statement to analyze formats
Break an if-else chain. While here, simplify code flow.
Also introduce a proper static error for unsupported image formats,
paving the way for being able to check against the error.
* Rewrite ifElse chains into switch statements
The "Effective Go" https://golang.org/doc/effective_go#switch document
mentions it is more idiomatic to write if-else chains as switches when
it is possible.
Find all the plain rewrite occurrences in the code base and rewrite.
In some cases, the if-else chains are replaced by a switch scrutinizer.
That is, the code sequence
if x == 1 {
..
} else if x == 2 {
..
} else if x == 3 {
...
}
can be rewritten into
switch x {
case 1:
..
case 2:
..
case 3:
..
}
which is clearer for the compiler: it can decide if the switch is
better served by a jump-table then a branch-chain.
* Rewrite switches, introduce static errors
Introduce two new static errors:
* `ErrNotImplmented`
* `ErrNotSupported`
And use these rather than forming new generative errors whenever the
code is called. Code can now test on the errors (since they are static
and the pointers to them wont change).
Also rewrite ifElse chains into switches in this part of the code base.
* Introduce a StashBoxError in configuration
Since all stashbox errors are the same, treat them as such in the code
base. While here, rewrite an ifElse chain.
In the future, it might be beneifical to refactor configuration errors
into one error which can handle missing fields, which context the error
occurs in and so on. But for now, try to get an overview of the error
categories by hoisting them into static errors.
* Get rid of an else-block in transaction handling
If we succesfully `recover()`, we then always `panic()`. This means the
rest of the code is not reachable, so we can avoid having an else-block
here.
It also solves an ifElse-chain style check in the code base.
* Use strings.ReplaceAll
Rewrite
strings.Replace(s, o, n, -1)
into
strings.ReplaceAll(s, o, n)
To make it consistent and clear that we are doing an all-replace in the
string rather than replacing parts of it. It's more of a nitpick since
there are no implementation differences: the stdlib implementation is
just to supply -1.
* Rewrite via gocritic's assignOp
Statements of the form
x = x + e
is rewritten into
x += e
where applicable.
* Formatting
* Review comments handled
Stash-box is a proper noun.
Rewrite a switch into an if-chain which returns on the first error
encountered.
* Use context.TODO() over context.Background()
Patch in the same vein as everything else: use the TODO() marker so we
can search for it later and link it into the context tree/tentacle once
it reaches down to this level in the code base.
* Tell the linter to ignore a section in manager_tasks.go
The section is less readable, so mark it with a nolint for now. Because
the rewrite enables a ifElseChain, also mark that as nolint for now.
* Use strings.ReplaceAll over strings.Replace
* Apply an ifElse rewrite
else { if .. { .. } } rewrite into else if { .. }
* Use switch-statements over ifElseChains
Rewrite chains of if-else into switch statements. Where applicable,
add an early nil-guard to simplify case analysis. Also, in
ScanTask's Start(..), invert the logic to outdent the whole block, and
help the reader: if it's not a scene, the function flow is now far more
local to the top of the function, and it's clear that the rest of the
function has to do with scene management.
* Enable gocritic on the code base.
Disable appendAssign for now since we aren't passing that check yet.
* Document the nolint additions
* Document StashBoxBatchPerformerTagInput
2021-10-18 03:12:40 +00:00
|
|
|
ret[i] = strings.ReplaceAll(arg, "{pluginDir}", dir)
|
2020-08-08 02:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
type interfaceEnum string
|
|
|
|
|
|
|
|
// Valid interfaceEnum values
|
|
|
|
const (
|
|
|
|
// InterfaceEnumRPC indicates that the plugin uses the RPCRunner interface
|
|
|
|
// declared in common/rpc.go.
|
|
|
|
InterfaceEnumRPC interfaceEnum = "rpc"
|
|
|
|
|
|
|
|
// InterfaceEnumRaw interfaces will have the common.PluginInput encoded as
|
|
|
|
// json (but may be ignored), and output will be decoded as
|
|
|
|
// common.PluginOutput. If this decoding fails, then the raw output will be
|
|
|
|
// treated as the output.
|
|
|
|
InterfaceEnumRaw interfaceEnum = "raw"
|
2021-05-26 04:17:53 +00:00
|
|
|
|
|
|
|
InterfaceEnumJS interfaceEnum = "js"
|
2020-08-08 02:05:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (i interfaceEnum) Valid() bool {
|
2021-05-26 04:17:53 +00:00
|
|
|
return i == InterfaceEnumRPC || i == InterfaceEnumRaw || i == InterfaceEnumJS
|
2020-08-08 02:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *interfaceEnum) getTaskBuilder() taskBuilder {
|
|
|
|
if *i == InterfaceEnumRaw {
|
|
|
|
return &rawTaskBuilder{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if *i == InterfaceEnumRPC {
|
|
|
|
return &rpcTaskBuilder{}
|
|
|
|
}
|
|
|
|
|
2021-05-26 04:17:53 +00:00
|
|
|
if *i == InterfaceEnumJS {
|
|
|
|
return &jsTaskBuilder{}
|
|
|
|
}
|
|
|
|
|
2020-08-08 02:05:35 +00:00
|
|
|
// shouldn't happen
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperationConfig describes the configuration for a single plugin operation
|
|
|
|
// provided by a plugin.
|
|
|
|
type OperationConfig struct {
|
|
|
|
// Used to identify the operation. Must be unique within a plugin
|
|
|
|
// configuration. This name is shown in the button for the operation
|
|
|
|
// in the UI.
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
|
|
|
// A short description of the operation. This description is shown below
|
|
|
|
// the button in the UI.
|
|
|
|
Description string `yaml:"description"`
|
|
|
|
|
|
|
|
// A list of arguments that will be appended to the plugin's Exec arguments
|
|
|
|
// when executing this operation.
|
|
|
|
ExecArgs []string `yaml:"execArgs"`
|
|
|
|
|
|
|
|
// A map of argument keys to their default values. The default value is
|
|
|
|
// used if the applicable argument is not provided during the operation
|
|
|
|
// call.
|
|
|
|
DefaultArgs map[string]string `yaml:"defaultArgs"`
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
type HookConfig struct {
|
|
|
|
OperationConfig `yaml:",inline"`
|
|
|
|
|
|
|
|
// A list of stash operations that will be used to trigger this hook operation.
|
|
|
|
TriggeredBy []HookTriggerEnum `yaml:"triggeredBy"`
|
|
|
|
}
|
|
|
|
|
2020-08-08 02:05:35 +00:00
|
|
|
func loadPluginFromYAML(reader io.Reader) (*Config, error) {
|
|
|
|
ret := &Config{}
|
|
|
|
|
|
|
|
parser := yaml.NewDecoder(reader)
|
|
|
|
parser.SetStrict(true)
|
|
|
|
err := parser.Decode(&ret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.Interface == "" {
|
|
|
|
ret.Interface = InterfaceEnumRaw
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ret.Interface.Valid() {
|
|
|
|
return nil, fmt.Errorf("invalid interface type %s", ret.Interface)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPluginFromYAMLFile(path string) (*Config, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-24 21:52:55 +00:00
|
|
|
defer file.Close()
|
2020-08-08 02:05:35 +00:00
|
|
|
|
|
|
|
ret, err := loadPluginFromYAML(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set id to the filename
|
|
|
|
id := filepath.Base(path)
|
|
|
|
ret.id = id[:strings.LastIndex(id, ".")]
|
|
|
|
ret.path = path
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|