forked from gobuffalo/fizz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizz.go
53 lines (45 loc) · 959 Bytes
/
fizz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package fizz
import (
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
)
type Options map[string]interface{}
type fizzer struct {
Bubbler *Bubbler
}
func (f fizzer) add(s string, err error) error {
if err != nil {
return errors.WithStack(err)
}
f.Bubbler.data = append(f.Bubbler.data, s)
return nil
}
func (f fizzer) Exec(out io.Writer) func(string) error {
return func(s string) error {
args := strings.Split(s, " ")
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = out
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return errors.Wrapf(err, "error executing command: %s", s)
}
return nil
}
}
func AFile(f *os.File, t Translator) (string, error) {
b, err := ioutil.ReadAll(f)
if err != nil {
return "", errors.WithStack(err)
}
return AString(string(b), t)
}
func AString(s string, t Translator) (string, error) {
b := NewBubbler(t)
return b.Bubble(s)
}