stash/pkg/plugin/config.go

446 lines
10 KiB
Go
Raw Normal View History

2020-08-08 02:05:35 +00:00
package plugin
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
2024-01-08 22:32:26 +00:00
"sort"
2020-08-08 02:05:35 +00:00
"strings"
"github.com/stashapp/stash/pkg/plugin/hook"
"github.com/stashapp/stash/pkg/utils"
2020-08-08 02:05:35 +00:00
"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"`
// The hooks configurations for hooks registered by this plugin.
Hooks []*HookConfig `yaml:"hooks"`
// Javascript files that will be injected into the stash UI.
UI UIConfig `yaml:"ui"`
// Settings that will be used to configure the plugin.
Settings map[string]SettingConfig `yaml:"settings"`
}
type PluginCSP struct {
ScriptSrc []string `json:"script-src" yaml:"script-src"`
StyleSrc []string `json:"style-src" yaml:"style-src"`
ConnectSrc []string `json:"connect-src" yaml:"connect-src"`
}
type UIConfig struct {
// Requires is a list of plugin IDs that this plugin depends on.
// These plugins will be loaded before this plugin.
Requires []string `yaml:"requires"`
// Content Security Policy configuration for the plugin.
CSP PluginCSP `yaml:"csp"`
// Javascript files that will be injected into the stash UI.
// These may be URLs or paths to files relative to the plugin configuration file.
Javascript []string `yaml:"javascript"`
// CSS files that will be injected into the stash UI.
// These may be URLs or paths to files relative to the plugin configuration file.
CSS []string `yaml:"css"`
// Assets is a map of URL prefixes to hosted directories.
// This allows plugins to serve static assets from a URL path.
// Plugin assets are exposed via the /plugin/{pluginId}/assets path.
// For example, if the plugin configuration file contains:
// /foo: bar
// /bar: baz
// /: root
// Then the following requests will be mapped to the following files:
// /plugin/{pluginId}/assets/foo/file.txt -> {pluginDir}/foo/file.txt
// /plugin/{pluginId}/assets/bar/file.txt -> {pluginDir}/baz/file.txt
// /plugin/{pluginId}/assets/file.txt -> {pluginDir}/root/file.txt
Assets utils.URLMap `yaml:"assets"`
}
func isURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}
func (c UIConfig) getCSSFiles(parent Config) []string {
var ret []string
for _, v := range c.CSS {
if !isURL(v) {
ret = append(ret, filepath.Join(parent.getConfigPath(), v))
}
}
return ret
}
func (c UIConfig) getExternalCSS() []string {
var ret []string
for _, v := range c.CSS {
if isURL(v) {
ret = append(ret, v)
}
}
return ret
}
func (c UIConfig) getJavascriptFiles(parent Config) []string {
var ret []string
for _, v := range c.Javascript {
if !isURL(v) {
ret = append(ret, filepath.Join(parent.getConfigPath(), v))
}
}
return ret
}
func (c UIConfig) getExternalScripts() []string {
var ret []string
for _, v := range c.Javascript {
if isURL(v) {
ret = append(ret, v)
}
}
return ret
2020-08-08 02:05:35 +00:00
}
type SettingConfig struct {
// defaults to string
Type PluginSettingTypeEnum `yaml:"type"`
// defaults to key name
DisplayName string `yaml:"displayName"`
Description string `yaml:"description"`
}
func (c Config) getPluginTasks(includePlugin bool) []*PluginTask {
var ret []*PluginTask
2020-08-08 02:05:35 +00:00
for _, o := range c.Tasks {
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
}
func (c Config) getPluginHooks(includePlugin bool) []*PluginHook {
var ret []*PluginHook
for _, o := range c.Hooks {
hook := &PluginHook{
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 []hook.TriggerEnum) []string {
var ret []string
for _, h := range hooks {
ret = append(ret, h.String())
}
return ret
}
func (c Config) getPluginSettings() []PluginSetting {
ret := []PluginSetting{}
2024-01-08 22:32:26 +00:00
var keys []string
for k := range c.Settings {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
o := c.Settings[k]
t := o.Type
if t == "" {
t = PluginSettingTypeEnumString
}
s := PluginSetting{
Name: k,
DisplayName: o.DisplayName,
Description: o.Description,
Type: t,
}
ret = append(ret, s)
}
return ret
}
2020-08-08 02:05:35 +00:00
func (c Config) getName() string {
if c.Name != "" {
return c.Name
}
return c.id
}
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),
Hooks: c.getPluginHooks(false),
UI: PluginUI{
Requires: c.UI.Requires,
ExternalScript: c.UI.getExternalScripts(),
ExternalCSS: c.UI.getExternalCSS(),
Javascript: c.UI.getJavascriptFiles(c),
CSS: c.UI.getCSSFiles(c),
CSP: c.UI.CSP,
Assets: c.UI.Assets,
},
Settings: c.getPluginSettings(),
ConfigPath: c.path,
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
}
func (c Config) getHooks(hookType hook.TriggerEnum) []*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
if task != nil {
ret = append(ret, task.ExecArgs...)
}
2020-08-08 02:05:35 +00:00
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
}
func (c Config) valid() error {
if c.Interface != "" && !c.Interface.Valid() {
return fmt.Errorf("invalid interface type %s", c.Interface)
}
for k, o := range c.Settings {
if o.Type != "" && !o.Type.IsValid() {
return fmt.Errorf("invalid type %s for setting %s", k, o.Type)
}
}
return nil
}
2020-08-08 02:05:35 +00:00
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"`
}
type HookConfig struct {
OperationConfig `yaml:",inline"`
// A list of stash operations that will be used to trigger this hook operation.
TriggeredBy []hook.TriggerEnum `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 err := ret.valid(); err != nil {
return nil, err
2020-08-08 02:05:35 +00:00
}
return ret, nil
}
func loadPluginFromYAMLFile(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
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
}