示例#1
0
文件: migrate.go 项目: umisama/kocha
func (c *migrateCommand) Run() {
	direction := c.flag.Arg(0)
	switch direction {
	case "up", "down":
		// do nothing.
	default:
		kocha.PanicOnError(c, `abort: no "up" or "down" specified`)
	}
	tmpDir, err := filepath.Abs("tmp")
	if err != nil {
		panic(err)
	}
	if err := os.MkdirAll(tmpDir, 0755); err != nil && !os.IsExist(err) {
		kocha.PanicOnError(c, "abort: failed to create directory: %v", err)
	}
	_, filename, _, _ := runtime.Caller(0)
	skeletonDir := filepath.Join(filepath.Dir(filename), "skeleton", "migrate")
	t := template.Must(template.ParseFiles(filepath.Join(skeletonDir, direction+".go.template")))
	mainFilePath := filepath.ToSlash(filepath.Join(tmpDir, "migrate.go"))
	file, err := os.Create(mainFilePath)
	if err != nil {
		kocha.PanicOnError(c, "abort: failed to create file: %v", err)
	}
	defer file.Close()
	appDir, err := kocha.FindAppDir()
	if err != nil {
		panic(err)
	}
	data := map[string]interface{}{
		"dbImportPath":         c.Package(path.Join(appDir, "db")).ImportPath,
		"migrationsImportPath": c.Package(path.Join(appDir, "db", "migrations")).ImportPath,
		"dbconf":               c.dbconf,
		"limit":                c.limit,
	}
	if err := t.Execute(file, data); err != nil {
		kocha.PanicOnError(c, "abort: failed to write file: %v", err)
	}
	c.execCmd("go", "run", mainFilePath)
	if err := os.RemoveAll(tmpDir); err != nil {
		panic(err)
	}
	kocha.PrintGreen("All migrations are successful!\n")
}
示例#2
0
文件: build.go 项目: umisama/kocha
// Run execute the process for `build` command.
func (c *buildCommand) Run() {
	dir, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	appDir, err := kocha.FindAppDir()
	if err != nil {
		panic(err)
	}
	appName := filepath.Base(dir)
	configPkg, err := c.Package(path.Join(appDir, "config"))
	if err != nil {
		kocha.PanicOnError(c, "abort: cannot import `%s`: %v", path.Join(appDir, "config"), err)
	}
	var dbImportPath string
	dbPkg, err := c.Package(path.Join(appDir, "db"))
	if err == nil {
		dbImportPath = dbPkg.ImportPath
	}
	var migrationsImportPath string
	migrationsPkg, err := c.Package(path.Join(appDir, "db", "migrations"))
	if err == nil {
		migrationsImportPath = migrationsPkg.ImportPath
	}
	tmpDir, err := filepath.Abs("tmp")
	if err != nil {
		panic(err)
	}
	if err := os.Mkdir(tmpDir, 0755); err != nil && !os.IsExist(err) {
		kocha.PanicOnError(c, "abort: failed to create directory: %v", err)
	}
	_, filename, _, _ := runtime.Caller(0)
	baseDir := filepath.Dir(filename)
	skeletonDir := filepath.Join(baseDir, "skeleton", "build")
	mainTemplate, err := ioutil.ReadFile(filepath.Join(skeletonDir, "main.go.template"))
	if err != nil {
		panic(err)
	}
	mainFilePath := filepath.ToSlash(filepath.Join(tmpDir, "main.go"))
	builderFilePath := filepath.ToSlash(filepath.Join(tmpDir, "builder.go"))
	file, err := os.Create(builderFilePath)
	if err != nil {
		kocha.PanicOnError(c, "abort: failed to create file: %v", err)
	}
	defer file.Close()
	builderTemplatePath := filepath.ToSlash(filepath.Join(skeletonDir, "builder.go.template"))
	t := template.Must(template.ParseFiles(builderTemplatePath))
	var resources map[string]string
	if c.all {
		resources = c.collectResourcePaths(filepath.Join(dir, kocha.StaticDir))
	}
	data := map[string]interface{}{
		"configImportPath":     configPkg.ImportPath,
		"dbImportPath":         dbImportPath,
		"migrationsImportPath": migrationsImportPath,
		"mainTemplate":         string(mainTemplate),
		"mainFilePath":         mainFilePath,
		"resources":            resources,
		"version":              c.detectVersionTag(),
	}
	if err := t.Execute(file, data); err != nil {
		kocha.PanicOnError(c, "abort: failed to write file: %v", err)
	}
	execName := appName
	if runtime.GOOS == "windows" {
		execName += ".exe"
	}
	c.execCmd("go", "run", builderFilePath)
	c.execCmd("go", "build", "-o", execName, mainFilePath)
	if err := os.RemoveAll(tmpDir); err != nil {
		panic(err)
	}
	printSettingEnv()
	fmt.Printf("build all-in-one binary to %v\n", filepath.Join(dir, execName))
	kocha.PrintGreen("Build successful!\n")
}