コード例 #1
0
ファイル: abot.go プロジェクト: itsabot/abot
func recursiveCopy(pth string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	parts := strings.Split(pth, string(os.PathSeparator))
	pth = ""
	for i := len(parts) - 1; i > 0; i-- {
		if parts[i] == "base" {
			break
		}
		pth = filepath.Join(parts[i], pth)
	}

	// Handle directories
	if info.IsDir() {
		if err = os.Mkdir(pth, 0777); err != nil {
			return err
		}
		return nil
	}

	// Handle files
	p := filepath.Join(os.Getenv("ABOT_PATH"), "base", pth)
	core.CopyFileContents(p, pth)
	return nil
}
コード例 #2
0
ファイル: abot.go プロジェクト: itsabot/abot
// newAbot creates a new directory for a new Abot project. It's similar to
// `rails new`.
func newAbot(l *log.Logger, name, dbconnstr string) error {
	// Create a new directory for the project
	if err := os.Mkdir(name, 0777); err != nil {
		return err
	}
	if err := os.Chdir(name); err != nil {
		return err
	}

	// Generate abot.env
	fi, err := os.Create("abot.env")
	if err != nil {
		return err
	}
	defer func() {
		if err = fi.Close(); err != nil {
			l.Info("failed to close abot.env.", err)
		}
	}()
	dir, err := os.Getwd()
	if err != nil {
		return err
	}
	_, err = fi.WriteString(serverAbotEnv(name, dir))
	if err != nil {
		return err
	}

	// Copy and modify base files
	p := filepath.Join(os.Getenv("ABOT_PATH"), "base", "plugins.json")
	if err = core.CopyFileContents(p, "plugins.json"); err != nil {
		return err
	}
	p = filepath.Join(os.Getenv("ABOT_PATH"), "base", "server.go.x")
	if err = core.CopyFileContents(p, "server.go"); err != nil {
		return err
	}
	p = filepath.Join(os.Getenv("ABOT_PATH"), "base", ".gitignore")
	if err = core.CopyFileContents(p, ".gitignore"); err != nil {
		return err
	}
	fi2, err := os.OpenFile(".gitignore", os.O_APPEND|os.O_WRONLY, 0666)
	if err != nil {
		return err
	}
	defer func() {
		if err = fi2.Close(); err != nil {
			l.Info("failed to close .gitignore.", err)
		}
	}()
	_, err = fi2.WriteString(name)
	if err != nil {
		l.Fatal("failed to write to .gitignore.", err)
	}

	// Walk the base/assets dir, copying all files
	p = filepath.Join(os.Getenv("ABOT_PATH"), "base", "assets")
	if err = filepath.Walk(p, recursiveCopy); err != nil {
		return err
	}
	p = filepath.Join(os.Getenv("ABOT_PATH"), "base", "cmd")
	if err = filepath.Walk(p, recursiveCopy); err != nil {
		return err
	}
	p = filepath.Join(os.Getenv("ABOT_PATH"), "base", "data")
	if err = filepath.Walk(p, recursiveCopy); err != nil {
		return err
	}
	p = filepath.Join(os.Getenv("ABOT_PATH"), "base", "db")
	if err = filepath.Walk(p, recursiveCopy); err != nil {
		return err
	}

	// Run cmd/dbsetup.sh
	cmd := exec.Command("/bin/sh", "-c", "cmd/dbsetup.sh "+dbconnstr)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err = cmd.Run(); err != nil {
		l.Info("Fix the errors above, then re-run cmd/dbsetup.sh")
		return err
	}

	// TODO analytics on a new Abot project
	return nil
}