mirror of
https://github.com/stashapp/stash.git
synced 2025-12-18 04:44:37 +03:00
Add plugin tasks (#651)
This commit is contained in:
28
pkg/plugin/examples/goraw/goraw.yml
Normal file
28
pkg/plugin/examples/goraw/goraw.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
# example plugin config
|
||||
name: Hawwwwt Tagger (Raw edition)
|
||||
description: Ultimate Hawwwwt tagging utility (using raw interface).
|
||||
version: 1.0
|
||||
url: http://www.github.com/stashapp/stash
|
||||
exec:
|
||||
- plugin_goraw
|
||||
interface: raw
|
||||
tasks:
|
||||
- name: Add hawwwwt tag to random scene
|
||||
description: Creates a "Hawwwwt" tag if not present and adds to a random scene.
|
||||
defaultArgs:
|
||||
mode: add
|
||||
- name: Remove hawwwwt tag from system
|
||||
description: Removes the "Hawwwwt" tag from all scenes and deletes the tag.
|
||||
defaultArgs:
|
||||
mode: remove
|
||||
- name: Indefinite task
|
||||
description: Sleeps indefinitely - interruptable
|
||||
# we'll try command-line argument for this one
|
||||
execArgs:
|
||||
- indef
|
||||
- "{pluginDir}"
|
||||
- name: Long task
|
||||
description: Sleeps for 100 seconds - interruptable
|
||||
defaultArgs:
|
||||
mode: long
|
||||
|
||||
106
pkg/plugin/examples/goraw/main.go
Normal file
106
pkg/plugin/examples/goraw/main.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// +build plugin_example
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
exampleCommon "github.com/stashapp/stash/pkg/plugin/examples/common"
|
||||
|
||||
"github.com/stashapp/stash/pkg/plugin/common"
|
||||
"github.com/stashapp/stash/pkg/plugin/common/log"
|
||||
"github.com/stashapp/stash/pkg/plugin/util"
|
||||
)
|
||||
|
||||
// raw plugins may accept the plugin input from stdin, or they can elect
|
||||
// to ignore it entirely. In this case it optionally reads from the
|
||||
// command-line parameters.
|
||||
func main() {
|
||||
input := common.PluginInput{}
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
inData, _ := ioutil.ReadAll(os.Stdin)
|
||||
log.Debugf("Raw input: %s", string(inData))
|
||||
decodeErr := json.Unmarshal(inData, &input)
|
||||
|
||||
if decodeErr != nil {
|
||||
panic("missing mode argument")
|
||||
}
|
||||
} else {
|
||||
log.Debug("Using command line inputs")
|
||||
mode := os.Args[1]
|
||||
log.Debugf("Command line inputs: %v", os.Args[1:])
|
||||
input.Args = common.ArgsMap{
|
||||
"mode": mode,
|
||||
}
|
||||
|
||||
// just some hard-coded values
|
||||
input.ServerConnection = common.StashServerConnection{
|
||||
Scheme: "http",
|
||||
Port: 9999,
|
||||
}
|
||||
}
|
||||
|
||||
output := common.PluginOutput{}
|
||||
Run(input, &output)
|
||||
|
||||
out, _ := json.Marshal(output)
|
||||
os.Stdout.WriteString(string(out))
|
||||
}
|
||||
|
||||
func Run(input common.PluginInput, output *common.PluginOutput) error {
|
||||
modeArg := input.Args.String("mode")
|
||||
|
||||
var err error
|
||||
if modeArg == "" || modeArg == "add" {
|
||||
client := util.NewClient(input.ServerConnection)
|
||||
err = exampleCommon.AddTag(client)
|
||||
} else if modeArg == "remove" {
|
||||
client := util.NewClient(input.ServerConnection)
|
||||
err = exampleCommon.RemoveTag(client)
|
||||
} else if modeArg == "long" {
|
||||
err = doLongTask()
|
||||
} else if modeArg == "indef" {
|
||||
err = doIndefiniteTask()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
*output = common.PluginOutput{
|
||||
Error: &errStr,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
outputStr := "ok"
|
||||
*output = common.PluginOutput{
|
||||
Output: &outputStr,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func doLongTask() error {
|
||||
const total = 100
|
||||
upTo := 0
|
||||
|
||||
log.Info("Doing long task")
|
||||
for upTo < total {
|
||||
time.Sleep(time.Second)
|
||||
|
||||
log.Progress(float64(upTo) / float64(total))
|
||||
upTo++
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func doIndefiniteTask() error {
|
||||
log.Warn("Sleeping indefinitely")
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user