Files
HellreigN/agent/internal/commander/impl.go
T
shinyzero0 fe7e41e4af
ci-agent / build (push) Failing after 3m4s
fix(commander): missing job id on errors
2026-04-04 19:32:04 +03:00

62 lines
1.3 KiB
Go

package commander
import (
"bytes"
"errors"
"io"
"os/exec"
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/proto/proto"
"golang.org/x/sync/errgroup"
)
type CommandExecutor struct{}
func (*CommandExecutor) Execute(command *proto.Command) (fc *proto.FinishedCommand, err error) {
fc = new(proto.FinishedCommand)
fc.Id = command.Id
cmd := exec.Command(command.Command[0], command.Command[1:]...)
var stdin io.WriteCloser
if command.Stdin != nil {
stdin, err = cmd.StdinPipe()
if err != nil {
return nil, err
}
}
stdout, err1 := cmd.StdoutPipe()
stderr, err2 := cmd.StderrPipe()
if err := errors.Join(err1, err2); err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
if command.Stdin != nil {
io.WriteString(stdin, *command.Stdin)
if err := stdin.Close(); err != nil {
return nil, err
}
}
eg := new(errgroup.Group)
stdoutbuf := new(bytes.Buffer)
stderrbuf := new(bytes.Buffer)
eg.Go(func() error {
_, err := io.Copy(stdoutbuf, stdout)
return err
})
eg.Go(func() error {
_, err := io.Copy(stderrbuf, stderr)
return err
})
if err := cmd.Wait(); err != nil {
return nil, err
}
if err := eg.Wait(); err != nil {
return nil, err
}
fc.Status = int32(cmd.ProcessState.ExitCode())
fc.Stdout = stdoutbuf.String()
fc.Stderr = stderrbuf.String()
return
}