github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/cmd/destroy/mailer.go (about)

     1  package destroy
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/gobuffalo/flect"
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // mailerCmd destroys a passed mailer
    14  var mailerCmd = &cobra.Command{
    15  	Use: "mailer [name]",
    16  	// Example: "mailer cars",
    17  	Aliases: []string{"l"},
    18  	Short:   "Destroy mailer files",
    19  	RunE: func(cmd *cobra.Command, args []string) error {
    20  		if len(args) == 0 {
    21  			return fmt.Errorf("you need to provide a valid mailer name in order to destroy it")
    22  		}
    23  
    24  		name := args[0]
    25  
    26  		removeMailer(name)
    27  
    28  		return nil
    29  	},
    30  }
    31  
    32  func removeMailer(name string) {
    33  	if yesToAll || confirm("Want to remove mailer? (y/N)") {
    34  		mailerFileName := flect.Singularize(flect.Underscore(name))
    35  
    36  		files := []string{
    37  			filepath.Join("mailers", fmt.Sprintf("%v.go", mailerFileName)),
    38  			filepath.Join("templates/mail", fmt.Sprintf("%v.html", mailerFileName)),
    39  			filepath.Join("templates/mail", fmt.Sprintf("%v.plush.html", mailerFileName)),
    40  		}
    41  
    42  		for _, f := range files {
    43  			os.Remove(f)
    44  			logrus.Infof("- Deleted %v", f)
    45  		}
    46  
    47  	}
    48  }