feat: values updaters

This commit is contained in:
2022-05-10 22:31:17 +02:00
parent 08476e5346
commit 0e7e955a58
16 changed files with 415 additions and 155 deletions

View 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{}
}

View 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
View 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}
}

View 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{}
}

View 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{}
}

View 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{}
}