github.com/ppphp/yayagf@v0.0.1/cmd/generate/curd/crud.go (about)

     1  package curd
     2  
     3  import (
     4  	stdlog "log"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/ppphp/yayagf/internal/log"
    10  
    11  	"github.com/ppphp/yayagf/internal/ent"
    12  
    13  	"github.com/ppphp/yayagf/internal/file"
    14  	"github.com/ppphp/yayagf/pkg/cli"
    15  )
    16  
    17  func CommandFactory() (*cli.Command, error) {
    18  	c := &cli.Command{
    19  		Run: func(args []string, flags map[string]string) (int, error) {
    20  			template := flags["t"]
    21  			if template == "" {
    22  				template = flags["template"]
    23  			}
    24  			templates := []string{}
    25  			if template != "" {
    26  				templates = append(templates, template)
    27  			}
    28  			_, debug := flags["d"]
    29  			if !debug {
    30  				_, debug = flags["debug"]
    31  			}
    32  			if debug {
    33  				stdlog.SetFlags(stdlog.Flags() | stdlog.Llongfile)
    34  				log.Logger.SetReportCaller(true)
    35  				log.Logger.SetLevel(logrus.DebugLevel)
    36  			}
    37  
    38  			if debug {
    39  				log.Printf("%v", args)
    40  			}
    41  			root, err := file.GetAppRoot()
    42  			if err != nil {
    43  				log.Printf("get project name failed: %v", err.Error())
    44  				return 1, err
    45  			}
    46  			mod, err := file.GetMod(root)
    47  			if err != nil {
    48  				return 1, err
    49  			}
    50  			if st, err := os.Stat(filepath.Join(root, "app", "schema", "template")); err == nil {
    51  				if st.IsDir() {
    52  					templates = append(templates, filepath.Join(root, "app", "schema", "template"))
    53  				}
    54  			}
    55  			if debug {
    56  				log.Printf("%v", templates)
    57  			}
    58  			if err := ent.GenerateCRUDFiles(mod, filepath.Join(root, "app", "schema"), filepath.Join(root, "app", "crud"), templates); err != nil {
    59  				return 1, err
    60  			}
    61  
    62  			return 0, nil
    63  		},
    64  	}
    65  	return c, nil
    66  }