コード例 #1
0
ファイル: migration.go プロジェクト: jmcarbo/kocha
// Generate generates migration templates.
func (g *migrationGenerator) Generate() {
	name := g.flag.Arg(0)
	if name == "" {
		util.PanicOnError(g, "abort: no NAME given")
	}
	tx := kocha.TxTypeMap[g.txType]
	if tx == nil {
		util.PanicOnError(g, "abort: unsupported transaction type: `%v`", g.txType)
	}
	now := Now().Format("20060102150405")
	data := map[string]interface{}{
		"Name":       util.ToCamelCase(name),
		"TimeStamp":  now,
		"ImportPath": tx.ImportPath(),
		"TxType":     reflect.TypeOf(tx.TransactionType()).String(),
	}
	util.CopyTemplate(g,
		filepath.Join(SkeletonDir("migration"), "migration.go.template"),
		filepath.Join("db", "migrations", fmt.Sprintf("%v_%v.go", now, util.ToSnakeCase(name))), data)
	initPath := filepath.Join("db", "migrations", "init.go")
	if _, err := os.Stat(initPath); os.IsNotExist(err) {
		appDir, err := util.FindAppDir()
		if err != nil {
			panic(err)
		}
		util.CopyTemplate(g,
			filepath.Join(SkeletonDir("migration"), "init.go.template"),
			initPath, map[string]interface{}{
				"typeName":     g.txType,
				"tx":           strings.TrimSpace(util.GoString(tx)),
				"dbImportPath": path.Join(appDir, "db"),
			})
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: naoina/kocha
// Run generates the controller templates.
func (c *generateControllerCommand) Run(args []string) error {
	if len(args) < 1 || args[0] == "" {
		return fmt.Errorf("no NAME given")
	}
	name := args[0]
	camelCaseName := util.ToCamelCase(name)
	snakeCaseName := util.ToSnakeCase(name)
	receiverName := strings.ToLower(name)
	if len(receiverName) > 1 {
		receiverName = receiverName[:2]
	} else {
		receiverName = receiverName[:1]
	}
	data := map[string]interface{}{
		"Name":     camelCaseName,
		"Receiver": receiverName,
	}
	if err := util.CopyTemplate(
		filepath.Join(skeletonDir("controller"), "controller.go"+util.TemplateSuffix),
		filepath.Join("app", "controller", snakeCaseName+".go"), data); err != nil {
		return err
	}
	if err := util.CopyTemplate(
		filepath.Join(skeletonDir("controller"), "view.html"+util.TemplateSuffix),
		filepath.Join("app", "view", snakeCaseName+".html"), data); err != nil {
		return err
	}
	return nil
}
コード例 #3
0
ファイル: main.go プロジェクト: naoina/kocha
// Run generates model templates.
func (c *generateModelCommand) Run(args []string) error {
	if len(args) < 1 || args[0] == "" {
		return fmt.Errorf("no NAME given")
	}
	name := args[0]
	if c.option.ORM == "" {
		c.option.ORM = defaultORM
	}
	mt, exists := modelTypeMap[c.option.ORM]
	if !exists {
		return fmt.Errorf("unsupported ORM: `%v'", c.option.ORM)
	}
	m := mt.FieldTypeMap()
	var fields []modelField
	for _, arg := range args[1:] {
		input := strings.Split(arg, ":")
		if len(input) != 2 {
			return fmt.Errorf("invalid argument format is specified: `%v'", arg)
		}
		name, t := input[0], input[1]
		if name == "" {
			return fmt.Errorf("field name isn't specified: `%v'", arg)
		}
		if t == "" {
			return fmt.Errorf("field type isn't specified: `%v'", arg)
		}
		ft, found := m[t]
		if !found {
			return fmt.Errorf("unsupported field type: `%v'", t)
		}
		fields = append(fields, modelField{
			Name:       util.ToCamelCase(name),
			Type:       ft.Name,
			Column:     util.ToSnakeCase(name),
			OptionTags: ft.OptionTags,
		})
	}
	camelCaseName := util.ToCamelCase(name)
	snakeCaseName := util.ToSnakeCase(name)
	data := map[string]interface{}{
		"Name":   camelCaseName,
		"Fields": fields,
	}
	templatePath, configTemplatePath := mt.TemplatePath()
	if err := util.CopyTemplate(templatePath, filepath.Join("app", "model", snakeCaseName+".go"), data); err != nil {
		return err
	}
	initPath := filepath.Join("db", "config.go")
	if _, err := os.Stat(initPath); os.IsNotExist(err) {
		if err := util.CopyTemplate(configTemplatePath, initPath, nil); err != nil {
			return err
		}
	}
	return nil
}
コード例 #4
0
ファイル: main.go プロジェクト: naoina/kocha
// Run generates migration templates.
func (c *generateMigrationCommand) Run(args []string) error {
	if len(args) < 1 || args[0] == "" {
		return fmt.Errorf("no NAME given")
	}
	name := args[0]
	if c.option.ORM == "" {
		c.option.ORM = defaultORM
	}
	orm, exists := ORM[c.option.ORM]
	if !exists {
		return fmt.Errorf("unsupported ORM: `%v'", c.option.ORM)
	}
	now := _time.Now().Format("20060102150405")
	data := map[string]interface{}{
		"Name":       util.ToCamelCase(name),
		"TimeStamp":  now,
		"ImportPath": orm.ImportPath(),
		"TxType":     reflect.TypeOf(orm.TransactionType()).String(),
	}
	if err := util.CopyTemplate(
		filepath.Join(skeletonDir("migration"), "migration.go"+util.TemplateSuffix),
		filepath.Join("db", "migration", fmt.Sprintf("%v_%v.go", now, util.ToSnakeCase(name))),
		data,
	); err != nil {
		return err
	}
	initPath := filepath.Join("db", "migration", "init.go")
	if _, err := os.Stat(initPath); os.IsNotExist(err) {
		appDir, err := util.FindAppDir()
		if err != nil {
			return err
		}
		if err := util.CopyTemplate(
			filepath.Join(skeletonDir("migration"), "init.go"+util.TemplateSuffix),
			initPath, map[string]interface{}{
				"typeName":     c.option.ORM,
				"tx":           strings.TrimSpace(util.GoString(orm)),
				"dbImportPath": path.Join(appDir, "db"),
			},
		); err != nil {
			return err
		}
	}
	return nil
}
コード例 #5
0
ファイル: controller.go プロジェクト: jmcarbo/kocha
// Generate generate a controller templates.
func (g *controllerGenerator) Generate() {
	name := g.flag.Arg(0)
	if name == "" {
		util.PanicOnError(g, "abort: no NAME given")
	}
	camelCaseName := util.ToCamelCase(name)
	snakeCaseName := util.ToSnakeCase(name)
	data := map[string]interface{}{
		"Name": camelCaseName,
	}
	util.CopyTemplate(g,
		filepath.Join(SkeletonDir("controller"), "controller.go.template"),
		filepath.Join("app", "controllers", snakeCaseName+".go"), data)
	util.CopyTemplate(g,
		filepath.Join(SkeletonDir("controller"), "view.html"),
		filepath.Join("app", "views", snakeCaseName+".html"), data)
	g.addRouteToFile(name)
}
コード例 #6
0
ファイル: model.go プロジェクト: jmcarbo/kocha
// Generate generates model templates.
func (g *modelGenerator) Generate() {
	name := g.flag.Arg(0)
	if name == "" {
		util.PanicOnError(g, "abort: no NAME given")
	}
	mt := modelTypeMap[g.orm]
	if mt == nil {
		util.PanicOnError(g, "abort: unsupported ORM type: `%v`", g.orm)
	}
	m := mt.FieldTypeMap()
	var fields []modelField
	for _, arg := range g.flag.Args()[1:] {
		input := strings.Split(arg, ":")
		if len(input) != 2 {
			util.PanicOnError(g, "abort: invalid argument format has been specified: `%v`", strings.Join(input, ", "))
		}
		name, t := input[0], input[1]
		if name == "" {
			util.PanicOnError(g, "abort: field name hasn't been specified")
		}
		ft, found := m[t]
		if !found {
			util.PanicOnError(g, "abort: unsupported field type: `%v`", t)
		}
		fields = append(fields, modelField{
			Name:       util.ToCamelCase(name),
			Type:       ft.Name,
			Column:     util.ToSnakeCase(name),
			OptionTags: ft.OptionTags,
		})
	}
	camelCaseName := util.ToCamelCase(name)
	snakeCaseName := util.ToSnakeCase(name)
	data := map[string]interface{}{
		"Name":   camelCaseName,
		"Fields": fields,
	}
	templatePath, configTemplatePath := mt.TemplatePath()
	util.CopyTemplate(g, templatePath, filepath.Join("app", "models", snakeCaseName+".go"), data)
	initPath := filepath.Join("db", "config.go")
	if _, err := os.Stat(initPath); os.IsNotExist(err) {
		util.CopyTemplate(g, configTemplatePath, initPath, nil)
	}
}
コード例 #7
0
ファイル: unit.go プロジェクト: jmcarbo/kocha
// Generate generates unit templates.
func (g *unitGenerator) Generate() {
	name := g.flag.Arg(0)
	if name == "" {
		util.PanicOnError(g, "abort: no NAME given")
	}
	camelCaseName := util.ToCamelCase(name)
	snakeCaseName := util.ToSnakeCase(name)
	data := map[string]interface{}{
		"Name": camelCaseName,
	}
	util.CopyTemplate(g,
		filepath.Join(SkeletonDir("unit"), "unit.go.template"),
		filepath.Join("app", "units", snakeCaseName+".go"), data)
}
コード例 #8
0
ファイル: main.go プロジェクト: naoina/kocha
// Run generates unit skeleton files.
func (c *generateUnitCommand) Run(args []string) error {
	if len(args) < 1 || args[0] == "" {
		return fmt.Errorf("no NAME given")
	}
	name := args[0]
	camelCaseName := util.ToCamelCase(name)
	snakeCaseName := util.ToSnakeCase(name)
	data := map[string]interface{}{
		"Name": camelCaseName,
	}
	if err := util.CopyTemplate(
		filepath.Join(skeletonDir("unit"), "unit.go"+util.TemplateSuffix),
		filepath.Join("app", "unit", snakeCaseName+".go"), data); err != nil {
		return err
	}
	return nil
}
コード例 #9
0
ファイル: new.go プロジェクト: jmcarbo/kocha
// Run execute the process for `new` command.
func (c *newCommand) Run() {
	appPath := c.flag.Arg(0)
	if appPath == "" {
		util.PanicOnError(c, "abort: no APP_PATH given")
	}
	dstBasePath := filepath.Join(filepath.SplitList(build.Default.GOPATH)[0], "src", appPath)
	_, filename, _, _ := runtime.Caller(0)
	baseDir := filepath.Dir(filename)
	skeletonDir := filepath.Join(baseDir, "skeleton", "new")
	if _, err := os.Stat(filepath.Join(dstBasePath, "config", "app.go")); err == nil {
		util.PanicOnError(c, "abort: Kocha application is already exists")
	}
	data := map[string]interface{}{
		"appName":   filepath.Base(appPath),
		"appPath":   appPath,
		"secretKey": fmt.Sprintf("%q", string(util.GenerateRandomKey(32))), // AES-256
		"signedKey": fmt.Sprintf("%q", string(util.GenerateRandomKey(16))),
	}
	filepath.Walk(skeletonDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			panic(err)
		}
		if info.IsDir() {
			return nil
		}
		dstPath := filepath.Join(dstBasePath, strings.TrimSuffix(strings.TrimPrefix(path, skeletonDir), ".template"))
		dstDir := filepath.Dir(dstPath)
		dirCreated, err := mkdirAllIfNotExists(dstDir)
		if err != nil {
			util.PanicOnError(c, "abort: failed to create directory: %v", err)
		}
		if dirCreated {
			util.PrintCreateDirectory(dstDir)
		}
		util.CopyTemplate(c, path, dstPath, data)
		return nil
	})
}