github.com/cheikhshift/buffalo@v0.9.5/buffalo/cmd/destroy/resource.go (about) 1 package destroy 2 3 import ( 4 "bufio" 5 "errors" 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "strings" 11 12 "github.com/markbates/inflect" 13 "github.com/spf13/cobra" 14 ) 15 16 //YesToAll means not to ask when destroying but simply confirm all beforehand. 17 var YesToAll = false 18 19 //ResourceCmd destroys a passed resource 20 var ResourceCmd = &cobra.Command{ 21 Use: "resource [name]", 22 //Example: "resource cars", 23 Aliases: []string{"r"}, 24 Short: "Destroys resource files.", 25 RunE: func(cmd *cobra.Command, args []string) error { 26 if _, err := os.Stat(".buffalo.dev.yml"); os.IsNotExist(err) { 27 return errors.New("destroy needs to run in your buffalo project root") 28 } 29 30 if len(args) == 0 { 31 return errors.New("you need to provide a valid resource name in order to destroy it") 32 } 33 34 name := args[0] 35 fileName := inflect.Pluralize(inflect.Underscore(name)) 36 37 removeTemplates(fileName) 38 removeActions(fileName) 39 removeLocales(fileName) 40 removeModel(name) 41 removeMigrations(fileName) 42 43 return nil 44 }, 45 } 46 47 func confirm(msg string) bool { 48 reader := bufio.NewReader(os.Stdin) 49 fmt.Printf(msg) 50 text, _ := reader.ReadString('\n') 51 52 return (text == "y\n" || text == "Y\n") 53 } 54 55 func removeTemplates(fileName string) { 56 if YesToAll || confirm("Want to remove templates? (Y/n)") { 57 templatesFolder := fmt.Sprintf(filepath.Join("templates", fileName)) 58 fmt.Printf("- Deleted %v folder\n", templatesFolder) 59 os.RemoveAll(templatesFolder) 60 } 61 } 62 63 func removeActions(fileName string) { 64 if YesToAll || confirm("Want to remove actions? (Y/n)") { 65 fmt.Printf("- Deleted %v\n", fmt.Sprintf("actions/%v.go", fileName)) 66 os.Remove(filepath.Join("actions", fmt.Sprintf("%v.go", fileName))) 67 68 fmt.Printf("- Deleted %v\n", fmt.Sprintf("actions/%v_test.go", fileName)) 69 os.Remove(filepath.Join("actions", fmt.Sprintf("%v_test.go", fileName))) 70 71 content, err := ioutil.ReadFile(filepath.Join("actions", "app.go")) 72 if err != nil { 73 fmt.Println("[WARNING] error reading app.go content") 74 return 75 } 76 77 resourceExpression := fmt.Sprintf("app.Resource(\"/%v\", %vResource{&buffalo.BaseResource{}})", fileName, inflect.Camelize(fileName)) 78 newContents := strings.Replace(string(content), resourceExpression, "", -1) 79 80 err = ioutil.WriteFile(filepath.Join("actions", "app.go"), []byte(newContents), 0) 81 if err != nil { 82 fmt.Println("[WARNING] error writing new app.go content") 83 return 84 } 85 86 fmt.Printf("- Deleted References for %v in actions/app.go\n", fileName) 87 } 88 } 89 90 func removeLocales(fileName string) { 91 if YesToAll || confirm("Want to remove locales? (Y/n)") { 92 removeMatch("locales", fmt.Sprintf("%v.*.yaml", fileName)) 93 } 94 } 95 96 func removeModel(name string) { 97 if YesToAll || confirm("Want to remove model? (Y/n)") { 98 modelFileName := inflect.Singularize(inflect.Underscore(name)) 99 100 os.Remove(filepath.Join("models", fmt.Sprintf("%v.go", modelFileName))) 101 os.Remove(filepath.Join("models", fmt.Sprintf("%v_test.go", modelFileName))) 102 103 fmt.Printf("- Deleted %v\n", fmt.Sprintf("models/%v.go", modelFileName)) 104 fmt.Printf("- Deleted %v\n", fmt.Sprintf("models/%v_test.go", modelFileName)) 105 } 106 } 107 108 func removeMigrations(fileName string) { 109 if YesToAll || confirm("Want to remove migrations? (Y/n)") { 110 removeMatch("migrations", fmt.Sprintf("*_create_%v.up.*", fileName)) 111 removeMatch("migrations", fmt.Sprintf("*_create_%v.down.*", fileName)) 112 } 113 } 114 115 func removeMatch(folder, pattern string) { 116 files, err := ioutil.ReadDir(folder) 117 if err == nil { 118 for _, f := range files { 119 matches, _ := filepath.Match(pattern, f.Name()) 120 if !f.IsDir() && matches { 121 path := filepath.Join(folder, f.Name()) 122 os.Remove(path) 123 fmt.Printf("- Deleted %v\n", path) 124 } 125 } 126 } 127 }