<commit_before>package dev

import (
	"fmt"
	"net"

	"github.com/jcelliott/lumber"
	"github.com/nanobox-io/golang-docker-client"
	"github.com/nanobox-io/nanobox-boxfile"

	container_generator "github.com/nanobox-io/nanobox/generators/containers"
	build_generator "github.com/nanobox-io/nanobox/generators/hooks/build"

	"github.com/nanobox-io/nanobox/models"
	"github.com/nanobox-io/nanobox/processors/env"
	"github.com/nanobox-io/nanobox/util/console"
	"github.com/nanobox-io/nanobox/util/dhcp"
	"github.com/nanobox-io/nanobox/util/display"
	"github.com/nanobox-io/nanobox/util/hookit"
	"github.com/nanobox-io/nanobox/util/locker"
	"github.com/nanobox-io/nanobox/util/provider"
	"github.com/nanobox-io/nanobox/util/watch"
)

// Start a dev container
func Console(envModel *models.Env, appModel *models.App, devRun bool) error {

	// ensure the environment is setup
	if err := env.Setup(envModel); err != nil {
		return fmt.Errorf("failed to setup environment: %s", err.Error())
	}

	// whatever happens next, ensure we teardown this container

	// setup the dev container
	if err := setup(appModel); err != nil {
		return fmt.Errorf("failed to setup dev container: %s", err.Error())
	}

	// start a watcher to watch for changes and inform the vm
	watchFiles(envModel, appModel)

	// if run then start the run commands
	if devRun {
		if err := Run(appModel); err != nil {
			return fmt.Errorf("failed to run your start commands: %s", err)
		}
	} else {
		// console into the newly created container
		if err := runConsole(appModel); err != nil {
			return fmt.Errorf("failed to console into dev container: %s", err.Error())
		}

	}

	if err := teardown(appModel); err != nil {
		return fmt.Errorf("unable to teardown dev: %s", err)
	}

	return nil
}

// sets up the dev container and network stack
func setup(appModel *models.App) error {

	// establish a local lock to ensure we're the only ones bringing up the
	// dev container. Also, we need to ensure the lock is released even in we error
	locker.LocalLock()
	defer locker.LocalUnlock()

	// we don't need to setup if dev is already running
	if isDevRunning() {
		return nil<commit_msg>Remove an old container after a crash fixes #247<commit_after>package dev

import (
	"fmt"
	"net"

	"github.com/jcelliott/lumber"
	"github.com/nanobox-io/golang-docker-client"
	"github.com/nanobox-io/nanobox-boxfile"

	container_generator "github.com/nanobox-io/nanobox/generators/containers"
	build_generator "github.com/nanobox-io/nanobox/generators/hooks/build"

	"github.com/nanobox-io/nanobox/models"
	"github.com/nanobox-io/nanobox/processors/env"
	"github.com/nanobox-io/nanobox/util/console"
	"github.com/nanobox-io/nanobox/util/dhcp"
	"github.com/nanobox-io/nanobox/util/display"
	"github.com/nanobox-io/nanobox/util/hookit"
	"github.com/nanobox-io/nanobox/util/locker"
	"github.com/nanobox-io/nanobox/util/provider"
	"github.com/nanobox-io/nanobox/util/watch"
)

// Start a dev container
func Console(envModel *models.Env, appModel *models.App, devRun bool) error {

	// ensure the environment is setup
	if err := env.Setup(envModel); err != nil {
		return fmt.Errorf("failed to setup environment: %s", err.Error())
	}

	// whatever happens next, ensure we teardown this container

	// setup the dev container
	if err := setup(appModel); err != nil {
		return fmt.Errorf("failed to setup dev container: %s", err.Error())
	}

	// start a watcher to watch for changes and inform the vm
	watchFiles(envModel, appModel)

	// if run then start the run commands
	if devRun {
		if err := Run(appModel); err != nil {
			return fmt.Errorf("failed to run your start commands: %s", err)
		}
	} else {
		// console into the newly created container
		if err := runConsole(appModel); err != nil {
			return fmt.Errorf("failed to console into dev container: %s", err.Error())
		}

	}

	if err := teardown(appModel); err != nil {
		return fmt.Errorf("unable to teardown dev: %s", err)
	}

	return nil
}

// sets up the dev container and network stack
func setup(appModel *models.App) error {

	// establish a local lock to ensure we're the only ones bringing up the
	// dev container. Also, we need to ensure the lock is released even in we error
	locker.LocalLock()
	defer locker.LocalUnlock()

	// we don't need to setup if dev is already running
	if isDevRunning() {
		if devInUse(container_generator.DevName()) {
			return nil
		} else {
			// if it isnt being used lets wipe it out and start again
			teardown(appModel)
		}
{'fraction_non_alphanumeric': 0.12413174994398386, 'fraction_numerical': 0.0006721935917544253, 'mean_word_length': 3.841648590021692, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6242303', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1491, 'n_words': 429}