shell/linux/run.go

187 lines
3.8 KiB
Go

package linux
import (
"bufio"
"errors"
"fmt"
"golang.org/x/sys/unix"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
)
func NewCommand(options CommandOptions) (*LinuxCommand, error) {
if len(options.Shell) == 0 {
options.Shell = "/bin/bash"
}
if len(options.Cwd) == 0 {
cwd, err := os.Getwd()
if err != nil {
return nil, ErrFetchingCwd
}
options.Cwd = cwd
}
return &LinuxCommand{
Options: options,
handlers: make(map[int]interface{}),
}, nil
}
func (cmd *LinuxCommand) AddHandler(handler interface{}) error {
switch h := handler.(type) {
case func(data EventOutputData) error:
cmd.handlers[EventOutput] = h
break
default:
return ErrInvalidHandler
}
return nil
}
func (cmd *LinuxCommand) Run() error {
var sourceCommand strings.Builder
for _, value := range cmd.Options.Sources {
sourceCommand.WriteString(fmt.Sprintf("source %s && ", value))
}
var commandOptions strings.Builder
commandOptions.WriteString(" ")
for index, arg := range cmd.Options.Args {
if len(cmd.Options.Args)-1 == index {
commandOptions.WriteString(fmt.Sprintf("%s", arg))
} else {
commandOptions.WriteString(fmt.Sprintf("%s ", arg))
}
}
command := exec.Command(cmd.Options.Shell, "-c", sourceCommand.String()+cmd.Options.Command+commandOptions.String())
command.Dir = cmd.Options.Cwd
var signalChannel chan os.Signal
if len(cmd.handlers) != 0 {
command.SysProcAttr = &unix.SysProcAttr{Setsid: true}
signalChannel = make(chan os.Signal, 1)
signal.Notify(signalChannel, unix.SIGINT, unix.SIGTERM)
var err error
cmd.stdout, err = command.StdoutPipe()
if err != nil {
return err
}
cmd.stdin, err = command.StdinPipe()
if err != nil {
return err
}
cmd.stderr, err = command.StderrPipe()
if err != nil {
return err
}
}
if err := command.Start(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 127 {
return ErrCommandNotFound
} else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok {
return cmd.Options.CustomErrors[int8(exitErr.ExitCode())]
} else {
return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err)
}
}
}
cmd.wg.Add(3)
go func() {
defer cmd.wg.Done()
scanner := bufio.NewScanner(cmd.stderr)
for scanner.Scan() {
line := scanner.Text()
if h, ok := cmd.handlers[EventOutput]; ok {
if err := h.(func(data EventOutputData) error)(EventOutputData{
Output: line,
}); err != nil {
return
}
}
}
}()
go func() {
defer cmd.wg.Done()
scanner := bufio.NewScanner(cmd.stdout)
for scanner.Scan() {
line := scanner.Text()
if h, ok := cmd.handlers[EventOutput]; ok {
if err := h.(func(data EventOutputData) error)(EventOutputData{
Output: line,
}); err != nil {
return
}
}
}
}()
go func() {
defer cmd.wg.Done()
select {
case _, ok := <-signalChannel:
if !ok {
return
}
if err := unix.Kill(-command.Process.Pid, syscall.SIGINT); err != nil {
return
}
}
}()
// Loop through env to format and add them to the command.
for key, value := range cmd.Options.Env {
command.Env = append(command.Env, fmt.Sprintf("%s=%s", key, value))
}
isCommandExecutable, err := cmd.isCommandExecutable(cmd.Options.Command)
if err != nil {
return err
}
if !isCommandExecutable {
return ErrCommandNotExecutable
}
if err := command.Wait(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 127 {
return ErrCommandNotFound
} else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok {
return cmd.Options.CustomErrors[int8(exitErr.ExitCode())]
} else {
return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err)
}
}
}
close(signalChannel)
signal.Stop(signalChannel)
cmd.wg.Wait()
return nil
}