mirror of
https://github.com/Bornholm/formidable.git
synced 2025-07-29 16:51:33 +02:00
feat: values updaters
This commit is contained in:
40
internal/data/format/json/encoder_handler.go
Normal file
40
internal/data/format/json/encoder_handler.go
Normal file
@ -0,0 +1,40 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"forge.cadoles.com/wpetit/formidable/internal/data/format"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type EncoderHandler struct{}
|
||||
|
||||
func (d *EncoderHandler) Match(url *url.URL) bool {
|
||||
ext := filepath.Ext(path.Join(url.Host, url.Path))
|
||||
|
||||
return ext == ExtensionJSON ||
|
||||
format.MatchURLQueryFormat(url, FormatJSON)
|
||||
}
|
||||
|
||||
func (d *EncoderHandler) Encode(url *url.URL, data interface{}) (io.Reader, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
encoder := json.NewEncoder(&buf)
|
||||
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return &buf, nil
|
||||
}
|
||||
|
||||
func NewEncoderHandler() *EncoderHandler {
|
||||
return &EncoderHandler{}
|
||||
}
|
38
internal/data/format/yaml/encoder_handler.go
Normal file
38
internal/data/format/yaml/encoder_handler.go
Normal file
@ -0,0 +1,38 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"forge.cadoles.com/wpetit/formidable/internal/data/format"
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type EncoderHandler struct{}
|
||||
|
||||
func (d *EncoderHandler) Match(url *url.URL) bool {
|
||||
ext := filepath.Ext(path.Join(url.Host, url.Path))
|
||||
|
||||
return ExtensionYAML.MatchString(ext) ||
|
||||
format.MatchURLQueryFormat(url, FormatYAML)
|
||||
}
|
||||
|
||||
func (d *EncoderHandler) Encode(url *url.URL, data interface{}) (io.Reader, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
encoder := yaml.NewEncoder(&buf)
|
||||
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return &buf, nil
|
||||
}
|
||||
|
||||
func NewEncoderHandler() *EncoderHandler {
|
||||
return &EncoderHandler{}
|
||||
}
|
38
internal/data/updater.go
Normal file
38
internal/data/updater.go
Normal file
@ -0,0 +1,38 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type UpdaterHandler interface {
|
||||
URLMatcher
|
||||
Update(url *url.URL) (io.WriteCloser, error)
|
||||
}
|
||||
|
||||
type Updater struct {
|
||||
handlers []UpdaterHandler
|
||||
}
|
||||
|
||||
func (u *Updater) Update(url *url.URL) (io.WriteCloser, error) {
|
||||
for _, h := range u.handlers {
|
||||
if !h.Match(url) {
|
||||
continue
|
||||
}
|
||||
|
||||
wr, err := h.Update(url)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return wr, nil
|
||||
}
|
||||
|
||||
return nil, errors.Wrapf(ErrHandlerNotFound, "could not find matching handler for url '%s'", url.String())
|
||||
}
|
||||
|
||||
func NewUpdater(handlers ...UpdaterHandler) *Updater {
|
||||
return &Updater{handlers}
|
||||
}
|
57
internal/data/updater/exec/updater_handler.go
Normal file
57
internal/data/updater/exec/updater_handler.go
Normal file
@ -0,0 +1,57 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const SchemeExec = "exec"
|
||||
|
||||
type UpdaterHandler struct{}
|
||||
|
||||
func (h *UpdaterHandler) Match(url *url.URL) bool {
|
||||
return url.Scheme == SchemeExec
|
||||
}
|
||||
|
||||
func (u *UpdaterHandler) Update(url *url.URL) (io.WriteCloser, error) {
|
||||
path := filepath.Join(url.Host, url.Path)
|
||||
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(absPath)
|
||||
|
||||
if url.Query().Get("env") == "yes" {
|
||||
cmd.Env = os.Environ()
|
||||
}
|
||||
|
||||
if url.Query().Get("stdout") == "yes" {
|
||||
cmd.Stdout = os.Stdout
|
||||
}
|
||||
|
||||
if url.Query().Get("stderr") == "yes" {
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
|
||||
writer, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
panic(errors.WithStack(err))
|
||||
}
|
||||
|
||||
return writer, nil
|
||||
}
|
||||
|
||||
func NewUpdaterHandler() *UpdaterHandler {
|
||||
return &UpdaterHandler{}
|
||||
}
|
33
internal/data/updater/file/updater_handler.go
Normal file
33
internal/data/updater/file/updater_handler.go
Normal file
@ -0,0 +1,33 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const SchemeFile = "file"
|
||||
|
||||
type UpdaterHandler struct{}
|
||||
|
||||
func (h *UpdaterHandler) Match(url *url.URL) bool {
|
||||
return url.Scheme == SchemeFile
|
||||
}
|
||||
|
||||
func (u *UpdaterHandler) Update(url *url.URL) (io.WriteCloser, error) {
|
||||
name := filepath.Join(url.Host, url.Path)
|
||||
|
||||
file, err := os.Create(name)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "could not open file '%s'", name)
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func NewUpdaterHandler() *UpdaterHandler {
|
||||
return &UpdaterHandler{}
|
||||
}
|
23
internal/data/updater/stdout/updater_handler.go
Normal file
23
internal/data/updater/stdout/updater_handler.go
Normal file
@ -0,0 +1,23 @@
|
||||
package stdout
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
const SchemeStdout = "stdout"
|
||||
|
||||
type UpdaterHandler struct{}
|
||||
|
||||
func (h *UpdaterHandler) Match(url *url.URL) bool {
|
||||
return url.Scheme == SchemeStdout
|
||||
}
|
||||
|
||||
func (u *UpdaterHandler) Update(url *url.URL) (io.WriteCloser, error) {
|
||||
return os.Stdout, nil
|
||||
}
|
||||
|
||||
func NewUpdaterHandler() *UpdaterHandler {
|
||||
return &UpdaterHandler{}
|
||||
}
|
Reference in New Issue
Block a user