<commit_before>package dockertest

import (
	"context"
	"errors"
	"io"
	"io/ioutil"
	"strings"

	"github.com/crewjam/errset"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/client"
)

// client.go provides a high level client for interacting with Docker

var (
	// ErrContainerNotFound is returned by GetContainer if we were
	// unable to find the requested c.
	ErrContainerNotFound = errors.New(
		"expected to find exactly one c for the given query")
)

// DockerClient provides a wrapper for the standard dc client
type DockerClient struct {
	Client *client.Client
}

// NewClient produces a new *DockerClient that can be used to interact
// with Docker.
func NewClient() (*DockerClient, error) {
	docker, err := client.NewEnvClient()
	if err != nil {
		return nil, err
	}
	return &DockerClient{Client: docker}, nil
}

// ContainerInfo retrieves a single c by id and returns a *ContainerInfo
// struct.
func (dc *DockerClient) ContainerInfo(ctx context.Context, id string) (*ContainerInfo, error) {
	args := filters.NewArgs()
	args.Add("id", id)
	options := types.ContainerListOptions{Filters: args}
	containers, err := dc.Client.ContainerList(ctx, options)
	if err != nil {
		return nil, err
	}

	if len(containers) == 0 {
		return nil, ErrContainerNotFound
	}

	inspection, err := dc.Client.ContainerInspect(ctx, id)
	if err != nil {
		return nil, err
	}

	return &ContainerInfo{
		Data:  containers[0],
		State: inspection.State, JSON: inspection,
		Warnings: []string{},
	}, nil
}

// ListContainers will return a list of *ContainerInfo structs based on the
// provided input.
func (dc *DockerClient) ListContainers(ctx context.Context, input *ClientInput) ([]*ContainerInfo, error) {
	options := types.ContainerListOptions{
		All:     input.All,
		Since:   input.Since,
		Before:  input.Before,
		Filters: input.FilterArgs(),
	}

	containers, err := dc.Client.ContainerList(ctx, options)
	if err != nil {
		return nil, err
	}

	infos := make(chan *ContainerInfo)
	errs := make(chan error)

	for _, entry := range containers {
		go func(c types.Container) {
			info, err := dc.ContainerInfo(ctx, c.ID)
			if err != nil {
				errs <- err
				return
			}<commit_msg>list all containers, pass client to container info<commit_after>package dockertest

import (
	"context"
	"errors"
	"io"
	"io/ioutil"
	"strings"

	"github.com/crewjam/errset"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/client"
)

// client.go provides a high level client for interacting with Docker

var (
	// ErrContainerNotFound is returned by GetContainer if we were
	// unable to find the requested c.
	ErrContainerNotFound = errors.New("failed to locate the container")
)

// DockerClient provides a wrapper for the standard dc client
type DockerClient struct {
	Client *client.Client
}

// NewClient produces a new *DockerClient that can be used to interact
// with Docker.
func NewClient() (*DockerClient, error) {
	docker, err := client.NewEnvClient()
	if err != nil {
		return nil, err
	}
	return &DockerClient{Client: docker}, nil
}

// ContainerInfo retrieves a single c by id and returns a *ContainerInfo
// struct.
func (dc *DockerClient) ContainerInfo(ctx context.Context, id string) (*ContainerInfo, error) {
	args := filters.NewArgs()
	args.Add("id", id)
	options := types.ContainerListOptions{Filters: args, All: true}
	containers, err := dc.Client.ContainerList(ctx, options)
	if err != nil {
		return nil, err
	}

	if len(containers) == 0 {
		return nil, ErrContainerNotFound
	}

	inspection, err := dc.Client.ContainerInspect(ctx, id)
	if err != nil {
		return nil, err
	}

	return &ContainerInfo{
		Data:  containers[0],
		State: inspection.State, JSON: inspection,
		Warnings: []string{},
		client:   dc,
	}, nil
}

// ListContainers will return a list of *ContainerInfo structs based on the
// provided input.
func (dc *DockerClient) ListContainers(ctx context.Context, input *ClientInput) ([]*ContainerInfo, error) {
	options := types.ContainerListOptions{
		All:     input.All,
		Since:   input.Since,
		Before:  input.Before,
		Filters: input.FilterArgs(),
	}

	containers, err := dc.Client.ContainerList(ctx, options)
	if err != nil {
		return nil, err
	}

	infos := make(chan *ContainerInfo)
	errs := make(chan error)

	for _, entry := range containers {
		go func(c types.Container) {
			info, err := dc.ContainerInfo(ctx, c.ID)
			if err != nil {
				errs <- err
				return
			}
{'fraction_non_alphanumeric': 0.12114115392803917, 'fraction_numerical': 0.0008516074089844582, 'mean_word_length': 3.9766949152542375, 'pattern_counts': {'":': 0, '<': 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': '2292364', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1510, 'n_words': 455}