<commit_before>package client

import (
	"os"
	"os/exec"
	"runtime"

	log "github.com/Sirupsen/logrus"
	"github.com/docker/docker/client"
	"github.com/maliceio/go-plugin-utils/utils"
	"github.com/maliceio/malice/config"
	er "github.com/maliceio/malice/malice/errors"
	"golang.org/x/net/context"
)

// NOTE: https://github.com/eris-ltd/eris-cli/blob/master/perform/docker_run.go

// Docker is the Malice docker client
type Docker struct {
	Client *client.Client
	ip     string
	port   string
}

// NewDockerClient creates a new Docker Client
func NewDockerClient() *Docker {
	var docker *client.Client
	var ip, port string
	var err error

	switch os := runtime.GOOS; os {
	case "linux":
		log.Debug("Running inside Docker...")
		log.Debug("Creating NewClient...")
		proto, addr, basePath, err := client.ParseHost("unix:///var/run/docker.sock")
		log.Debug("Proto: ", proto, ", Addr: ", addr, ", BasePath: ", basePath, ", Error: ", err)
		defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
		docker, err = client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
		ip = "localhost"
		port = "2375"
	case "darwin":
		log.Debug("Running inside Docker for Mac...")
		log.Debug("Creating NewClient...")
		proto, addr, basePath, err := client.ParseHost("unix:///var/run/docker.sock")
		log.Debug("Proto: ", proto, ", Addr: ", addr, ", BasePath: ", basePath, ", Error: ", err)
		defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
		docker, err = client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
		ip = "localhost"
		port = "2375"
	case "windows":
		log.Debug("Creating NewEnvClient...")
		docker, err = client.NewEnvClient()
		if err != nil {
			log.Fatal(err)
		}
		ip, port, err = parseDockerEndoint(utils.Getopt("DOCKER_HOST", config.Conf.Docker.EndPoint))
	default:
		log.Debug("Creating NewEnvClient...")
		docker, err = client.NewEnvClient()
		if err != nil {
			log.Fatal(err)
		}
		ip, port, err = parseDockerEndoint(utils.Getopt("DOCKER_HOST", config.Conf.Docker.EndPoint))
	}
	if err != nil {
		log.Fatal(err)
	}
	// Check if client can connect
	log.Debug("Docker Info...")
	if _, err = docker.Info(context.Background()); err != nil {
		log.Debug("Docker Info FAILED...")
		handleClientError(err)
	} else {
		log.WithFields(log.Fields{"ip": ip, "port": port}).Debug("Connected to docker daemon client")
	}

	log.Debug("Docker Info...")
	if _, err = docker.Info(context.Background()); err != nil {
		log.Debug("Docker Info FAILED...")
		handleClientError(err)
	} else {
		log.WithFields(log.Fields{"ip": ip, "port": port}).Debug("Connected to docker daemon client")
	}

	log.Debug("Docker Info...")
	if _, err = docker.Info(context.Background()); err != nil {
		log.Debug("Docker Info FAILED...")
		handleClientError(err)
	} else {
		log.WithFields(log.Fields{"ip": ip, "port": port}).Debug("Connected to docker daemon client")
	}

	return &Docker{
		Client: docker,
		ip:     ip,
		port:   port,
	}

	// if _, found := os.LookupEnv("MALICE_IN_DOCKER"); found {

	// } else {
	// 	log.Debug("Running outside Docker...")
	// 	log.Debug("Creating NewClient...")
	// 	defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
	// 	docker, err = client.NewClient("unix:///var/run/docker.sock", "v1.23", nil, defaultHeaders)
	// 	if err != nil {
	// 		log.Fatal(err)
	// 	}
	// 	// Check if client can connect
	// 	log.Debug("Docker Info2...")
	// 	if _, err = docker.Info(context.Background()); err != nil {
	// 		// If failed to connect try to create docker client via socket
	// 		log.Debug("Docker Info2 FAILED...")
	// 		log.Debug("Creating NewEnvClient...")
	// 		docker, err = client.NewEnvClient()
	// 		if err != nil {
	// 			log.Fatal(err)
	// 		}
	// 	} else {
	// 		_, _, _, err := client.ParseHost(utils.Getopt("DOCKER_HOST", client.DefaultDockerHost))
	// 		if err != nil {
	// 			log.Error(err)
	// 		}
	// 		ip, port, err = parseDockerEndoint(utils.Getopt("DOCKER_HOST", config.Conf.Docker.EndPoint))
	// 		if err != nil {
	// 			log.Error(err)
	// 		}
	// 		log.WithFields(log.Fields{"ip": ip, "port": port}).Debug("Connected to docker daemon with docker-machine")
	// 	}
	// }

	// return &Docker{
	// 	Client: docker,
	// 	ip:     ip,
	// 	port:   port,
	// }
}

// GetIP returns IP of docker client
func (docker *Docker) GetIP() string {
	return docker.ip
}

// Ping pings docker client to see if it is up or not by checking Info.
func (docker *Docker) Ping() bool {
	// ctx, cancel := context.WithTimeout(context.Background(), config.Conf.Docker.Timeout*time.Second)
	// defer cancel()

	_, err := docker.Client.Info(context.Background())
	if err != nil {
		er.CheckError(err)
		return false
	}
	return true
}

// TODO: Make this betta MUCHO betta
func handleClientError(dockerError error) {
	if dockerError != nil {
		log.WithFields(log.Fields{"env": config.Conf.Environment.Run}).Error("Unable to connect to docker client")
		switch runtime.GOOS {
		case "darwin":
			if _, err := exec.LookPath("docker-machine"); err != nil {<commit_msg>BUG?!<commit_after>package client

import (
	"os"
	"os/exec"
	"runtime"

	log "github.com/Sirupsen/logrus"
	"github.com/docker/docker/client"
	"github.com/maliceio/go-plugin-utils/utils"
	"github.com/maliceio/malice/config"
	er "github.com/maliceio/malice/malice/errors"
	"golang.org/x/net/context"
)

// NOTE: https://github.com/eris-ltd/eris-cli/blob/master/perform/docker_run.go

// Docker is the Malice docker client
type Docker struct {
	Client *client.Client
	ip     string
	port   string
}

// NewDockerClient creates a new Docker Client
func NewDockerClient() *Docker {
	var docker *client.Client
	var ip, port string
	var err error

	switch os := runtime.GOOS; os {
	case "linux":
		log.Debug("Running inside Docker...")
		log.Debug("Creating NewClient...")
		proto, addr, basePath, err := client.ParseHost("unix:///var/run/docker.sock")
		log.Debug("Proto: ", proto, ", Addr: ", addr, ", BasePath: ", basePath, ", Error: ", err)
		defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
		docker, err = client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
		ip = "localhost"
		port = "2375"
	case "darwin":
		log.Debug("Running inside Docker for Mac...")
		log.Debug("Creating NewClient...")
		proto, addr, basePath, err := client.ParseHost("unix:///var/run/docker.sock")
		log.Debug("Proto: ", proto, ", Addr: ", addr, ", BasePath: ", basePath, ", Error: ", err)
		defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
		docker, err = client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
		ip = "localhost"
		port = "2375"
	case "windows":
		log.Debug("Creating NewEnvClient...")
		docker, err = client.NewEnvClient()
		if err != nil {
			log.Fatal(err)
		}
		ip, port, err = parseDockerEndoint(utils.Getopt("DOCKER_HOST", config.Conf.Docker.EndPoint))
	default:
		log.Debug("Creating NewEnvClient...")
		docker, err = client.NewEnvClient()
		if err != nil {
			log.Fatal(err)
		}
		ip, port, err = parseDockerEndoint(utils.Getopt("DOCKER_HOST", config.Conf.Docker.EndPoint))
	}
	if err != nil {
		log.Fatal(err)
	}
	// Check if client can connect
	log.Debug("Docker Info...")
	if _, err = docker.Info(context.Background()); err != nil {
		log.Debug("Docker Info FAILED...")
		handleClientError(err)
	} else {
		log.WithFields(log.Fields{"ip": ip, "port": port}).Debug("Connected to docker daemon client")
	}

	log.Debug("Docker Info...")
	if _, err = docker.Info(context.Background()); err != nil {
		log.Debug("Docker Info FAILED...")
		handleClientError(err)
	} else {
		log.WithFields(log.Fields{"ip": ip, "port": port}).Debug("Connected to docker daemon client")
	}

	log.Debug("Docker Info...")
	if _, err = docker.Info(context.Background()); err != nil {
		log.Debug("Docker Info FAILED...")
		er.CheckError(err)
		handleClientError(err)
	} else {
		log.WithFields(log.Fields{"ip": ip, "port": port}).Debug("Connected to docker daemon client")
	}

	return &Docker{
		Client: docker,
		ip:     ip,
		port:   port,
	}
}

// GetIP returns IP of docker client
func (docker *Docker) GetIP() string {
	return docker.ip
}

// Ping pings docker client to see if it is up or not by checking Info.
func (docker *Docker) Ping() bool {
	// ctx, cancel := context.WithTimeout(context.Background(), config.Conf.Docker.Timeout*time.Second)
	// defer cancel()

	_, err := docker.Client.Info(context.Background())
	if err != nil {
		er.CheckError(err)
		return false
	}
	return true
}

// TODO: Make this betta MUCHO betta
func handleClientError(dockerError error) {
	if dockerError != nil {
		log.WithFields(log.Fields{"env": config.Conf.Environment.Run}).Error("Unable to connect to docker client")
		switch runtime.GOOS {
		case "darwin":
			if _, err := exec.LookPath("docker-machine"); err != nil {
{'fraction_non_alphanumeric': 0.17546039995480736, 'fraction_numerical': 0.004858208112077731, 'mean_word_length': 3.5889061689994817, 'pattern_counts': {'":': 29, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '496031', 'n_tokens_mistral': 3394, 'n_tokens_neox': 3140, 'n_words': 707}