<commit_before>package config

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"strings"

	"github.com/cloudfoundry-incubator/candiedyaml"
)

type ContainerConfig struct {
	ContainerName  string
	Daemon         bool
	Mounts         map[string]string
	PublishedPorts map[int]int
	EnvVars        map[string]string
	ImageTag       string
	Command        []string
	DropletDir     string
}

func NewStageContainerConfig(directories *Directories) (containerConfig *ContainerConfig) {
	containerConfig = &ContainerConfig{
		ContainerName: "cloudfocker-staging",
		Mounts:        directories.Mounts(),
		ImageTag:      "cloudfocker-base:latest",
		Command:       []string{"/focker/fock", "stage", "internal"},
	}
	return
}

func NewRuntimeContainerConfig(cloudfoundryDropletDir string) (containerConfig *ContainerConfig) {
	containerConfig = &ContainerConfig{
		ContainerName: "cloudfocker-runtime",
		Daemon:        true,
		Mounts: map[string]string{
			cloudfoundryDropletDir + "/app": "/app",
		},
		PublishedPorts: map[int]int{8080: 8080},
		EnvVars: map[string]string{
			"HOME":          "/app",
			"TMPDIR":        "/app/tmp",
			"PORT":          "8080",
			"VCAP_SERVICES": vcapServices(cloudfoundryDropletDir),
			"DATABASE_URL":  databaseURL(cloudfoundryDropletDir),
		},
		ImageTag: "cloudfocker-base:latest",
		Command: append([]string{"/bin/bash", "/app/cloudfocker-start-1c4352a23e52040ddb1857d7675fe3cc.sh", "/app"},
			parseStartCommand(cloudfoundryDropletDir)...),
		DropletDir: cloudfoundryDropletDir,
	}
	return
}

func vcapServices(cloudfoundryDropletDir string) (services string) {
	servicesBytes, err := ioutil.ReadFile(cloudfoundryDropletDir + "/app/vcap_services.json")
	if err != nil {
		return
	}
	services = string(servicesBytes)
	return
}

type database struct {
	Credentials struct {
		URI string
	}
}

func databaseURL(cloudfoundryDropletDir string) (databaseURL string) {
	servicesBytes, err := ioutil.ReadFile(cloudfoundryDropletDir + "/app/vcap_services.json")
	if err != nil {
		return
	}

	var services map[string][]database

	json.Unmarshal(servicesBytes, &services)

	for _, serviceDatabase := range services {
		if len(serviceDatabase) > 0 && serviceDatabase[0].Credentials.URI != "" {
			databaseURL = serviceDatabase[0].Credentials.URI
		}
	}

	return
}

type StagingInfoYml struct {
	DetectedBuildpack string `yaml:"detected_buildpack"`
	StartCommand      string `yaml:"start_command"`
}

type ProcfileYml struct {
	Web string `yaml:"web"`
}

func parseStartCommand(cloudfoundryDropletDir string) (startCommand []string) {
	stagingInfoFile, err := os.Open(cloudfoundryDropletDir + "/staging_info.yml")
	if err == nil {
		stagingInfo := new(StagingInfoYml)
		decoder := candiedyaml.NewDecoder(stagingInfoFile)
		err = decoder.Decode(stagingInfo)
		if err != nil {
			log.Fatalf("Failed to decode document: %s", err)
		}
		startCommand = strings.Split(stagingInfo.StartCommand, " ")
		if startCommand[0] != "" {
			return
		}
		procfileFile, err := os.Open(cloudfoundryDropletDir + "/app/Procfile")
		if err == nil {
			procfileInfo := new(ProcfileYml)
			decoder := candiedyaml.NewDecoder(procfileFile)
			err = decoder.Decode(procfileInfo)
			if err != nil {
				log.Fatalf("Failed to decode document: %s", err)
			}
			startCommand = strings.Split(procfileInfo.Web, " ")
			return
		}
	}
	log.Fatal("Unable to find staging_info.yml")
	return
}<commit_msg>renamed cloudfoundryDropletDir to dropletDir<commit_after>package config

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"strings"

	"github.com/cloudfoundry-incubator/candiedyaml"
)

type ContainerConfig struct {
	ContainerName  string
	Daemon         bool
	Mounts         map[string]string
	PublishedPorts map[int]int
	EnvVars        map[string]string
	ImageTag       string
	Command        []string
	DropletDir     string
}

func NewStageContainerConfig(directories *Directories) (containerConfig *ContainerConfig) {
	containerConfig = &ContainerConfig{
		ContainerName: "cloudfocker-staging",
		Mounts:        directories.Mounts(),
		ImageTag:      "cloudfocker-base:latest",
		Command:       []string{"/focker/fock", "stage", "internal"},
	}
	return
}

func NewRuntimeContainerConfig(dropletDir string) (containerConfig *ContainerConfig) {
	containerConfig = &ContainerConfig{
		ContainerName: "cloudfocker-runtime",
		Daemon:        true,
		Mounts: map[string]string{
			dropletDir + "/app": "/app",
		},
		PublishedPorts: map[int]int{8080: 8080},
		EnvVars: map[string]string{
			"HOME":          "/app",
			"TMPDIR":        "/app/tmp",
			"PORT":          "8080",
			"VCAP_SERVICES": vcapServices(dropletDir),
			"DATABASE_URL":  databaseURL(dropletDir),
		},
		ImageTag: "cloudfocker-base:latest",
		Command: append([]string{"/bin/bash", "/app/cloudfocker-start-1c4352a23e52040ddb1857d7675fe3cc.sh", "/app"},
			parseStartCommand(dropletDir)...),
		DropletDir: dropletDir,
	}
	return
}

func vcapServices(dropletDir string) (services string) {
	servicesBytes, err := ioutil.ReadFile(dropletDir + "/app/vcap_services.json")
	if err != nil {
		return
	}
	services = string(servicesBytes)
	return
}

type database struct {
	Credentials struct {
		URI string
	}
}

func databaseURL(dropletDir string) (databaseURL string) {
	servicesBytes, err := ioutil.ReadFile(dropletDir + "/app/vcap_services.json")
	if err != nil {
		return
	}

	var services map[string][]database

	json.Unmarshal(servicesBytes, &services)

	for _, serviceDatabase := range services {
		if len(serviceDatabase) > 0 && serviceDatabase[0].Credentials.URI != "" {
			databaseURL = serviceDatabase[0].Credentials.URI
		}
	}

	return
}

type StagingInfoYml struct {
	DetectedBuildpack string `yaml:"detected_buildpack"`
	StartCommand      string `yaml:"start_command"`
}

type ProcfileYml struct {
	Web string `yaml:"web"`
}

func parseStartCommand(dropletDir string) (startCommand []string) {
	stagingInfoFile, err := os.Open(dropletDir + "/staging_info.yml")
	if err == nil {
		stagingInfo := new(StagingInfoYml)
		decoder := candiedyaml.NewDecoder(stagingInfoFile)
		err = decoder.Decode(stagingInfo)
		if err != nil {
			log.Fatalf("Failed to decode document: %s", err)
		}
		startCommand = strings.Split(stagingInfo.StartCommand, " ")
		if startCommand[0] != "" {
			return
		}
		procfileFile, err := os.Open(dropletDir + "/app/Procfile")
		if err == nil {
			procfileInfo := new(ProcfileYml)
			decoder := candiedyaml.NewDecoder(procfileFile)
			err = decoder.Decode(procfileInfo)
			if err != nil {
				log.Fatalf("Failed to decode document: %s", err)
			}
			startCommand = strings.Split(procfileInfo.Web, " ")
			return
		}
	}
	log.Fatal("Unable to find staging_info.yml")
	return
}
{'fraction_non_alphanumeric': 0.12486867777277502, 'fraction_numerical': 0.011106108359597779, 'mean_word_length': 4.288888888888889, 'pattern_counts': {'":': 12, '<': 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': '327597', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2245, 'n_words': 435}