<commit_before>package dockerutil

import (
	"bufio"
	"bytes"
	"fmt"
	exec2 "github.com/drud/ddev/pkg/exec"
	"github.com/drud/ddev/pkg/fileutil"
	"github.com/drud/ddev/pkg/globalconfig"
	"io"
	"log"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/drud/ddev/pkg/archive"
	"github.com/drud/ddev/pkg/nodeps"
	"github.com/drud/ddev/pkg/util"
	"github.com/drud/ddev/pkg/version"

	"net/url"

	"github.com/Masterminds/semver/v3"
	"github.com/drud/ddev/pkg/output"
	docker "github.com/fsouza/go-dockerclient"
)

// NetName provides the default network name for ddev.
const NetName = "ddev_default"

// EnsureNetwork will ensure the docker network for ddev is created.
func EnsureNetwork(client *docker.Client, name string) error {
	if !NetExists(client, name) {
		netOptions := docker.CreateNetworkOptions{
			Name:     name,
			Driver:   "bridge",
			Internal: false,
		}
		_, err := client.CreateNetwork(netOptions)
		if err != nil {
			return err
		}
		output.UserOut.Println("Network", name, "created")

	}
	return nil
}

// EnsureDdevNetwork just creates or ensures the ddev network exists or
// exits with fatal.
func EnsureDdevNetwork() {
	// ensure we have the fallback global ddev network
	client := GetDockerClient()
	err := EnsureNetwork(client, NetName)
	if err != nil {
		log.Fatalf("Failed to ensure docker network %s: %v", NetName, err)
	}
}

// NetworkExists returns true if the named network exists
// Mostly intended for tests
func NetworkExists(netName string) bool {
	// ensure we have docker network
	client := GetDockerClient()
	return NetExists(client, strings.ToLower(netName))
}

// RemoveNetwork removes the named docker network
func RemoveNetwork(netName string) error {
	client := GetDockerClient()
	err := client.RemoveNetwork(netName)
	return err
}

var dockerHost string

// GetDockerClient returns a docker client respecting the current docker context
// but DOCKER_HOST gets priority
func GetDockerClient() *docker.Client {
	var err error

	// This is a cheap way of using docker contexts by running `docker context inspect`
	// I would wish for something far better, but trying to transplant the code from
	// docker/cli did not succeed. rfay 2021-12-16
	// `docker context inspect` will already respect $DOCKER_CONTEXT so we don't have to do that.
	// This section is skipped anyway if $DOCKER_HOST is set
	if dockerHost == "" {
		contextInfo, err := exec2.RunHostCommand("docker", "context", "inspect", "-f", `{{ .Name }} {{ .Endpoints.docker.Host }}`)
		if err != nil {
			util.Warning("unable to run docker context inspect: %v", err)
		} else {
			contextInfo = strings.Trim(contextInfo, " \r\n")
			parts := strings.SplitN(contextInfo, " ", 2)
			if len(parts) != 2 {
				util.Warning("unable to run split docker context info %s: %v", contextInfo, err)
			}
			dockerHost = parts[1]
			util.Debug("Using docker context %s (%v)", parts[0], dockerHost)
		}
	}
	// Respect DOCKER_HOST in case it's set
	if os.Getenv("DOCKER_HOST") == "" {<commit_msg>Don't let ddev continue processing if docker context inspect doesn't work, fixes #3548 (#3605)<commit_after>package dockerutil

import (
	"bufio"
	"bytes"
	"fmt"
	exec2 "github.com/drud/ddev/pkg/exec"
	"github.com/drud/ddev/pkg/fileutil"
	"github.com/drud/ddev/pkg/globalconfig"
	"io"
	"log"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/drud/ddev/pkg/archive"
	"github.com/drud/ddev/pkg/nodeps"
	"github.com/drud/ddev/pkg/util"
	"github.com/drud/ddev/pkg/version"

	"net/url"

	"github.com/Masterminds/semver/v3"
	"github.com/drud/ddev/pkg/output"
	docker "github.com/fsouza/go-dockerclient"
)

// NetName provides the default network name for ddev.
const NetName = "ddev_default"

// EnsureNetwork will ensure the docker network for ddev is created.
func EnsureNetwork(client *docker.Client, name string) error {
	if !NetExists(client, name) {
		netOptions := docker.CreateNetworkOptions{
			Name:     name,
			Driver:   "bridge",
			Internal: false,
		}
		_, err := client.CreateNetwork(netOptions)
		if err != nil {
			return err
		}
		output.UserOut.Println("Network", name, "created")

	}
	return nil
}

// EnsureDdevNetwork just creates or ensures the ddev network exists or
// exits with fatal.
func EnsureDdevNetwork() {
	// ensure we have the fallback global ddev network
	client := GetDockerClient()
	err := EnsureNetwork(client, NetName)
	if err != nil {
		log.Fatalf("Failed to ensure docker network %s: %v", NetName, err)
	}
}

// NetworkExists returns true if the named network exists
// Mostly intended for tests
func NetworkExists(netName string) bool {
	// ensure we have docker network
	client := GetDockerClient()
	return NetExists(client, strings.ToLower(netName))
}

// RemoveNetwork removes the named docker network
func RemoveNetwork(netName string) error {
	client := GetDockerClient()
	err := client.RemoveNetwork(netName)
	return err
}

var dockerHost string

// GetDockerClient returns a docker client respecting the current docker context
// but DOCKER_HOST gets priority
func GetDockerClient() *docker.Client {
	var err error

	// This is a cheap way of using docker contexts by running `docker context inspect`
	// I would wish for something far better, but trying to transplant the code from
	// docker/cli did not succeed. rfay 2021-12-16
	// `docker context inspect` will already respect $DOCKER_CONTEXT so we don't have to do that.
	// This section is skipped anyway if $DOCKER_HOST is set
	if dockerHost == "" {
		contextInfo, err := exec2.RunHostCommand("docker", "context", "inspect", "-f", `{{ .Name }} {{ .Endpoints.docker.Host }}`)
		if err != nil {
			util.Failed("unable to run 'docker context inspect' - please make sure docker client is in path and up-to-date: %v", err)
		} else {
			contextInfo = strings.Trim(contextInfo, " \r\n")
			parts := strings.SplitN(contextInfo, " ", 2)
			if len(parts) != 2 {
				util.Warning("unable to run split docker context info %s: %v", contextInfo, err)
			}
			dockerHost = parts[1]
			util.Debug("Using docker context %s (%v)", parts[0], dockerHost)
		}
	}
	// Respect DOCKER_HOST in case it's set
	if os.Getenv("DOCKER_HOST") == "" {
{'fraction_non_alphanumeric': 0.12389664580324186, 'fraction_numerical': 0.0060985395602632005, 'mean_word_length': 3.9381933438985737, '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': '4527793', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2088, 'n_words': 627}