<commit_before>package forge

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/sclevine/forge/engine"
	"github.com/sclevine/forge/engine/docker/term"
)

const runScript = `
	set -eo pipefail
	if [[ -d /tmp/local ]]; then
		rsync -a --delete /tmp/local/ /home/vcap/app
    fi
	exec /packs/launcher "$1"
`

var bytesPattern = regexp.MustCompile(`(?i)^(-?\d+)([KMGT])B?$`)

type Runner struct {
	Logs   io.Writer
	TTY    engine.TTY
	Loader Loader
	engine Engine
}

type RunConfig struct {
	Droplet       engine.Stream
	Stack         string
	AppDir        string
	Shell         bool
	Restart       <-chan time.Time
	Color         Colorizer
	AppConfig     *AppConfig
	NetworkConfig *NetworkConfig
}

func NewRunner(engine Engine) *Runner {
	return &Runner{
		Logs: os.Stdout,
		TTY: &term.TTY{
			In:  os.Stdin,
			Out: os.Stdout,
		},
		Loader: noopLoader{},
		engine: engine,
	}
}

func (r *Runner) Run(config *RunConfig) (status int64, err error) {
	if err := r.pull(config.Stack); err != nil {
		return 0, err
	}

	var binds []string
	if config.AppDir != "" {
		binds = []string{config.AppDir + ":/tmp/local"}
	}
	containerConfig, err := r.buildConfig(config.AppConfig, config.NetworkConfig, binds, config.Stack)
	if err != nil {
		return 0, err
	}
	contr, err := r.engine.NewContainer(containerConfig)
	if err != nil {
		return 0, err
	}
	defer contr.Close()

	if err := contr.StreamTarTo(config.Droplet, "/home/vcap"); err != nil {
		return 0, err
	}
	color := config.Color("[%s] ", config.AppConfig.Name)
	if !config.Shell {
		return contr.Start(color, r.Logs, config.Restart)
	}
	if err := contr.Background(); err != nil {
		return 0, err
	}
	return 0, contr.Shell(r.TTY, "/packs/shell")<commit_msg>Added new configuration options for runner<commit_after>package forge

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/sclevine/forge/engine"
	"github.com/sclevine/forge/engine/docker/term"
)

const runScript = `
	set -eo pipefail
	if [[ -d /tmp/local ]]; then
		rsync -a --delete /tmp/local/ /home/vcap/app
    fi
	exec /packs/launcher "$1"
`

var bytesPattern = regexp.MustCompile(`(?i)^(-?\d+)([KMGT])B?$`)

type Runner struct {
	Logs   io.Writer
	TTY    engine.TTY
	Loader Loader
	engine Engine
}

type RunConfig struct {
	Droplet       engine.Stream
	Stack         string
	AppDir        string
	Shell         bool
	Restart       <-chan time.Time
	Color         Colorizer
	AppConfig     *AppConfig
	NetworkConfig *NetworkConfig
	SkipStackPull bool
	HomeDir       string
}

func NewRunner(engine Engine) *Runner {
	return &Runner{
		Logs: os.Stdout,
		TTY: &term.TTY{
			In:  os.Stdin,
			Out: os.Stdout,
		},
		Loader: noopLoader{},
		engine: engine,
	}
}

func (r *Runner) Run(config *RunConfig) (status int64, err error) {
	if config.SkipStackPull == false {
		if err := r.pull(config.Stack); err != nil {
			return 0, err
		}
	}

	var binds []string
	if config.AppDir != "" {
		binds = []string{config.AppDir + ":/tmp/local"}
	}
	containerConfig, err := r.buildConfig(config.AppConfig, config.NetworkConfig, binds, config.Stack)
	if err != nil {
		return 0, err
	}
	contr, err := r.engine.NewContainer(containerConfig)
	if err != nil {
		return 0, err
	}
	defer contr.Close()

	homeDir := config.HomeDir
	if homeDir == "" {
		homeDir = "/home/vcap"
	}
	if err := contr.StreamTarTo(config.Droplet, homeDir); err != nil {
		return 0, err
	}
	color := config.Color("[%s] ", config.AppConfig.Name)
	if !config.Shell {
		return contr.Start(color, r.Logs, config.Restart)
	}
	if err := contr.Background(); err != nil {
		return 0, err
	}
	return 0, contr.Shell(r.TTY, "/packs/shell")
{'fraction_non_alphanumeric': 0.14618931380526173, 'fraction_numerical': 0.004882017900732303, 'mean_word_length': 3.1531531531531534, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, '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': '1288875', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1397, 'n_words': 339}