<commit_before>// Package utils contains commonly useful functions from Deisctl

package utils

import (
	_ "bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"syscall"
	"time"

	"github.com/deis/deisctl/constant"
	"github.com/docker/docker/api/client"
	uuid "github.com/satori/go.uuid"
)

// NewUuid returns a new V4-style unique identifier.
func NewUuid() string {
	u1 := uuid.NewV4()
	s1 := fmt.Sprintf("%s", u1)
	return strings.Split(s1, "-")[0]
}

func GetNewClient() (
	cli *client.DockerCli, stdout *io.PipeReader, stdoutPipe *io.PipeWriter) {
	testDaemonAddr := "/var/run/docker.sock"
	testDaemonProto := "unix"
	stdout, stdoutPipe = io.Pipe()
	cli = client.NewDockerCli(
		nil, stdoutPipe, nil, testDaemonProto, testDaemonAddr, nil)
	return
}

func PullImage(cli *client.DockerCli, args ...string) error {
	fmt.Println("pulling image : " + args[0])
	err := cli.CmdPull(args...)
	return err
}

func GetServices() []string {
	service := []string{
		"deis-builder.service",
		"deis-builder-data.service",
		"deis-cache.service",
		"deis-controller.service",
		"deis-database.service",
		"deis-database-data.service",
		"deis-logger.service",
		"deis-logger-data.service",
		"deis-registry.service",
		"deis-registry-data.service",
		"deis-router.service",
	}
	return service
}

// getClientID returns the CoreOS Machine ID or an unknown UUID string
func GetClientID() string {
	machineID := GetMachineID("/")
	if machineID == "" {
		return fmt.Sprintf("{unknown-" + utils.NewUuid() + "}")
	}
	return machineID
}

func GetMachineID(root string) string {
	fullPath := filepath.Join(root, constant.MachineId)
	id, err := ioutil.ReadFile(fullPath)
	if err != nil {
		return ""
	}
	return strings.TrimSpace(string(id))
}

func GetVersion() string {
	id, err := ioutil.ReadFile(constant.Version)
	if err != nil {
		return "0.0.0"
	}
	return strings.TrimSpace(string(id))
}

// GetFileBytes returns a byte array of the contents of a file.
func GetFileBytes(filename string) []byte {
	file, _ := os.Open(filename)
	defer file.Close()
	stat, _ := file.Stat()
	bs := make([]byte, stat.Size())
	_, _ = file.Read(bs)<commit_msg>fix(deisctl): fix utils error<commit_after>// Package utils contains commonly useful functions from Deisctl

package utils

import (
	_ "bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"syscall"
	"time"

	"github.com/deis/deisctl/constant"
	"github.com/docker/docker/api/client"
	uuid "github.com/satori/go.uuid"
)

// NewUuid returns a new V4-style unique identifier.
func NewUuid() string {
	u1 := uuid.NewV4()
	s1 := fmt.Sprintf("%s", u1)
	return strings.Split(s1, "-")[0]
}

func GetNewClient() (
	cli *client.DockerCli, stdout *io.PipeReader, stdoutPipe *io.PipeWriter) {
	testDaemonAddr := "/var/run/docker.sock"
	testDaemonProto := "unix"
	stdout, stdoutPipe = io.Pipe()
	cli = client.NewDockerCli(
		nil, stdoutPipe, nil, testDaemonProto, testDaemonAddr, nil)
	return
}

func PullImage(cli *client.DockerCli, args ...string) error {
	fmt.Println("pulling image : " + args[0])
	err := cli.CmdPull(args...)
	return err
}

func GetServices() []string {
	service := []string{
		"deis-builder.service",
		"deis-builder-data.service",
		"deis-cache.service",
		"deis-controller.service",
		"deis-database.service",
		"deis-database-data.service",
		"deis-logger.service",
		"deis-logger-data.service",
		"deis-registry.service",
		"deis-registry-data.service",
		"deis-router.service",
	}
	return service
}

// getClientID returns the CoreOS Machine ID or an unknown UUID string
func GetClientID() string {
	machineID := GetMachineID("/")
	if machineID == "" {
		return fmt.Sprintf("{unknown-" + NewUuid() + "}")
	}
	return machineID
}

func GetMachineID(root string) string {
	fullPath := filepath.Join(root, constant.MachineId)
	id, err := ioutil.ReadFile(fullPath)
	if err != nil {
		return ""
	}
	return strings.TrimSpace(string(id))
}

func GetVersion() string {
	id, err := ioutil.ReadFile(constant.Version)
	if err != nil {
		return "0.0.0"
	}
	return strings.TrimSpace(string(id))
}

// GetFileBytes returns a byte array of the contents of a file.
func GetFileBytes(filename string) []byte {
	file, _ := os.Open(filename)
	defer file.Close()
	stat, _ := file.Stat()
	bs := make([]byte, stat.Size())
	_, _ = file.Read(bs)
{'fraction_non_alphanumeric': 0.15976058931860038, 'fraction_numerical': 0.0050644567219152855, 'mean_word_length': 4.166468489892985, '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': '6367030', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1649, 'n_words': 358}