mirror of
https://github.com/rwinkhart/go-boilerplate.git
synced 2026-09-04 08:07:17 -04:00
Return errors, rather than printing them; move PrintError() to front package
This commit is contained in:
+4
-3
@@ -2,24 +2,25 @@ package back
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteToStdin is a utility function that writes a string to a command's stdin.
|
// 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()
|
stdin, err := cmd.StdinPipe()
|
||||||
if err != nil {
|
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() {
|
go func() {
|
||||||
defer func(stdin io.WriteCloser) {
|
defer func(stdin io.WriteCloser) {
|
||||||
_ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed
|
_ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed
|
||||||
}(stdin)
|
}(stdin)
|
||||||
_, _ = io.WriteString(stdin, input)
|
_, _ = io.WriteString(stdin, input)
|
||||||
}()
|
}()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFromStdin is a utility function that reads a string from stdin.
|
// ReadFromStdin is a utility function that reads a string from stdin.
|
||||||
|
|||||||
+11
-10
@@ -1,6 +1,7 @@
|
|||||||
package back
|
package back
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -8,34 +9,34 @@ import (
|
|||||||
// TargetIsFile checks if the targetLocation is a file, directory, or is inaccessible.
|
// TargetIsFile checks if the targetLocation is a file, directory, or is inaccessible.
|
||||||
// Requires: failCondition (0 = fail on inaccessible, 1 = fail on inaccessible&file, 2 = fail on inaccessible&directory).
|
// Requires: failCondition (0 = fail on inaccessible, 1 = fail on inaccessible&file, 2 = fail on inaccessible&directory).
|
||||||
// Returns: isFile, isAccessible.
|
// Returns: isFile, isAccessible.
|
||||||
func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8) (bool, bool) {
|
func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8) (bool, bool, error) {
|
||||||
targetInfo, err := os.Stat(targetLocation)
|
targetInfo, err := os.Stat(targetLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errorOnFail {
|
if errorOnFail {
|
||||||
PrintError("Failed to access \""+targetLocation+"\" - Ensure it exists and has the correct permissions", ErrorTargetNotFound, true)
|
return false, false, errors.New("unable to access \"" + targetLocation + "\": " + err.Error())
|
||||||
}
|
}
|
||||||
return false, false
|
return false, false, nil
|
||||||
}
|
}
|
||||||
if targetInfo.IsDir() {
|
if targetInfo.IsDir() {
|
||||||
if errorOnFail && failCondition == 2 {
|
if errorOnFail && failCondition == 2 {
|
||||||
PrintError("\""+targetLocation+"\" is a directory", ErrorTargetWrongType, true)
|
return false, true, errors.New("\"" + targetLocation + "\" is a directory")
|
||||||
}
|
}
|
||||||
return false, true
|
return false, true, nil
|
||||||
} else {
|
} else {
|
||||||
if errorOnFail && failCondition == 1 {
|
if errorOnFail && failCondition == 1 {
|
||||||
PrintError("\""+targetLocation+"\" is a file", ErrorTargetWrongType, true)
|
return true, true, errors.New("\"" + targetLocation + "\" is a file")
|
||||||
}
|
}
|
||||||
return true, true
|
return true, true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTempFile creates a temporary file and returns a pointer to it.
|
// CreateTempFile creates a temporary file and returns a pointer to it.
|
||||||
func CreateTempFile() *os.File {
|
func CreateTempFile() (*os.File, error) {
|
||||||
tempFile, err := os.CreateTemp("", "*.markdown")
|
tempFile, err := os.CreateTemp("", "*.markdown")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
PrintError("Failed to create temporary file: "+err.Error(), ErrorWrite, true)
|
return nil, errors.New("unable to create temporary file: " + err.Error())
|
||||||
}
|
}
|
||||||
return tempFile
|
return tempFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExpandPathWithHome, given a path (as a string) containing "~", returns the path with "~" expanded to the user's home directory.
|
// ExpandPathWithHome, given a path (as a string) containing "~", returns the path with "~" expanded to the user's home directory.
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
package back
|
package back
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RemoveTrailingEmptyStrings removes empty strings from the end of a slice.
|
// RemoveTrailingEmptyStrings removes empty strings from the end of a slice.
|
||||||
func RemoveTrailingEmptyStrings(slice []string) []string {
|
func RemoveTrailingEmptyStrings(slice []string) []string {
|
||||||
for i := len(slice) - 1; i >= 0; i-- {
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
@@ -14,16 +9,3 @@ func RemoveTrailingEmptyStrings(slice []string) []string {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintError prints an error message in the standard libmutton format and exits with the specified exit code.
|
|
||||||
// Requires: message (the error message to print),
|
|
||||||
// exitCode (the exit code to use),
|
|
||||||
// forceHardExit (if true, exit immediately; if false, allow soft exit for interactive clients).
|
|
||||||
func PrintError(message string, exitCode int, forceHardExit bool) {
|
|
||||||
fmt.Println(AnsiError + message + AnsiReset)
|
|
||||||
if forceHardExit {
|
|
||||||
os.Exit(exitCode)
|
|
||||||
} else {
|
|
||||||
Exit(exitCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package front
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/rwinkhart/go-boilerplate/back"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PrintError prints an error message in the MUTN format and exits with the specified exit code.
|
||||||
|
// Requires: message (the error message to print),
|
||||||
|
// exitCode (the exit code to use),
|
||||||
|
// forceHardExit (if true, exit immediately; if false, allow soft exit for interactive clients).
|
||||||
|
func PrintError(message string, exitCode int, forceHardExit bool) {
|
||||||
|
fmt.Println(back.AnsiError + message + back.AnsiReset)
|
||||||
|
if forceHardExit {
|
||||||
|
os.Exit(exitCode)
|
||||||
|
} else {
|
||||||
|
back.Exit(exitCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user