mirror of
https://github.com/Bornholm/formidable.git
synced 2025-07-24 22:41:32 +02:00
feat: values updaters
This commit is contained in:
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