QBitRun/lib/workflow/steps.go

53 lines
1.2 KiB
Go

package workflow
import (
"fmt"
"io"
"strings"
"github.com/valyala/fasttemplate"
)
type Step struct {
Name string `yaml:"name"`
Run string `yaml:"run"`
Shell string `yaml:"shell"`
}
func (s Step) RenderCommand(c Context) string {
return fasttemplate.ExecuteFuncString(s.Run, "{{", "}}", func(w io.Writer, tag string) (int, error) {
switch tag {
case "torrent_name":
return fmt.Fprintf(w, "%q", c.TorrentName)
case "!torrent_name":
return io.WriteString(w, c.TorrentName)
case "category":
return fmt.Fprintf(w, "%q", c.Category)
case "!category":
return io.WriteString(w, c.Category)
case "tags":
return fmt.Fprintf(w, "%q", strings.Join(c.Tags, ","))
case "!tags":
return io.WriteString(w, strings.Join(c.Tags, ","))
default:
if strings.HasPrefix(tag, "tags[") {
var index int
if n, _ := fmt.Sscanf(tag, "tags[%d]", &index); n > 0 {
if len(c.Tags) > index {
return fmt.Fprintf(w, "%q", c.Tags[index])
}
}
}
if strings.HasPrefix(tag, "!tags[") {
var index int
if n, _ := fmt.Sscanf(tag, "!tags[%d]", &index); n > 0 {
if len(c.Tags) > index {
return io.WriteString(w, c.Tags[index])
}
}
}
return 0, nil
}
})
}