Return errors, rather than printing them; move PrintError() to front package

This commit is contained in:
2025-06-01 13:45:32 -04:00
parent e2e64d7fa4
commit 799fafafe9
4 changed files with 36 additions and 31 deletions
+4 -3
View File
@@ -2,24 +2,25 @@ package back
import (
"bufio"
"errors"
"io"
"os"
"os/exec"
)
// WriteToStdin is a utility function that writes a string to a command's stdin.
func WriteToStdin(cmd *exec.Cmd, input string) {
func WriteToStdin(cmd *exec.Cmd, input string) error {
stdin, err := cmd.StdinPipe()
if err != nil {
PrintError("Failed to access stdin for system command: "+err.Error(), ErrorOther, true)
return errors.New("unable to access stdin for system command: " + err.Error())
}
go func() {
defer func(stdin io.WriteCloser) {
_ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed
}(stdin)
_, _ = io.WriteString(stdin, input)
}()
return nil
}
// ReadFromStdin is a utility function that reads a string from stdin.