package connection

import (
	"os"
	"os/exec"
)

func (c Connection) RunSync(command string, args ...string) ([]byte, error) {
	fc := append([]string{"-s", c.Device().Serial(), command}, args...)
	return exec.Command(c.Adb().BinPath(), fc...).CombinedOutput()
}

func (c Connection) StartAsync(command string, args ...string) (*exec.Cmd, error) {
	fc := append([]string{"-s", c.Device().Serial(), command}, args...)
	cmd := exec.Command(c.Adb().BinPath(), fc...)
	return cmd, cmd.Start()
}

func (c Connection) StopAsync(cmd *exec.Cmd) error {
	if err := cmd.Process.Signal(os.Interrupt); err != nil {
		return err
	}
	if err := cmd.Process.Release(); err == nil || err.Error() == "signal: interrupt" {
		return nil
	} else {
		return err
	}
}
