forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.go
59 lines (51 loc) · 1.17 KB
/
fix.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
package cmd
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/gobuffalo/pop/fix"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var fixCmd = &cobra.Command{
Use: "fix",
Aliases: []string{"f", "update"},
Short: "Brings pop, soda, and fizz files in line with the latest APIs",
RunE: func(cmd *cobra.Command, args []string) error {
return filepath.Walk(migrationPath, func(path string, info os.FileInfo, _ error) error {
if info == nil {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if ext != ".fizz" {
return nil
}
b, err := ioutil.ReadFile(path)
if err != nil {
return errors.WithStack(err)
}
content := string(b)
fixed, err := fix.Anko(content)
if err != nil {
return errors.WithStack(err)
}
if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
f, err := os.Create(path)
if err != nil {
return errors.WithStack(err)
}
if _, err := f.WriteString(fixed); err != nil {
return errors.WithStack(err)
}
if err := f.Close(); err != nil {
return errors.WithStack(err)
}
}
return nil
})
},
}
func init() {
RootCmd.AddCommand(fixCmd)
}