feat: added the program and template operations
parent
ae59cd6088
commit
3ff4f1c06c
@ -0,0 +1,130 @@
|
||||
package operation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
|
||||
"github.com/aziis98/cabret"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerType("program", &Program{})
|
||||
}
|
||||
|
||||
// Program is a [cabret.ItemOperation] that passes the incoming item as input to the given command.
|
||||
// Options are the following:
|
||||
//
|
||||
// command: <shell command>
|
||||
// io: <format> # optional, by default is "raw"
|
||||
//
|
||||
// The io format can be one of the following
|
||||
//
|
||||
// - "raw" will just pass the item data to the program
|
||||
// - "json" will pass the whole item as JSON to the given program, this should be useful for
|
||||
// making external plugins compatible with cabret.
|
||||
type Program struct {
|
||||
IOFormat string
|
||||
ShellCommand string
|
||||
}
|
||||
|
||||
type Format interface {
|
||||
Input(item cabret.Content) (stdin io.Reader, err error)
|
||||
Output(item cabret.Content, stdout io.Reader) (*cabret.Content, error)
|
||||
}
|
||||
|
||||
var ioProgramFormats = map[string]Format{}
|
||||
|
||||
func init() {
|
||||
ioProgramFormats["json"] = JsonFormat{}
|
||||
ioProgramFormats["raw"] = RawFormat{}
|
||||
}
|
||||
|
||||
type JsonFormat struct{}
|
||||
|
||||
func (JsonFormat) Input(item cabret.Content) (stdin io.Reader, err error) {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
if err := json.NewEncoder(buf).Encode(map[string]any{
|
||||
"type": item.Type,
|
||||
"metadata": item.Metadata,
|
||||
"data": item.Data,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (JsonFormat) Output(item cabret.Content, stdout io.Reader) (*cabret.Content, error) {
|
||||
var result struct {
|
||||
Type string `json:"type"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(stdout).Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cabret.Content{
|
||||
Type: result.Type,
|
||||
Metadata: result.Metadata,
|
||||
Data: []byte(result.Data),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type RawFormat struct{}
|
||||
|
||||
func (RawFormat) Input(item cabret.Content) (stdin io.Reader, err error) {
|
||||
return bytes.NewReader(item.Data), nil
|
||||
}
|
||||
|
||||
func (RawFormat) Output(item cabret.Content, stdout io.Reader) (*cabret.Content, error) {
|
||||
data, err := io.ReadAll(stdout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
item.Data = data
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (op *Program) Configure(options map[string]any) error {
|
||||
var err error
|
||||
op.IOFormat, err = getKey(options, "io", "raw")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.ShellCommand, err = getKey[string](options, "command")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (op *Program) ProcessItem(item cabret.Content) (*cabret.Content, error) {
|
||||
ioFmt, ok := ioProgramFormats[op.IOFormat]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(`unknown io format "%s"`, op.IOFormat)
|
||||
}
|
||||
|
||||
r, err := ioFmt.Input(item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd := exec.Command("sh", "-c", op.ShellCommand)
|
||||
cmd.Stdin = r
|
||||
var buf bytes.Buffer
|
||||
cmd.Stdout = &buf
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ioFmt.Output(item, &buf)
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package operation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
|
||||
"github.com/aziis98/cabret"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerType("template", &Template{})
|
||||
}
|
||||
|
||||
type Template struct {
|
||||
Engine string
|
||||
}
|
||||
|
||||
func (op *Template) Configure(options map[string]any) error {
|
||||
var err error
|
||||
op.Engine, err = getKey[string](options, "engine")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (op *Template) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
|
||||
var t bytes.Buffer
|
||||
|
||||
// concatenate all templates
|
||||
for _, item := range items {
|
||||
t.Write(item.Data)
|
||||
}
|
||||
|
||||
tmpl := t.String()
|
||||
|
||||
var data bytes.Buffer
|
||||
switch op.Engine {
|
||||
case "html":
|
||||
if err := op.RenderHtml(tmpl, items, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case "text":
|
||||
if err := op.RenderText(tmpl, items, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf(`unknown format "%s"`, op.Engine)
|
||||
}
|
||||
|
||||
return []cabret.Content{
|
||||
{
|
||||
Type: items[0].Type,
|
||||
Metadata: cabret.Map{},
|
||||
Data: data.Bytes(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (op *Template) RenderHtml(tmpl string, items []cabret.Content, w io.Writer) error {
|
||||
t, err := template.New("template").Parse(tmpl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := t.ExecuteTemplate(w, "template", map[string]any{"Items": items}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (op *Template) RenderText(tmpl string, items []cabret.Content, w io.Writer) error {
|
||||
t, err := template.New("template").Parse(tmpl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := t.ExecuteTemplate(w, "template", map[string]any{"Items": items}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue