mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
Use github.com/google/shlex to parse commands
Implements: https://todo.sr.ht/~adnano/kiln/21
This commit is contained in:
parent
3ce91bc9ab
commit
38a168c514
8
funcs.go
8
funcs.go
|
@ -5,6 +5,8 @@ import (
|
|||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/google/shlex"
|
||||
)
|
||||
|
||||
// funcs returns functions for use in templates.
|
||||
|
@ -56,7 +58,11 @@ func (_strings) TrimSuffix(a, b string) string { return strings.TrimSuffi
|
|||
|
||||
func executeString(command, input string) (string, error) {
|
||||
var b strings.Builder
|
||||
if err := execute(command, strings.NewReader(input), &b); err != nil {
|
||||
cmd, err := shlex.Split(command)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := execute(cmd, strings.NewReader(input), &b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return b.String(), nil
|
||||
|
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module kiln
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
||||
github.com/pelletier/go-toml v1.9.0
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
|||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
|
||||
github.com/pelletier/go-toml v1.9.0 h1:NOd0BRdOKpPf0SxkL3HxSQOG7rNh+4kl6PHcBPFs7Q0=
|
||||
github.com/pelletier/go-toml v1.9.0/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
|
|
12
page.go
12
page.go
|
@ -120,7 +120,7 @@ func (p *Page) _read(srcDir, path string, task *Task, cfg *Site) error {
|
|||
content = bytes.TrimLeft(content, "\r\n")
|
||||
}
|
||||
|
||||
if cmd, ok := task.Preprocess[strings.TrimPrefix(ext, ".")]; ok {
|
||||
if cmd, ok := task.preprocess[strings.TrimPrefix(ext, ".")]; ok {
|
||||
var buf bytes.Buffer
|
||||
if err := execute(cmd, bytes.NewReader(content), &buf); err != nil {
|
||||
return err
|
||||
|
@ -275,7 +275,7 @@ func (p *Page) write(dstDir string, task *Task) error {
|
|||
|
||||
func (p *Page) writeTo(dstPath string, task *Task) error {
|
||||
var content []byte
|
||||
if cmd := task.Postprocess; cmd != "" {
|
||||
if cmd := task.postprocess; cmd != nil {
|
||||
var buf bytes.Buffer
|
||||
if err := execute(cmd, strings.NewReader(p.Content), &buf); err != nil {
|
||||
return err
|
||||
|
@ -331,9 +331,11 @@ func sortPages(pages []*Page) {
|
|||
}
|
||||
|
||||
// execute runs a command.
|
||||
func execute(command string, input io.Reader, output io.Writer) error {
|
||||
split := strings.Split(command, " ")
|
||||
cmd := exec.Command(split[0], split[1:]...)
|
||||
func execute(command []string, input io.Reader, output io.Writer) error {
|
||||
if len(command) == 0 {
|
||||
return nil
|
||||
}
|
||||
cmd := exec.Command(command[0], command[1:]...)
|
||||
cmd.Stdin = input
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = output
|
||||
|
|
22
site.go
22
site.go
|
@ -6,6 +6,7 @@ import (
|
|||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/google/shlex"
|
||||
"github.com/pelletier/go-toml"
|
||||
)
|
||||
|
||||
|
@ -33,6 +34,8 @@ type Task struct {
|
|||
URL string `toml:"url"`
|
||||
UglyURLs bool `toml:"ugly_urls"`
|
||||
Feeds []Feed `toml:"feeds"`
|
||||
preprocess map[string][]string
|
||||
postprocess []string
|
||||
feeds map[string][]Feed
|
||||
}
|
||||
|
||||
|
@ -92,13 +95,30 @@ func LoadSite(config string) (*Site, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Populate task feeds map
|
||||
for _, task := range site.Tasks {
|
||||
// Populate feeds
|
||||
task.feeds = map[string][]Feed{}
|
||||
for _, feed := range task.Feeds {
|
||||
dir := feed.InputDir
|
||||
task.feeds[dir] = append(task.feeds[dir], feed)
|
||||
}
|
||||
|
||||
// Parse commands
|
||||
task.preprocess = map[string][]string{}
|
||||
for path := range task.Preprocess {
|
||||
preprocess, err := shlex.Split(task.Preprocess[path])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
task.preprocess[path] = preprocess
|
||||
}
|
||||
if task.Postprocess != "" {
|
||||
postprocess, err := shlex.Split(task.Postprocess)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
task.postprocess = postprocess
|
||||
}
|
||||
}
|
||||
|
||||
return site, nil
|
||||
|
|
Loading…
Reference in a new issue