<commit_before>// Package main generates web project.
package main

import (
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strings"
)

func recursiveSearchReplaceFiles(fullpath string, replacers map[string]string) error {
	fileOrDirList := []string{}
	err := filepath.Walk(fullpath, func(path string, f os.FileInfo, err error) error {
		fileOrDirList = append(fileOrDirList, path)
		return nil
	})

	if err != nil {
		return err
	}

	for _, fileOrDir := range fileOrDirList {
		fileInfo, _ := os.Stat(fileOrDir)
		if !fileInfo.IsDir() {
			for oldString, newString := range replacers {
				log.Print("Replacing " + oldString + " -> '" + newString + "' on " + fileOrDir)

				contentBytes, _ := ioutil.ReadFile(fileOrDir)
				newContentBytes := bytes.Replace(contentBytes, []byte(oldString), []byte(newString), -1)

				err := ioutil.WriteFile(fileOrDir, newContentBytes, fileInfo.Mode())
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}

func main() {
	dir := flag.String("dir", "", "directory of project relative to $GOPATH/src/")
	flag.Parse()

	if *dir == "" {
		log.Fatal("dir option is missing.")
		os.Exit(1)
	}

	dirChunks := strings.Split(*dir, "/")
	repoName := dirChunks[len(dirChunks)-1]
	repoUser := dirChunks[len(dirChunks)-2]

	fullpath := os.ExpandEnv(filepath.Join("$GOPATH", "src", *dir))

	// 1. Create target directory
	log.Print("Creating " + fullpath + "...")
	err := os.MkdirAll(fullpath, 0755)
	if err != nil {
		log.Fatal(err)
	}

	// 2. Copy everything under blank directory to target directory.
	log.Print("Copying a blank project to " + fullpath + "...")
	if output, err := exec.Command("cp", "-rf", "./blank/", fullpath).CombinedOutput(); err != nil {
		log.Fatal(output)
	}

	// 3. Interpolate placeholder variables on the new project.
	log.Print("Replacing placeholder variables to " + repoUser + "/" + repoName + "...")
	replacers := make(map[string]string)
	replacers["$GO_BOOTSTRAP_REPO_USER"] = repoUser
	replacers["$GO_BOOTSTRAP_REPO_NAME"] = repoName
	if err := recursiveSearchReplaceFiles(fullpath, replacers); err != nil {
		log.Fatal(err)
	}

	// 4. Create PostgreSQL databases.
	log.Print("Creating a database named " + repoName + "...")
	if exec.Command("createdb", repoName).Run() != nil {
		log.Print("Unable to create PostgreSQL database: " + repoName)
	}

	log.Print("Creating a database named " + repoName + "-test" + "...")
	if exec.Command("createdb", repoName+"-test").Run() != nil {
		log.Print("Unable to create PostgreSQL database: " + repoName + "-test")
	}

	// 5.a. go get github.com/mattes/migrate.
	log.Print("Installing github.com/mattes/migrate...")<commit_msg>Cleanup logging.<commit_after>// Package main generates web project.
package main

import (
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strings"
)

func recursiveSearchReplaceFiles(fullpath string, replacers map[string]string) error {
	fileOrDirList := []string{}
	err := filepath.Walk(fullpath, func(path string, f os.FileInfo, err error) error {
		fileOrDirList = append(fileOrDirList, path)
		return nil
	})

	if err != nil {
		return err
	}

	for _, fileOrDir := range fileOrDirList {
		fileInfo, _ := os.Stat(fileOrDir)
		if !fileInfo.IsDir() {
			for oldString, newString := range replacers {
				log.Print("Replacing " + oldString + " -> '" + newString + "' on " + fileOrDir)

				contentBytes, _ := ioutil.ReadFile(fileOrDir)
				newContentBytes := bytes.Replace(contentBytes, []byte(oldString), []byte(newString), -1)

				err := ioutil.WriteFile(fileOrDir, newContentBytes, fileInfo.Mode())
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}

func main() {
	dir := flag.String("dir", "", "directory of project relative to $GOPATH/src/")
	flag.Parse()

	if *dir == "" {
		log.Fatal("dir option is missing.")
	}

	dirChunks := strings.Split(*dir, "/")
	repoName := dirChunks[len(dirChunks)-1]
	repoUser := dirChunks[len(dirChunks)-2]

	fullpath := os.ExpandEnv(filepath.Join("$GOPATH", "src", *dir))

	// 1. Create target directory
	log.Print("Creating " + fullpath + "...")
	err := os.MkdirAll(fullpath, 0755)
	if err != nil {
		log.Fatal(err)
	}

	// 2. Copy everything under blank directory to target directory.
	log.Print("Copying a blank project to " + fullpath + "...")
	if output, err := exec.Command("cp", "-rf", "./blank/", fullpath).CombinedOutput(); err != nil {
		log.Fatal(string(output))
	}

	// 3. Interpolate placeholder variables on the new project.
	log.Print("Replacing placeholder variables to " + repoUser + "/" + repoName + "...")
	replacers := make(map[string]string)
	replacers["$GO_BOOTSTRAP_REPO_USER"] = repoUser
	replacers["$GO_BOOTSTRAP_REPO_NAME"] = repoName
	if err := recursiveSearchReplaceFiles(fullpath, replacers); err != nil {
		log.Fatal(err)
	}

	// 4. Create PostgreSQL databases.
	log.Print("Creating a database named " + repoName + "...")
	if exec.Command("createdb", repoName).Run() != nil {
		log.Print("Unable to create PostgreSQL database: " + repoName)
	}

	log.Print("Creating a database named " + repoName + "-test" + "...")
	if exec.Command("createdb", repoName+"-test").Run() != nil {
		log.Print("Unable to create PostgreSQL database: " + repoName + "-test")
	}

	// 5.a. go get github.com/mattes/migrate.
	log.Print("Installing github.com/mattes/migrate...")
{'fraction_non_alphanumeric': 0.16623183004099887, 'fraction_numerical': 0.004658963846440552, 'mean_word_length': 3.8570135746606335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3410429', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1891, 'n_words': 433}