<commit_before>// package container contains convenience functions for manipulating containers
package container

import (
	"io"
	"log"
	"os"
	"strings"

	"github.com/fsouza/go-dockerclient"
)

const defaultDockerHost = "unix:///var/run/docker.sock"

var DefaultConfig = docker.Config{
	AttachStdin:  true,
	AttachStdout: true,
	AttachStderr: true,
	OpenStdin:    true,
	StdinOnce:    true,
}

func RawStartContainer(opts docker.CreateContainerOptions) (string, error) {
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return "", err
	}
	container, err := client.CreateContainer(opts)
	if err != nil {
		log.Print(err)
		return "", err
	}
	err = client.StartContainer(container.ID, opts.HostConfig)
	if err != nil {
		log.Print(err)
		return "", err
	}

	return container.ID, nil
}

func StartContainer(image string, command []string) (string, error) {
	config := docker.Config{Image: image, Cmd: command}
	opts := docker.CreateContainerOptions{Config: &config}
	return RawStartContainer(opts)
}

func StopContainer(id string) error {
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return err
	}
	return client.StopContainer(id, 5)
}

func KillContainer(id string) error {
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return err
	}
	return client.KillContainer(docker.KillContainerOptions{ID: id})
}

func IpAddr(containerId string) (string, error) {
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return "", err
	}
	container, err := client.InspectContainer(containerId)
	if err != nil {
		log.Print(err)
		return "", err
	}

	return container.NetworkSettings.IPAddress, nil
}

func PullImage(image string) error {
	repo_tag := strings.Split(image, ":")
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return err
	}
	opts := docker.PullImageOptions{Repository: repo_tag[0], Tag: "latest"}
	if len(repo_tag) == 2 {
		opts.Tag = repo_tag[1]
	}
	return client.PullImage(opts, docker.AuthConfiguration{})
}

func PipeToStdin(id string, in io.Reader) error {
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return err
	}
	return client.AttachToContainer(docker.AttachToContainerOptions{
		Container:   id,
		InputStream: in,
		Stdin:       true,
		Stream:      true,
	})
}

func ContainerLogs(id string, out io.Writer) error {
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return err
	}
	return client.AttachToContainer(docker.AttachToContainerOptions{
		Container:    id,
		OutputStream: out,
		ErrorStream:  out,
		Stdout:       true,
		Stderr:       true,
		Logs:         true,
	})
}

func WaitContainer(id string) (int, error) {
	client, err := docker.NewClient(getDockerHost())
	if err != nil {
		log.Print(err)
		return 0, err
	}

	return client.WaitContainer(id)
}

func getDockerHost() (host string) {
	host = os.Getenv("DOCKER_HOST")

	if host == "" {
		host = defaultDockerHost
	}

	return
}<commit_msg>Create a TLS Docker client if needed<commit_after>// package container contains convenience functions for manipulating containers
package container

import (
	"io"
	"log"
	"os"
	"strings"

	"github.com/fsouza/go-dockerclient"
)

const defaultDockerHost = "unix:///var/run/docker.sock"

var DefaultConfig = docker.Config{
	AttachStdin:  true,
	AttachStdout: true,
	AttachStderr: true,
	OpenStdin:    true,
	StdinOnce:    true,
}

func RawStartContainer(opts docker.CreateContainerOptions) (string, error) {
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return "", err
	}
	container, err := client.CreateContainer(opts)
	if err != nil {
		log.Print(err)
		return "", err
	}
	err = client.StartContainer(container.ID, opts.HostConfig)
	if err != nil {
		log.Print(err)
		return "", err
	}

	return container.ID, nil
}

func StartContainer(image string, command []string) (string, error) {
	config := docker.Config{Image: image, Cmd: command}
	opts := docker.CreateContainerOptions{Config: &config}
	return RawStartContainer(opts)
}

func StopContainer(id string) error {
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return err
	}
	return client.StopContainer(id, 5)
}

func KillContainer(id string) error {
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return err
	}
	return client.KillContainer(docker.KillContainerOptions{ID: id})
}

func IpAddr(containerId string) (string, error) {
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return "", err
	}
	container, err := client.InspectContainer(containerId)
	if err != nil {
		log.Print(err)
		return "", err
	}

	return container.NetworkSettings.IPAddress, nil
}

func PullImage(image string) error {
	repo_tag := strings.Split(image, ":")
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return err
	}
	opts := docker.PullImageOptions{Repository: repo_tag[0], Tag: "latest"}
	if len(repo_tag) == 2 {
		opts.Tag = repo_tag[1]
	}
	return client.PullImage(opts, docker.AuthConfiguration{})
}

func PipeToStdin(id string, in io.Reader) error {
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return err
	}
	return client.AttachToContainer(docker.AttachToContainerOptions{
		Container:   id,
		InputStream: in,
		Stdin:       true,
		Stream:      true,
	})
}

func ContainerLogs(id string, out io.Writer) error {
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return err
	}
	return client.AttachToContainer(docker.AttachToContainerOptions{
		Container:    id,
		OutputStream: out,
		ErrorStream:  out,
		Stdout:       true,
		Stderr:       true,
		Logs:         true,
	})
}

func WaitContainer(id string) (int, error) {
	client, err := getNewDockerClient()
	if err != nil {
		log.Print(err)
		return 0, err
	}

	return client.WaitContainer(id)
}

func getNewDockerClient() (*docker.Client, error) {
	if os.Getenv("DOCKER_TLS_VERIFY") == "1" {
		path := os.Getenv("DOCKER_CERT_PATH")

		if path == "" {
			path = "."
		}

		return docker.NewTLSClient(
			getDockerHost(),
			path+"/cert.pem",
			path+"/key.pem",
			path+"/ca.pem",
		)
	}

	return docker.NewClient(getDockerHost())
}

func getDockerHost() (host string) {
	host = os.Getenv("DOCKER_HOST")

	if host == "" {
		host = defaultDockerHost
	}

	return
}
{'fraction_non_alphanumeric': 0.13177570093457944, 'fraction_numerical': 0.0017133956386292835, 'mean_word_length': 4.055905511811024, 'pattern_counts': {'":': 2, '<': 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': '4980515', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2248, 'n_words': 532}