forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
127 lines (104 loc) · 3.58 KB
/
resource.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package destroy
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/markbates/inflect"
"github.com/spf13/cobra"
)
//YesToAll means not to ask when destroying but simply confirm all beforehand.
var YesToAll = false
//ResourceCmd destroys a passed resource
var ResourceCmd = &cobra.Command{
Use: "resource [name]",
//Example: "resource cars",
Aliases: []string{"r"},
Short: "Destroys resource files.",
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := os.Stat(".buffalo.dev.yml"); os.IsNotExist(err) {
return errors.New("destroy needs to run in your buffalo project root")
}
if len(args) == 0 {
return errors.New("you need to provide a valid resource name in order to destroy it")
}
name := args[0]
fileName := inflect.Pluralize(inflect.Underscore(name))
removeTemplates(fileName)
removeActions(fileName)
removeLocales(fileName)
removeModel(name)
removeMigrations(fileName)
return nil
},
}
func confirm(msg string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(msg)
text, _ := reader.ReadString('\n')
return (text == "y\n" || text == "Y\n")
}
func removeTemplates(fileName string) {
if YesToAll || confirm("Want to remove templates? (Y/n)") {
templatesFolder := fmt.Sprintf(filepath.Join("templates", fileName))
fmt.Printf("- Deleted %v folder\n", templatesFolder)
os.RemoveAll(templatesFolder)
}
}
func removeActions(fileName string) {
if YesToAll || confirm("Want to remove actions? (Y/n)") {
fmt.Printf("- Deleted %v\n", fmt.Sprintf("actions/%v.go", fileName))
os.Remove(filepath.Join("actions", fmt.Sprintf("%v.go", fileName)))
fmt.Printf("- Deleted %v\n", fmt.Sprintf("actions/%v_test.go", fileName))
os.Remove(filepath.Join("actions", fmt.Sprintf("%v_test.go", fileName)))
content, err := ioutil.ReadFile(filepath.Join("actions", "app.go"))
if err != nil {
fmt.Println("[WARNING] error reading app.go content")
return
}
resourceExpression := fmt.Sprintf("app.Resource(\"/%v\", %vResource{&buffalo.BaseResource{}})", fileName, inflect.Camelize(fileName))
newContents := strings.Replace(string(content), resourceExpression, "", -1)
err = ioutil.WriteFile(filepath.Join("actions", "app.go"), []byte(newContents), 0)
if err != nil {
fmt.Println("[WARNING] error writing new app.go content")
return
}
fmt.Printf("- Deleted References for %v in actions/app.go\n", fileName)
}
}
func removeLocales(fileName string) {
if YesToAll || confirm("Want to remove locales? (Y/n)") {
removeMatch("locales", fmt.Sprintf("%v.*.yaml", fileName))
}
}
func removeModel(name string) {
if YesToAll || confirm("Want to remove model? (Y/n)") {
modelFileName := inflect.Singularize(inflect.Underscore(name))
os.Remove(filepath.Join("models", fmt.Sprintf("%v.go", modelFileName)))
os.Remove(filepath.Join("models", fmt.Sprintf("%v_test.go", modelFileName)))
fmt.Printf("- Deleted %v\n", fmt.Sprintf("models/%v.go", modelFileName))
fmt.Printf("- Deleted %v\n", fmt.Sprintf("models/%v_test.go", modelFileName))
}
}
func removeMigrations(fileName string) {
if YesToAll || confirm("Want to remove migrations? (Y/n)") {
removeMatch("migrations", fmt.Sprintf("*_create_%v.up.*", fileName))
removeMatch("migrations", fmt.Sprintf("*_create_%v.down.*", fileName))
}
}
func removeMatch(folder, pattern string) {
files, err := ioutil.ReadDir(folder)
if err == nil {
for _, f := range files {
matches, _ := filepath.Match(pattern, f.Name())
if !f.IsDir() && matches {
path := filepath.Join(folder, f.Name())
os.Remove(path)
fmt.Printf("- Deleted %v\n", path)
}
}
}
}