forked from gobuffalo/fizz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizz.go
62 lines (54 loc) · 1.31 KB
/
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
54
55
56
57
58
59
60
61
62
/*
Package fizz is a common DSL for writing SQL migrations
*/
package fizz
import (
"io"
"io/ioutil"
"os"
"os/exec"
shellquote "github.com/kballard/go-shellquote"
"github.com/pkg/errors"
)
// Options is a generic map of options.
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, err := shellquote.Split(s)
if err != nil {
return errors.Wrapf(err, "error parsing command: %s", 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
}
}
// AFile reads in a fizz migration from an io.Reader and translates its contents to SQL.
func AFile(f io.Reader, t Translator) (string, error) {
b, err := ioutil.ReadAll(f)
if err != nil {
return "", errors.WithStack(err)
}
return AString(string(b), t)
}
// AString reads a fizz string, and translates its contents to SQL.
func AString(s string, t Translator) (string, error) {
b := NewBubbler(t)
return b.Bubble(s)
}