func revel_db(project_dir string) (*sql.DB, error) { CONFIG, err := revel.LoadConfig("app.conf") if err != nil { return nil, err } driver := CONFIG.StringDefault("db.driver", "mysql") host := CONFIG.StringDefault("db.host", "127.0.0.1") port := CONFIG.IntDefault("db.port", 3306) user := CONFIG.StringDefault("db.username", "mym_dev_user") pass := CONFIG.StringDefault("db.password", "mym_dev_pass") name := CONFIG.StringDefault("db.database", "mym_dev") dsn := symbol.DSNFormat(host, port, user, pass, name) return sql.Open(driver, dsn) }
func modelGenerate(ctx *cli.Context) { template_dir, err := default_template_dir(ctx) if err != nil { fmt.Println("scaffold model generate failed:", err) return } args := ctx.Args() if len(args) != 1 { fmt.Println("Usage: scaffold model generate <project/path>") return } project := args[0] root_dir, err := gopath_src_dir() if err != nil { fmt.Println("scaffold model generate failed:", err) return } project_dir := path.Join(root_dir, project) driver := ctx.String("driver") database := ctx.String("database") host := ctx.String("host") port := ctx.Int("port") user := ctx.String("username") pass := ctx.String("password") dsn := symbol.DSNFormat(host, port, user, pass, database) db, err := sql.Open(driver, dsn) if err != nil { fmt.Println("scaffold model generate failed:", err) return } defer db.Close() tables, err := symbol.GetAllTables(db) if err != nil { fmt.Println("scaffold model generate failed:", err) return } if err := symbol.RenderTemplate(path.Join(template_dir, "model", "model.go.t"), path.Join(project_dir, "model.go"), map[string]interface{}{ "project": project, "tables": tables, }, true); err != nil { fmt.Println("scaffold model generate failed:", err) return } for _, table := range tables { data := map[string]interface{}{ "table": table, } dest := fmt.Sprintf("model.%s.go", symbol.ModuleName(table.Name())) if err := symbol.RenderTemplate(path.Join(template_dir, "model", "model.table.go.t"), path.Join(project_dir, dest), data, true); err != nil { fmt.Println("scaffold model generate failed:", err) return } } }
func Generate(ctx *cli.Context) { template_dir, err := src_template_dir(ctx) if err != nil { fmt.Println("scaffold generate failed:", err) return } if template_dir == "" { fmt.Println("scaffold generate failed: none templates") return } args := ctx.Args() if len(args) != 1 { fmt.Println("Usage: scaffold generate <project/path>") return } project := args[0] project_dir, err := dest_project_dir(project) if err != nil { fmt.Println("scaffold generate failed:", err) return } suffixes := ctx.GlobalStringSlice("include-template-suffix") ignores := ctx.GlobalStringSlice("exclude-template-suffix") force := ctx.GlobalBool("force") driver := ctx.String("driver") database := ctx.String("database") host := ctx.String("host") port := ctx.Int("port") user := ctx.String("username") pass := ctx.String("password") dsn := symbol.DSNFormat(host, port, user, pass, database) db, err := sql.Open(driver, dsn) if err != nil { fmt.Println("scaffold generate failed:", err) return } defer db.Close() tables, err := symbol.GetAllTables(db) if err != nil { fmt.Println("scaffold generate failed:", err) return } if err := filepath.Walk(template_dir, func(srcPath string, info os.FileInfo, err error) error { data := map[string]interface{}{ "project": project, "tables": tables, } for i, table := range tables { data["table"] = table data["index"] = i pathName, err := symbol.RenderString(srcPath, data) if err != nil { return err } destPath := path.Join(project_dir, strings.TrimPrefix(pathName, template_dir)) if info.IsDir() { if err := os.MkdirAll(destPath, info.Mode()); err != nil { return err } } else { if hasSuffix(pathName, ignores) { break } else if hasSuffix(pathName, suffixes) { if err := symbol.RenderTemplate(srcPath, destPath, data, force); err != nil { return err } } else { if err := symbol.CopyFile(srcPath, destPath, force); err != nil { return err } } } if srcPath == pathName { break } } return nil }); err != nil { fmt.Println("scaffold generate failed:", err) return } if len(suffixes) == 0 { fmt.Println("WARNING: scaffold template suffix is unset, just copy files.") } }