package cmd

import (
	"bytes"
	"io"
	"log"
	"os"
	"os/exec"
)

type cmd struct {
	fullCommand	string
	*exec.Cmd
	bufOut		bytes.Buffer
	bufErr		bytes.Buffer
}

func New(name string, args string) *cmd{
	var bO, bE bytes.Buffer
	c := cmd {
		name + " " + args,
		exec.Command(name, args),
		bO,
		bE,
	}
	c.Stdout = io.MultiWriter(os.Stdout, &c.bufOut)
	c.Stderr = io.MultiWriter(os.Stderr, &c.bufErr)
	return &c
}

func (c *cmd) Execute() {
	err := c.Start()
	if err != nil {
		log.Printf("%s failed with %s\n", c.fullCommand, err)
	}
}

func (c *cmd) GetOut() string {
	return string(c.bufOut.Bytes())
}

func (c *cmd) GetErr() string {
	return string(c.bufErr.Bytes())
}

func (c *cmd) WaitH() {
	err := c.Wait()
	if err != nil {
		log.Printf("%s failed with %s\n", c.fullCommand, err)
	}
}