Initial code commit

This commit is contained in:
2025-05-09 15:43:43 +00:00
parent f08b95f350
commit 857b8526a1
9 changed files with 208 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
package back
import "os"
var Home, _ = os.UserHomeDir()
const (
AnsiError = "\033[38;5;9m"
AnsiReset = "\033[0m"
ErrorRead = 101
ErrorWrite = 102
ErrorTargetNotFound = 105
ErrorTargetWrongType = 107
ErrorOther = 111
)
+32
View File
@@ -0,0 +1,32 @@
package back
import (
"bufio"
"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) {
stdin, err := cmd.StdinPipe()
if err != nil {
PrintError("Failed to access stdin for system command: "+err.Error(), ErrorOther, true)
}
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)
}()
}
// ReadFromStdin is a utility function that reads a string from stdin.
func ReadFromStdin() string {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
return scanner.Text()
}
return ""
}
+10
View File
@@ -0,0 +1,10 @@
//go:build !interactive
package back
import "os"
// Exit (hard) is meant to be used in non-interactive CLI implementations to exit the program after an operation.
func Exit(code int) {
os.Exit(code)
}
+8
View File
@@ -0,0 +1,8 @@
//go:build interactive
package back
// Exit (soft) is meant to be used in interactive implementations (GUIs/TUIs) to keep the program running after an operation.
func Exit(code int) int {
return code
}
+44
View File
@@ -0,0 +1,44 @@
package back
import (
"os"
"strings"
)
// 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).
// Returns: isFile, isAccessible.
func TargetIsFile(targetLocation string, errorOnFail bool, failCondition uint8) (bool, bool) {
targetInfo, err := os.Stat(targetLocation)
if err != nil {
if errorOnFail {
PrintError("Failed to access \""+targetLocation+"\" - Ensure it exists and has the correct permissions", ErrorTargetNotFound, true)
}
return false, false
}
if targetInfo.IsDir() {
if errorOnFail && failCondition == 2 {
PrintError("\""+targetLocation+"\" is a directory", ErrorTargetWrongType, true)
}
return false, true
} else {
if errorOnFail && failCondition == 1 {
PrintError("\""+targetLocation+"\" is a file", ErrorTargetWrongType, true)
}
return true, true
}
}
// CreateTempFile creates a temporary file and returns a pointer to it.
func CreateTempFile() *os.File {
tempFile, err := os.CreateTemp("", "*.markdown")
if err != nil {
PrintError("Failed to create temporary file: "+err.Error(), ErrorWrite, true)
}
return tempFile
}
// ExpandPathWithHome, given a path (as a string) containing "~", returns the path with "~" expanded to the user's home directory.
func ExpandPathWithHome(path string) string {
return strings.Replace(path, "~", Home, 1)
}
+29
View File
@@ -0,0 +1,29 @@
package back
import (
"fmt"
"os"
)
// RemoveTrailingEmptyStrings removes empty strings from the end of a slice.
func RemoveTrailingEmptyStrings(slice []string) []string {
for i := len(slice) - 1; i >= 0; i-- {
if slice[i] != "" {
return slice[:i+1]
}
}
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)
}
}