<commit_before>package utils

import (
	"fmt"
	engineCli "github.com/docker/docker/client"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/mitchellh/mapstructure"
	"github.com/pkg/errors"
	"github.com/rancher/agent/core/progress"
	"github.com/rancher/agent/model"
	"github.com/rancher/agent/utilities/constants"
	revents "github.com/rancher/event-subscriber/events"
	"github.com/rancher/go-rancher/v2"
	"golang.org/x/net/context"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path"
	"regexp"
	"strconv"
	"strings"
	"syscall"
	"time"
)

func GetInstanceAndHost(event *revents.Event) (model.Instance, model.Host, error) {

	data := event.Data
	var ihm model.InstanceHostMap
	if err := mapstructure.Decode(data["instanceHostMap"], &ihm); err != nil {
		return model.Instance{}, model.Host{}, errors.Wrap(err, constants.GetInstanceAndHostError+"failed to marshall instancehostmap")
	}

	var instance model.Instance
	if err := mapstructure.Decode(ihm.Instance, &instance); err != nil {
		return model.Instance{}, model.Host{}, errors.Wrap(err, constants.GetInstanceAndHostError+"failed to marshall instance data")
	}
	var host model.Host
	if err := mapstructure.Decode(ihm.Host, &host); err != nil {
		return model.Instance{}, model.Host{}, errors.Wrap(err, constants.GetInstanceAndHostError+"failed to marshall host data")
	}

	return instance, host, nil
}

func IsNoOp(data model.ProcessData) bool {
	return data.ContainerNoOpEvent
}

func AddLabel(config *container.Config, key string, value string) {
	config.Labels[key] = value
}

func SearchInList(slice []string, target string) bool {
	for _, value := range slice {
		if target == value {
			return true
		}
	}
	return false
}

func IsNonrancherContainer(instance model.Instance) bool {
	return instance.NativeContainer
}

func AddToEnv(config *container.Config, result map[string]string, args ...string) {
	envs := config.Env
	for key, value := range result {
		envs = append(envs, fmt.Sprintf("%v=%v", key, value))
	}
	config.Env = envs
}

func HasKey(m interface{}, key string) bool {
	_, ok := m.(map[string]interface{})[key]
	return ok
}

func HasLabel(instance model.Instance) bool {
	_, ok := instance.Labels[constants.CattelURLLabel]
	return ok
}

func ReadBuffer(reader io.ReadCloser) string {
	buffer := make([]byte, 1024)
	s := ""
	for {
		n, err := reader.Read(buffer)
		s = s + string(buffer[:n])
		if err != nil {
			break<commit_msg>env fix<commit_after>package utils

import (
	"fmt"
	engineCli "github.com/docker/docker/client"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/mitchellh/mapstructure"
	"github.com/pkg/errors"
	"github.com/rancher/agent/core/progress"
	"github.com/rancher/agent/model"
	"github.com/rancher/agent/utilities/constants"
	revents "github.com/rancher/event-subscriber/events"
	"github.com/rancher/go-rancher/v2"
	"golang.org/x/net/context"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path"
	"regexp"
	"strconv"
	"strings"
	"syscall"
	"time"
)

func GetInstanceAndHost(event *revents.Event) (model.Instance, model.Host, error) {

	data := event.Data
	var ihm model.InstanceHostMap
	if err := mapstructure.Decode(data["instanceHostMap"], &ihm); err != nil {
		return model.Instance{}, model.Host{}, errors.Wrap(err, constants.GetInstanceAndHostError+"failed to marshall instancehostmap")
	}

	var instance model.Instance
	if err := mapstructure.Decode(ihm.Instance, &instance); err != nil {
		return model.Instance{}, model.Host{}, errors.Wrap(err, constants.GetInstanceAndHostError+"failed to marshall instance data")
	}
	var host model.Host
	if err := mapstructure.Decode(ihm.Host, &host); err != nil {
		return model.Instance{}, model.Host{}, errors.Wrap(err, constants.GetInstanceAndHostError+"failed to marshall host data")
	}

	return instance, host, nil
}

func IsNoOp(data model.ProcessData) bool {
	return data.ContainerNoOpEvent
}

func AddLabel(config *container.Config, key string, value string) {
	config.Labels[key] = value
}

func SearchInList(slice []string, target string) bool {
	for _, value := range slice {
		if target == value {
			return true
		}
	}
	return false
}

func IsNonrancherContainer(instance model.Instance) bool {
	return instance.NativeContainer
}

func AddToEnv(config *container.Config, result map[string]string, args ...string) {
	envs := config.Env
	existKeys := map[string]struct{}{}
	for _, key := range envs {
		parts := strings.Split(key, "=")
		if len(parts) > 0 {
			existKeys[parts[0]] = struct{}{}
		}
	}
	for key, value := range result {
		if _, ok := existKeys[key]; !ok {
			envs = append(envs, fmt.Sprintf("%v=%v", key, value))
		}
	}
	config.Env = envs
}

func HasKey(m interface{}, key string) bool {
	_, ok := m.(map[string]interface{})[key]
	return ok
}

func HasLabel(instance model.Instance) bool {
	_, ok := instance.Labels[constants.CattelURLLabel]
	return ok
}

func ReadBuffer(reader io.ReadCloser) string {
	buffer := make([]byte, 1024)
	s := ""
	for {
		n, err := reader.Read(buffer)
		s = s + string(buffer[:n])
		if err != nil {
			break
{'fraction_non_alphanumeric': 0.14645173972872028, 'fraction_numerical': 0.0023589541969726755, 'mean_word_length': 4.60352422907489, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4194189', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1778, 'n_words': 393}