package util

import (
	"bytes"
	"fmt"
	"os/exec"
	"strings"

	// Vendors
	"github.com/Sirupsen/logrus"
)

const CmdNotFound = "Command Not Found"

func ExecuteCommand(command string, args ...string) (string, string) {
	logrus.Debug(command, " ", args)

	cmd := exec.Command(command, args...)
	var out bytes.Buffer
	var errMsg bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &errMsg
	err := cmd.Run()
	if err != nil {
		if strings.Contains(err.Error(), "not found") {
			return "", CmdNotFound
		}
		return "", errMsg.String()
	}
	return out.String(), errMsg.String()
}

func ExecuteCommandString(command string) (string, string) {
	out, errMsg := ExecuteCommand("bash",
		"-c",
		command)

	return out, errMsg
}

func LogError(format string, args ...interface{}) error {
	logrus.Errorf(format, args...)
	return fmt.Errorf(format, args...)
}

