github.com/singlemusic/buffalo@v0.16.30/buffalo/cmd/destroy/resource.go (about) 1 package destroy 2 3 import ( 4 "bufio" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "strings" 10 11 "github.com/gobuffalo/flect" 12 13 "github.com/sirupsen/logrus" 14 "github.com/spf13/cobra" 15 ) 16 17 // YesToAll means not to ask when destroying but simply confirm all beforehand. 18 var YesToAll = false 19 20 // ResourceCmd destroys a passed resource 21 var ResourceCmd = &cobra.Command{ 22 Use: "resource [name]", 23 // Example: "resource cars", 24 Aliases: []string{"r"}, 25 Short: "Destroy resource files", 26 RunE: func(cmd *cobra.Command, args []string) error { 27 if len(args) == 0 { 28 return fmt.Errorf("you need to provide a valid resource name in order to destroy it") 29 } 30 31 name := args[0] 32 fileName := flect.Pluralize(flect.Underscore(name)) 33 34 removeTemplates(fileName) 35 if err := removeActions(fileName); err != nil { 36 return err 37 } 38 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.Print(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 := filepath.Join("templates", fileName) 58 logrus.Infof("- Deleted %v folder", templatesFolder) 59 os.RemoveAll(templatesFolder) 60 } 61 } 62 63 func removeActions(fileName string) error { 64 if YesToAll || confirm("Want to remove actions? (y/N)") { 65 logrus.Infof("- Deleted %v", fmt.Sprintf("actions/%v.go", fileName)) 66 os.Remove(filepath.Join("actions", fmt.Sprintf("%v.go", fileName))) 67 68 logrus.Infof("- Deleted %v", 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 logrus.Warn("error reading app.go content") 74 return err 75 } 76 77 resourceExpression := fmt.Sprintf("app.Resource(\"/%v\", %vResource{})", fileName, flect.Pascalize(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 logrus.Error("error writing new app.go content") 83 return err 84 } 85 86 logrus.Infof("- Deleted References for %v in actions/app.go", fileName) 87 } 88 89 return nil 90 } 91 92 func removeLocales(fileName string) { 93 if YesToAll || confirm("Want to remove locales? (y/N)") { 94 removeMatch("locales", fmt.Sprintf("%v.*.yaml", fileName)) 95 } 96 } 97 98 func removeModel(name string) { 99 if YesToAll || confirm("Want to remove model? (y/N)") { 100 modelFileName := flect.Singularize(flect.Underscore(name)) 101 102 os.Remove(filepath.Join("models", fmt.Sprintf("%v.go", modelFileName))) 103 os.Remove(filepath.Join("models", fmt.Sprintf("%v_test.go", modelFileName))) 104 105 logrus.Infof("- Deleted %v", fmt.Sprintf("models/%v.go", modelFileName)) 106 logrus.Infof("- Deleted %v", fmt.Sprintf("models/%v_test.go", modelFileName)) 107 } 108 } 109 110 func removeMigrations(fileName string) { 111 if YesToAll || confirm("Want to remove migrations? (y/N)") { 112 removeMatch("migrations", fmt.Sprintf("*_create_%v.up.*", fileName)) 113 removeMatch("migrations", fmt.Sprintf("*_create_%v.down.*", fileName)) 114 } 115 } 116 117 func removeMatch(folder, pattern string) { 118 files, err := ioutil.ReadDir(folder) 119 if err == nil { 120 for _, f := range files { 121 matches, _ := filepath.Match(pattern, f.Name()) 122 if !f.IsDir() && matches { 123 path := filepath.Join(folder, f.Name()) 124 os.Remove(path) 125 logrus.Infof("- Deleted %v", path) 126 } 127 } 128 } 129 }