func generateCrudFile(fileName, currentPath, targetPath string) error {
	buf, err := crud.CrudTemplates.ReadFile(filepath.Join(currentPath, fileName))
	if err != nil {
		return err
	}
	switch fileName {
	case "crud.en.yaml":
		if tmplData.localePath != "." {
			targetPath = tmplData.localePath
			if err = os.MkdirAll(targetPath, 0755); err != nil {
				return err
			}
		}
		fileName = tmplData.Singular + "." + fileName
	case "root.tmpl":
		fileName = tmplData.Singular + ".go"
	default:
		fileName = fileName[:len(fileName)-5] + ".go"
	}
	err = generate.GenerateFileFromTemplate(fileName, targetPath, string(buf), tmplData)
	if err != nil {
		return err
	}
	return nil
}
generateCrudFile takes the target file name, target path and the path
of the template as arguments and generates a CRUD file using it.
package crud
import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"github.com/aerogear/charmil/cli/pkg/common/generate"
	"github.com/aerogear/charmil/cli/pkg/common/modname"
	"github.com/aerogear/charmil/cli/internal/template/crud"
	"github.com/aerogear/charmil/cli/pkg/factory"
	"github.com/spf13/cobra"
)
type TemplateData struct {
	crudPath string
	localePath string
	Singular string
	Plural string
	ModName string
}
var tmplData = TemplateData{}
func CrudCommand(f *factory.Factory) (*cobra.Command, error) {
	cmd := &cobra.Command{
		Use:           f.Localizer.LocalizeByID("crud.cmd.use"),
		Short:         f.Localizer.LocalizeByID("crud.cmd.short"),
		Long:          f.Localizer.LocalizeByID("crud.cmd.long"),
		Example:       f.Localizer.LocalizeByID("crud.cmd.example"),
		SilenceErrors: true,
		RunE: func(cmd *cobra.Command, args []string) error {
			modName, err := modname.GetModuleName()
			if err != nil {
				return err
			}
			tmplData.ModName = modName
			return generateCrudPackages()
		},
	}
	cmd.Flags().StringVarP(&tmplData.crudPath, f.Localizer.LocalizeByID("crud.flag.crudPath.name"), "c", ".", f.Localizer.LocalizeByID("crud.flag.crudPath.description"))
	cmd.Flags().StringVarP(&tmplData.localePath, f.Localizer.LocalizeByID("crud.flag.localePath.name"), "l", ".", f.Localizer.LocalizeByID("crud.flag.localePath.description"))
	cmd.Flags().StringVarP(&tmplData.Singular, f.Localizer.LocalizeByID("crud.flag.singular.name"), "s", "", f.Localizer.LocalizeByID("crud.flag.singular.description"))
	cmd.Flags().StringVarP(&tmplData.Plural, f.Localizer.LocalizeByID("crud.flag.plural.name"), "p", "", f.Localizer.LocalizeByID("crud.flag.plural.description"))
	err := cmd.MarkFlagRequired("singular")
	if err != nil {
		return nil, err
	}
	err = cmd.MarkFlagRequired("plural")
	if err != nil {
		return nil, err
	}
	return cmd, nil
}
func generateCrudPackages() error {
	err := fs.WalkDir(crud.CrudTemplates, ".", func(path string, info fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if info.Name() == "crud.en.yaml" || info.Name() == "root.tmpl" {
			if err = generateCrudFile(info.Name(), ".", tmplData.crudPath); err != nil {
				return err
			}
			return nil
		}
		if !info.IsDir() || info.Name() == "." {
			return nil
		}
		entries, err := crud.CrudTemplates.ReadDir(path)
		if err != nil {
			return err
		}
		targetPath := fmt.Sprintf("%s/%s", tmplData.crudPath, info.Name())
		for _, entry := range entries {
			err := os.MkdirAll(targetPath, 0755)
			if err != nil {
				return err
			}
			err = generateCrudFile(entry.Name(), path, targetPath)
			if err != nil {
				return err
			}
		}
		return nil
	})
	return err
}