mirror of
https://github.com/Bornholm/formidable.git
synced 2025-07-24 14:31:34 +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{}
|
||||
}
|
Reference in New Issue
Block a user