github.com/jacobsoderblom/buffalo@v0.11.0/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/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:   "Destroys resource files.",
    26  	RunE: func(cmd *cobra.Command, args []string) error {
    27  		if len(args) == 0 {
    28  			return errors.New("you need to provide a valid resource name in order to destroy it")
    29  		}
    30  
    31  		name := args[0]
    32  		fileName := inflect.Pluralize(inflect.Underscore(name))
    33  
    34  		removeTemplates(fileName)
    35  		err := removeActions(fileName)
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		removeLocales(fileName)
    41  		removeModel(name)
    42  		removeMigrations(fileName)
    43  
    44  		return nil
    45  	},
    46  }
    47  
    48  func confirm(msg string) bool {
    49  	reader := bufio.NewReader(os.Stdin)
    50  	fmt.Printf(msg)
    51  	text, _ := reader.ReadString('\n')
    52  
    53  	return (text == "y\n" || text == "Y\n")
    54  }
    55  
    56  func removeTemplates(fileName string) {
    57  	if YesToAll || confirm("Want to remove templates? (Y/n)") {
    58  		templatesFolder := fmt.Sprintf(filepath.Join("templates", fileName))
    59  		logrus.Infof("- Deleted %v folder\n", templatesFolder)
    60  		os.RemoveAll(templatesFolder)
    61  	}
    62  }
    63  
    64  func removeActions(fileName string) error {
    65  	if YesToAll || confirm("Want to remove actions? (Y/n)") {
    66  		logrus.Infof("- Deleted %v\n", fmt.Sprintf("actions/%v.go", fileName))
    67  		os.Remove(filepath.Join("actions", fmt.Sprintf("%v.go", fileName)))
    68  
    69  		logrus.Infof("- Deleted %v\n", fmt.Sprintf("actions/%v_test.go", fileName))
    70  		os.Remove(filepath.Join("actions", fmt.Sprintf("%v_test.go", fileName)))
    71  
    72  		content, err := ioutil.ReadFile(filepath.Join("actions", "app.go"))
    73  		if err != nil {
    74  			logrus.Warn("error reading app.go content")
    75  			return err
    76  		}
    77  
    78  		resourceExpression := fmt.Sprintf("app.Resource(\"/%v\", %vResource{})", fileName, inflect.Camelize(fileName))
    79  		newContents := strings.Replace(string(content), resourceExpression, "", -1)
    80  
    81  		err = ioutil.WriteFile(filepath.Join("actions", "app.go"), []byte(newContents), 0)
    82  		if err != nil {
    83  			logrus.Error("error writing new app.go content")
    84  			return err
    85  		}
    86  
    87  		logrus.Infof("- Deleted References for %v in actions/app.go\n", fileName)
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func removeLocales(fileName string) {
    94  	if YesToAll || confirm("Want to remove locales? (Y/n)") {
    95  		removeMatch("locales", fmt.Sprintf("%v.*.yaml", fileName))
    96  	}
    97  }
    98  
    99  func removeModel(name string) {
   100  	if YesToAll || confirm("Want to remove model? (Y/n)") {
   101  		modelFileName := inflect.Singularize(inflect.Underscore(name))
   102  
   103  		os.Remove(filepath.Join("models", fmt.Sprintf("%v.go", modelFileName)))
   104  		os.Remove(filepath.Join("models", fmt.Sprintf("%v_test.go", modelFileName)))
   105  
   106  		logrus.Infof("- Deleted %v\n", fmt.Sprintf("models/%v.go", modelFileName))
   107  		logrus.Infof("- Deleted %v\n", fmt.Sprintf("models/%v_test.go", modelFileName))
   108  	}
   109  }
   110  
   111  func removeMigrations(fileName string) {
   112  	if YesToAll || confirm("Want to remove migrations? (Y/n)") {
   113  		removeMatch("migrations", fmt.Sprintf("*_create_%v.up.*", fileName))
   114  		removeMatch("migrations", fmt.Sprintf("*_create_%v.down.*", fileName))
   115  	}
   116  }
   117  
   118  func removeMatch(folder, pattern string) {
   119  	files, err := ioutil.ReadDir(folder)
   120  	if err == nil {
   121  		for _, f := range files {
   122  			matches, _ := filepath.Match(pattern, f.Name())
   123  			if !f.IsDir() && matches {
   124  				path := filepath.Join(folder, f.Name())
   125  				os.Remove(path)
   126  				logrus.Infof("- Deleted %v\n", path)
   127  			}
   128  		}
   129  	}
   130  }