<commit_before>package containers

import (
	"context"
	"io"
	"time"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/api/types/strslice"
	"github.com/docker/docker/client"
	"github.com/docker/go-connections/nat"
)

type Container struct {
	Image string
	ID    string
	Name  string
}

func New(
	ctx context.Context,
	image string,
	name string,
	networkName string,
	portMap map[string]string,
) (*Container, error) {
	// Init the env
	cli, err := client.NewEnvClient()
	if err != nil {
		return nil, err
	}

	// Create the port mapping
	portBinding := nat.PortMap{}

	for conPort, hstPort := range portMap {
		hostBinding := nat.PortBinding{
			HostIP:   "0.0.0.0",
			HostPort: hstPort,
		}

		containerPort, err := nat.NewPort("tcp", conPort)
		if err != nil {
			// TODO log error
			continue
		}

		portBinding[containerPort] = []nat.PortBinding{hostBinding}
	}

	// Pull the image
	// TODO This should not be here, should be a pre-requisite
	_, err = cli.ImagePull(
		ctx,
		image,
		types.ImagePullOptions{},
	)
	if err != nil {
		return nil, err<commit_msg>feat(simulation): block container creation until image is pulled<commit_after>package containers

import (
	"context"
	"io"
	"io/ioutil"
	"time"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/api/types/strslice"
	"github.com/docker/docker/client"
	"github.com/docker/go-connections/nat"
)

type Container struct {
	Image string
	ID    string
	Name  string
}

func New(
	ctx context.Context,
	image string,
	name string,
	networkName string,
	portMap map[string]string,
) (*Container, error) {
	// Init the env
	cli, err := client.NewEnvClient()
	if err != nil {
		return nil, err
	}

	// Create the port mapping
	portBinding := nat.PortMap{}

	for conPort, hstPort := range portMap {
		hostBinding := nat.PortBinding{
			HostIP:   "0.0.0.0",
			HostPort: hstPort,
		}

		containerPort, err := nat.NewPort("tcp", conPort)
		if err != nil {
			// TODO log error
			continue
		}

		portBinding[containerPort] = []nat.PortBinding{hostBinding}
	}

	// Pull the image
	// TODO This should not be here, should be a pre-requisite
	imgr, err := cli.ImagePull(
		ctx,
		image,
		types.ImagePullOptions{},
	)
	if err != nil {
		return nil, err
	}
	_, err = io.Copy(ioutil.Discard, imgr)
	if err != nil {
		return nil, err
{'fraction_non_alphanumeric': 0.1316, 'fraction_numerical': 0.0032, 'mean_word_length': 3.4660714285714285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5516564', 'n_tokens_mistral': 977, 'n_tokens_neox': 895, 'n_words': 219}